Class: SparkApi::Models::Photo

Inherits:
Base
  • Object
show all
Extended by:
Subresource
Defined in:
lib/spark_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, #errors, #parent

Instance Method Summary collapse

Methods included from Subresource

build_subclass, find_by_id, find_by_listing_key, parse_date_start_and_end_times

Methods inherited from Base

#connection, connection, count, element_name, element_name=, first, get, #id, #load, #method_missing, #parse_id, path, #path, #persisted?, prefix, prefix=, #resource_pluralized, #resource_uri, #respond_to?, #to_param, #to_partial_path

Methods included from Paginate

#collect, #paginate, #per_page

Methods included from Dirty

#changed, #changed?, #changed_attributes, #changes, #dirty_attributes, #previous_changes

Constructor Details

#initialize(opts = {}) ⇒ Photo

Returns a new instance of Photo.



13
14
15
16
17
18
19
20
# File 'lib/spark_api/models/photo.rb', line 13

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 SparkApi::Models::Base

Instance Attribute Details

#update_pathObject

Returns the value of attribute update_path.



9
10
11
# File 'lib/spark_api/models/photo.rb', line 9

def update_path
  @update_path
end

Instance Method Details

#delete(args = {}) ⇒ Object



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

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

#exists?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/spark_api/models/photo.rb', line 71

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

#load_picture(file_name) ⇒ Object



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

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)


22
23
24
# File 'lib/spark_api/models/photo.rb', line 22

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

#rollback(version) ⇒ Object



57
58
59
60
# File 'lib/spark_api/models/photo.rb', line 57

def rollback(version)
  payload = {"Version" => version}
  connection.put "#{update_path}/#{self.Id}/versions/current", payload
end

#rotate!(direction) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/spark_api/models/photo.rb', line 63

def rotate!(direction)
  unless [:clockwise, :counterclockwise].include? direction.to_sym
    raise ArgumentError, "Photo rotate failed. '#{direction}' is not supported."
  end
  payload = { 'Photos' => [{'Rotate' => direction.to_s }] }
  connection.put "#{self.update_path}/#{self.Id}", payload
end

#save(arguments = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/spark_api/models/photo.rb', line 26

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

#save!(arguments = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/spark_api/models/photo.rb', line 36

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