Class: FlexmlsApi::Models::Photo

Inherits:
Base
  • Object
show all
Extended by:
Subresource
Defined in:
lib/flexmls_api/models/photo.rb

Constant Summary collapse

EDITABLE_FIELDS =
[:Picture, :FileName, :Name, :Caption, :Primary]

Constants included from Paginate

Paginate::DEFAULT_PAGE_SIZE

Instance Attribute Summary collapse

Attributes inherited from Base

#attributes, #changed, #errors

Instance Method Summary collapse

Methods included from Subresource

build_subclass, find_by_id, find_by_listing_key

Methods inherited from Base

#connection, connection, count, element_name, element_name=, first, get, #load, #method_missing, #parse_id, path, prefix, prefix=, #respond_to?

Methods included from Paginate

#collect, #paginate, #per_page

Constructor Details

#initialize(opts = {}) ⇒ Photo

Returns a new instance of Photo.



11
12
13
14
15
16
17
18
# File 'lib/flexmls_api/models/photo.rb', line 11

def initialize(opts={})
  defaulted_opts = {}
  EDITABLE_FIELDS.each do |k|
    key = k.to_s()
    defaulted_opts[key] = opts[key] || nil
  end
  super(opts.merge(defaulted_opts))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class FlexmlsApi::Models::Base

Instance Attribute Details

#update_pathObject

Returns the value of attribute update_path.



7
8
9
# File 'lib/flexmls_api/models/photo.rb', line 7

def update_path
  @update_path
end

Instance Method Details

#delete(args = {}) ⇒ Object



49
50
51
# File 'lib/flexmls_api/models/photo.rb', line 49

def delete(args={})
  connection.delete("#{update_path}/#{self.Id}", args)
end

#exists?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/flexmls_api/models/photo.rb', line 53

def exists?
  @attributes.include?("Id")
end

#load_picture(file_name) ⇒ Object



44
45
46
47
# File 'lib/flexmls_api/models/photo.rb', line 44

def load_picture(file_name)
  self.Picture = Base64.encode64(File.open(file_name, 'rb').read).gsub(/\n/, '')
  self.FileName = File.basename(file_name)
end

#primary?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/flexmls_api/models/photo.rb', line 20

def primary? 
  @attributes["Primary"] == true 
end

#save(arguments = {}) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/flexmls_api/models/photo.rb', line 24

def save(arguments={})
  begin
    return save!(arguments)
  rescue NotFound, BadResourceRequest => e
    FlexmlsApi.logger.error("Failed to save resource #{self}: #{e.message}")
  end
  false
end

#save!(arguments = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/flexmls_api/models/photo.rb', line 32

def save!(arguments={})
  payload = {"Photos" => [ build_photo_hash]}
  if exists?
    results = connection.put "#{update_path}/#{self.Id}", payload, arguments
  else
    results = connection.post update_path, payload, arguments
  end
  result = results.first
  load(result)
  true
end