Module: Frame::Util

Defined in:
lib/frame/util.rb

Constant Summary collapse

OBJECT_CLASSES =
{
  "account" => "Account",
  "bank_account" => "BankAccount",
  "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

Class Method Details

.convert_to_frame_object(resp, opts = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/frame/util.rb', line 35

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



77
78
79
# File 'lib/frame/util.rb', line 77

def self.normalize_id(id)
  id&.to_s
end

.normalize_opts(opts) ⇒ Object



81
82
83
# File 'lib/frame/util.rb', line 81

def self.normalize_opts(opts)
  opts.clone
end

.object_classesObject



31
32
33
# File 'lib/frame/util.rb', line 31

def self.object_classes
  OBJECT_CLASSES
end

.object_classes_to_constantsObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/frame/util.rb', line 85

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/frame/util.rb', line 57

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