Class: Supplejack::Item
- Inherits:
-
Object
- Object
- Supplejack::Item
- Includes:
- Request
- Defined in:
- lib/supplejack/item.rb
Overview
The Item
class represents a SetItem on the Supplejack API
An Item always belongs to a UserSet. In the API the SetItem class only has a record_id and position, but it gets augmented with some of the attributes of the Record that it represents.
An Item object can have the following values:
-
record_id
-
position
If more attributes of the Record are needed, they should be added to the SetItem::ATTRIBUTES array in the API SetItem model.
Constant Summary collapse
- ATTRIBUTES =
[:record_id, :title, :description, :large_thumbnail_url, :thumbnail_url, :contributing_partner, :display_content_partner, :display_collection, :landing_url, :category, :date, :dnz_type, :dc_identifier, :creator]
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#position ⇒ Object
Returns the value of attribute position.
-
#user_set_id ⇒ Object
readonly
Returns the value of attribute user_set_id.
Instance Method Summary collapse
-
#date ⇒ Object
Date getter to force the date attribute to be a Date object.
-
#destroy ⇒ true, false
Executes a DELETE request to the API to remove the current Item.
- #id ⇒ Object
-
#initialize(attributes = {}) ⇒ Item
constructor
A new instance of Item.
- #method_missing(symbol, *args, &block) ⇒ Object
-
#save ⇒ true, false
Executes a POST request to the API to persist the current Item.
Methods included from Request
Constructor Details
#initialize(attributes = {}) ⇒ Item
Returns a new instance of Item.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/supplejack/item.rb', line 34 def initialize(attributes={}) @attributes = attributes.try(:symbolize_keys) || {} @user_set_id = @attributes[:user_set_id] @api_key = @attributes[:api_key] @position = @attributes[:position] ATTRIBUTES.each do |attribute| self.instance_variable_set("@#{attribute}", @attributes[attribute]) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
92 93 94 |
# File 'lib/supplejack/item.rb', line 92 def method_missing(symbol, *args, &block) return nil end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
32 33 34 |
# File 'lib/supplejack/item.rb', line 32 def api_key @api_key end |
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
31 32 33 |
# File 'lib/supplejack/item.rb', line 31 def attributes @attributes end |
#errors ⇒ Object
Returns the value of attribute errors.
32 33 34 |
# File 'lib/supplejack/item.rb', line 32 def errors @errors end |
#position ⇒ Object
Returns the value of attribute position.
32 33 34 |
# File 'lib/supplejack/item.rb', line 32 def position @position end |
#user_set_id ⇒ Object (readonly)
Returns the value of attribute user_set_id.
31 32 33 |
# File 'lib/supplejack/item.rb', line 31 def user_set_id @user_set_id end |
Instance Method Details
#date ⇒ Object
Date getter to force the date attribute to be a Date object.
87 88 89 90 |
# File 'lib/supplejack/item.rb', line 87 def date @date = @date.first if @date.is_a?(Array) Time.parse(@date) rescue nil if @date end |
#destroy ⇒ true, false
Executes a DELETE request to the API to remove the current Item
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/supplejack/item.rb', line 74 def destroy begin delete("/sets/#{self.user_set_id}/records/#{self.record_id}", {api_key: self.api_key}) Rails.cache.delete("/users/#{self.api_key}/sets") if Supplejack.enable_caching return true rescue StandardError => e self.errors = e.inspect return false end end |
#id ⇒ Object
49 50 51 |
# File 'lib/supplejack/item.rb', line 49 def id self.record_id end |
#save ⇒ true, false
Executes a POST request to the API to persist the current Item
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/supplejack/item.rb', line 57 def save begin api_attributes = {record_id: self.record_id} api_attributes[:position] = self.position if self.position.present? post("/sets/#{self.user_set_id}/records", {api_key: self.api_key}, {record: api_attributes}) Rails.cache.delete("/users/#{self.api_key}/sets") if Supplejack.enable_caching return true rescue StandardError => e self.errors = e.inspect return false end end |