Sha256: 5788a9f6299ee1ba7c05bcf069a65e4398c6b7978c17c1265b60d5b17bc66891

Contents?: true

Size: 1.49 KB

Versions: 33

Compression:

Stored size: 1.49 KB

Contents

require 'intercom/utils'

module Intercom
  module Lib
      # Responsibility: To decide whether we are deserializing a collection or an
      # entity of a particular type and to dispatch deserialization
      class TypedJsonDeserializer
        attr_reader :json

        def initialize(json)
          @json = json
        end

        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

        def blank_object_type?(object_type)
          object_type.nil? || object_type == ''
        end

        def list_object_type?(object_type)
          object_type.end_with?('.list')
        end

        def deserialize_collection(collection_json)
          return [] if collection_json == nil
          collection_json.map { |item_json| TypedJsonDeserializer.new(item_json).deserialize }
        end

        def deserialize_object(object_json)
          entity_class = Utils.constantize_singular_resource_name(object_entity_key)
          entity_class.from_api(object_json)
        end

        def object_type
          @object_type ||= json['type']
        end

        def object_entity_key
          @object_entity_key ||= Utils.entity_key_from_type(object_type)
        end

      end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
intercom-3.5.24 lib/intercom/lib/typed_json_deserializer.rb
intercom-3.5.23 lib/intercom/lib/typed_json_deserializer.rb
intercom-3.5.22 lib/intercom/lib/typed_json_deserializer.rb
intercom-3.5.21 lib/intercom/lib/typed_json_deserializer.rb
intercom-3.5.20 lib/intercom/lib/typed_json_deserializer.rb
intercom-3.5.19 lib/intercom/lib/typed_json_deserializer.rb
intercom-3.5.17 lib/intercom/lib/typed_json_deserializer.rb
intercom-3.5.16 lib/intercom/lib/typed_json_deserializer.rb
intercom-3.5.15 lib/intercom/lib/typed_json_deserializer.rb
intercom-3.5.14 lib/intercom/lib/typed_json_deserializer.rb
intercom-3.5.12 lib/intercom/lib/typed_json_deserializer.rb
intercom-3.5.11 lib/intercom/lib/typed_json_deserializer.rb
intercom-3.5.10 lib/intercom/lib/typed_json_deserializer.rb