Module: Immoscout::Models::Actions::Attachment

Extended by:
ActiveSupport::Concern
Includes:
Concerns::Modelable
Included in:
Document, Picture
Defined in:
lib/immoscout/models/actions/attachment.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Modelable

#api, from_raw, #handle_response, handle_response, #id_from_response, id_from_response, unpack

Class Method Details

.all(real_estate_id) ⇒ Object

rubocop:disable Metrics/AbcSize because of the mapping logic



84
85
86
87
88
89
90
91
92
93
# File 'lib/immoscout/models/actions/attachment.rb', line 84

def all(real_estate_id)
  response = api.get(
    "user/#{api.user_name}/realestate/#{real_estate_id}/attachment"
  )
  handle_response(response)
  objects = unpack_collection.call(response.body)
  objects
    .map { |object| new(object) }
    .select { |object| object.type =~ /#{name.demodulize}/i }
end

.content_type_from_extension(ext) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/immoscout/models/actions/attachment.rb', line 73

def content_type_from_extension(ext)
  {
    '.jpg' => 'image/jpeg',
    '.jpeg' => 'image/jpeg',
    '.gif' => 'image/gif',
    '.png' => 'image/png',
    '.pdf' => 'application/pdf'
  }.fetch(ext)
end

Instance Method Details

#destroyObject

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength



46
47
48
49
50
51
52
53
54
55
# File 'lib/immoscout/models/actions/attachment.rb', line 46

def destroy
  attachable_id = attachable.try(:id) || attachable
  response = api.delete(
    "user/#{api.user_name}/" \
    "realestate/#{attachable_id}/" \
    "attachment/#{id}"
  )
  handle_response(response)
  self
end

#saveObject

rubocop:disable Metrics/AbcSize because this is the

bare minimum logic

rubocop:disable Metrics/MethodLength dito



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/immoscout/models/actions/attachment.rb', line 27

def save
  attachable_id = attachable.try(:id) || attachable
  response = api.post(
    "user/#{api.user_name}/realestate/#{attachable_id}/attachment",
    nil,
    attachment: Faraday::UploadIO.new(file, content_type, file_name),
    metadata: Faraday::UploadIO.new(
      StringIO.new(to_json),
      'application/json',
      'metadata.json'
    )
  )
  handle_response(response)
  self.id = id_from_response(response)
  self
end