Module: Immoscout::Models::Concerns::Modelable

Extended by:
ActiveSupport::Concern
Included in:
Actions::Attachment, Actions::Contact, Actions::Publish, Actions::RealEstate
Defined in:
lib/immoscout/models/concerns/modelable.rb

Overview

Provides base functionality to reference/map/(de)serialize models against the immoscout API.

Class Method Summary collapse

Class Method Details

.from_raw(raw_hash) ⇒ Object



29
30
31
32
# File 'lib/immoscout/models/concerns/modelable.rb', line 29

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

.handle_response(response) ⇒ Object



34
35
36
37
38
# File 'lib/immoscout/models/concerns/modelable.rb', line 34

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

  raise Immoscout::Errors::Failed, response
end

.id_from_response(response) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/immoscout/models/concerns/modelable.rb', line 40

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

.unpack(hash) ⇒ Object



25
26
27
# File 'lib/immoscout/models/concerns/modelable.rb', line 25

def unpack(hash)
  hash.values.first
end