Method: Telnyx::Util.convert_to_telnyx_object

Defined in:
lib/telnyx/util.rb

.convert_to_telnyx_object(data, opts = {}) ⇒ Object

Converts a hash of fields or an array of hashes into a TelnyxObject or array of TelnyxObjects. These new objects will be created as a concrete type as dictated by their ‘record_type` field (e.g. a `record_type` value of `messaging_profile` would create an instance of MessagingProfile), but if `record_type` is not present or of an unknown type, the newly created instance will fall back to being a TelnyxObject.

Attributes

  • Data - Hash of fields and values to be converted into a TelnyxObject.

  • opts - Options for TelnyxObject like an API key that will be reused on subsequent API calls.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/telnyx/util.rb', line 71

def self.convert_to_telnyx_object(data, opts = {})
  case data
  when Array
    data.map { |i| convert_to_telnyx_object(i, opts) }
  when Hash
    # Try converting to a known object class. If none available, fall back to generic TelnyxObject
    if data[:data].is_a?(Array)
      ListObject.construct_from(data, opts)
    elsif data[:data].is_a?(Hash) && data[:data][:record_type]
      object_classes.fetch(data[:data][:record_type], TelnyxObject).construct_from(data[:data], opts)
    elsif data[:record_type]
      object_classes.fetch(data[:record_type], TelnyxObject).construct_from(data, opts)
    else
      TelnyxObject.construct_from(data, opts)
    end
  else
    data
  end
end