class Intercom::Lib::TypedJsonDeserializer

Responsibility: To decide whether we are deserializing a collection or an entity of a particular type and to dispatch deserialization

Attributes

json[R]

Public Class Methods

new(json, client, type = nil) click to toggle source
# File lib/intercom/lib/typed_json_deserializer.rb, line 12
def initialize(json, client, type = nil)
  @json = json
  @client = client
  @type = type
end

Public Instance Methods

deserialize() click to toggle source
# File lib/intercom/lib/typed_json_deserializer.rb, line 18
def deserialize
  if blank_object_type?(object_type)
    raise DeserializationError, 'No type field was found to facilitate deserialization'
  elsif list_object_type?(object_type)
    deserialize_collection(json[object_entity_key])
  else # singular object type
    deserialize_object(json)
  end
end

Private Instance Methods

blank_object_type?(object_type) click to toggle source
# File lib/intercom/lib/typed_json_deserializer.rb, line 30
def blank_object_type?(object_type)
  object_type.nil? || object_type == '' && @type.nil?
end
deserialize_collection(collection_json) click to toggle source
# File lib/intercom/lib/typed_json_deserializer.rb, line 38
def deserialize_collection(collection_json)
  return [] if collection_json.nil?

  collection_json.map { |item_json| TypedJsonDeserializer.new(item_json, @client).deserialize }
end
deserialize_object(object_json) click to toggle source
# File lib/intercom/lib/typed_json_deserializer.rb, line 44
def deserialize_object(object_json)
  entity_class = Utils.constantize_singular_resource_name(object_entity_key)
  deserialized = entity_class.from_api(object_json)
  deserialized.client = @client
  deserialized
end
list_object_type?(object_type) click to toggle source
# File lib/intercom/lib/typed_json_deserializer.rb, line 34
def list_object_type?(object_type)
  object_type.end_with?('.list')
end
object_entity_key() click to toggle source
# File lib/intercom/lib/typed_json_deserializer.rb, line 59
def object_entity_key
  @object_entity_key ||= Utils.entity_key_from_type(object_type)
end
object_type() click to toggle source
# File lib/intercom/lib/typed_json_deserializer.rb, line 51
def object_type
  if !@type.nil?
    @object_type = @type
  else
    @object_type ||= json['type'] 
  end
end