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

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

Overview

Actions to work with attachments.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Modelable

from_raw, handle_response, id_from_response, unpack

Class Method Details

.all(real_estate_id) ⇒ Object



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

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



64
65
66
67
68
69
70
71
72
# File 'lib/immoscout/models/actions/attachment.rb', line 64

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

#destroy ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/immoscout/models/actions/attachment.rb', line 37

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

#save ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/immoscout/models/actions/attachment.rb', line 20

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