Sha256: df1589b4a0f929e05f09c1a553917ab226f54a709b64db4cd7840ac41e5f5413

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 KB

Contents

require 'json'

module Immoscout
  module Models
    module Concerns
      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

5 entries across 5 versions & 1 rubygems

Version Path
immoscout-1.3.1 lib/immoscout/models/concerns/modelable.rb
immoscout-1.3.0 lib/immoscout/models/concerns/modelable.rb
immoscout-1.2.0 lib/immoscout/models/concerns/modelable.rb
immoscout-1.1.0 lib/immoscout/models/concerns/modelable.rb
immoscout-1.0.0 lib/immoscout/models/concerns/modelable.rb