Sha256: e395f71624cb4af2681e7bd6cffb944416bf115116d047f6719e84f506ef4a21

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Immoscout
  module Models
    module Concerns
      # Provides base functionality to reference/map/(de)serialize
      # models against the immoscout API.
      module Modelable
        extend ActiveSupport::Concern

        included do
          cattr_accessor :json_wrapper, :unpack_collection
          cattr_reader :api, instance_accessor: false do
            Immoscout::Api::Client.instance
          end

          def api
            self.class.api
          end

          def handle_response(response)
            self.class.handle_response(response)
          end

          def id_from_response(response)
            self.class.id_from_response(response)
          end
        end

        class_methods do
          def unpack(hash)
            hash.values.first
          end

          def from_raw(raw_hash)
            hash = raw_hash.is_a?(String) ? JSON.parse(raw_hash) : raw_hash
            new(unpack(hash))
          end

          def handle_response(response)
            return response if response.success?

            raise Immoscout::Errors::Failed, response
          end

          def id_from_response(response)
            response
              .body
              .fetch('common.messages')
              .first
              .fetch('message', {})
              .fetch('id', nil)
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
immoscout-1.9.0 lib/immoscout/models/concerns/modelable.rb
immoscout-1.8.1 lib/immoscout/models/concerns/modelable.rb
immoscout-1.8.0 lib/immoscout/models/concerns/modelable.rb