Class: SparkApi::Models::Photo
- 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
Instance Attribute Summary collapse
-
#update_path ⇒ Object
Returns the value of attribute update_path.
Attributes inherited from Base
Instance Method Summary collapse
- #delete(args = {}) ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(opts = {}) ⇒ Photo
constructor
A new instance of Photo.
- #load_picture(file_name) ⇒ Object
- #primary? ⇒ Boolean
- #save(arguments = {}) ⇒ Object
- #save!(arguments = {}) ⇒ Object
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, #load, #method_missing, #parse_id, #path, path, #persisted?, prefix, prefix=, #resource_uri, #respond_to?
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_path ⇒ Object
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
57 58 59 |
# File 'lib/spark_api/models/photo.rb', line 57 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
22 23 24 |
# File 'lib/spark_api/models/photo.rb', line 22 def primary? @attributes["Primary"] == true 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 |