Module: Frame::Util

Defined in:
lib/frame/util.rb

Constant Summary collapse

OBJECT_CLASSES =
{
  "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",
  "list" => "ListObject"
}.freeze

Class Method Summary collapse

Class Method Details

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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/frame/util.rb', line 25

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



67
68
69
# File 'lib/frame/util.rb', line 67

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

.normalize_opts(opts) ⇒ Object



71
72
73
# File 'lib/frame/util.rb', line 71

def self.normalize_opts(opts)
  opts.clone
end

.object_classesObject



21
22
23
# File 'lib/frame/util.rb', line 21

def self.object_classes
  OBJECT_CLASSES
end

.object_classes_to_constantsObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/frame/util.rb', line 75

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/frame/util.rb', line 47

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