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

Instance Method Summary collapse

Class Method Details

.from_raw(raw_hash) ⇒ Object



37
38
39
40
# File 'lib/immoscout/models/concerns/modelable.rb', line 37

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



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

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

  raise Immoscout::Errors::Failed, response
end

.id_from_response(response) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/immoscout/models/concerns/modelable.rb', line 48

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

.unpack(hash) ⇒ Object



33
34
35
# File 'lib/immoscout/models/concerns/modelable.rb', line 33

def unpack(hash)
  hash.values.first
end

Instance Method Details

#apiObject



19
20
21
# File 'lib/immoscout/models/concerns/modelable.rb', line 19

def api
  self.class.api
end

#handle_response(response) ⇒ Object



23
24
25
# File 'lib/immoscout/models/concerns/modelable.rb', line 23

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

#id_from_response(response) ⇒ Object



27
28
29
# File 'lib/immoscout/models/concerns/modelable.rb', line 27

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