Module: Frame::Util
- Defined in:
- lib/frame/util.rb
Constant Summary collapse
- OBJECT_CLASSES =
{ "account" => "Account", "customer" => "Customer", "charge_intent" => "ChargeIntent", "payment_method" => "PaymentMethod", "refund" => "Refund", "invoice" => "Invoice", "invoice_line_item" => "InvoiceLineItem", "subscription" => "Subscription", "subscription_phase" => "SubscriptionPhase", "product" => "Product", "product_phase" => "ProductPhase", "webhook_endpoint" => "WebhookEndpoint", "customer_identity_verification" => "CustomerIdentityVerification", "onboarding_session" => "Onboarding", "dispute" => "Dispute", "coupon" => "Coupon", "promotion_code" => "PromotionCode", "discount" => "Discount", "transfer" => "Transfer", "transfer_fee_plan" => "TransferFeePlan", "transfer_billing_agreement" => "TransferBillingAgreement", "list" => "ListObject" }.freeze
Class Method Summary collapse
- .convert_to_frame_object(resp, opts = {}) ⇒ Object
- .normalize_id(id) ⇒ Object
- .normalize_opts(opts) ⇒ Object
- .object_classes ⇒ Object
- .object_classes_to_constants ⇒ Object
- .symbolize_names(object) ⇒ Object
Class Method Details
.convert_to_frame_object(resp, opts = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/frame/util.rb', line 34 def self.convert_to_frame_object(resp, opts = {}) case resp when Array resp.map { |i| convert_to_frame_object(i, opts) } when Hash # Try converting to a specific object class if this is a response with an object key object_name = resp[:object] if object_name && object_classes[object_name] klass = object_classes_to_constants[object_name] klass.construct_from(resp, opts) elsif resp[:data]&.is_a?(Array) # This is a list object ListObject.construct_from(resp, opts) else FrameObject.construct_from(resp, opts) end else resp end end |
.normalize_id(id) ⇒ Object
76 77 78 |
# File 'lib/frame/util.rb', line 76 def self.normalize_id(id) id&.to_s end |
.normalize_opts(opts) ⇒ Object
80 81 82 |
# File 'lib/frame/util.rb', line 80 def self.normalize_opts(opts) opts.clone end |
.object_classes ⇒ Object
30 31 32 |
# File 'lib/frame/util.rb', line 30 def self.object_classes OBJECT_CLASSES end |
.object_classes_to_constants ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/frame/util.rb', line 84 def self.object_classes_to_constants @object_classes_to_constants ||= begin constants = {} object_classes.each do |object_name, class_name| # Store with both string and symbol keys for flexibility constants[object_name] = Frame.const_get(class_name, false) constants[object_name.to_sym] = Frame.const_get(class_name, false) end constants end end |
.symbolize_names(object) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/frame/util.rb', line 56 def self.symbolize_names(object) case object when Hash new_hash = {} object.each do |key, value| key = begin key.to_sym rescue key end || key new_hash[key] = symbolize_names(value) end new_hash when Array object.map { |value| symbolize_names(value) } else object end end |