Class: Supplejack::StoryItem
- Inherits:
-
Object
- Object
- Supplejack::StoryItem
- Includes:
- Request
- Defined in:
- lib/supplejack/story_item.rb
Overview
The StoryItem
class represents a StoryItem on the Supplejack API
A StoryItem always belongs to a Story. represents.
A StoryItem object has the following values:
-
id
-
type
-
sub_type
-
position
-
meta [Hash]
-
content [Hash]
Constant Summary collapse
- MODIFIABLE_ATTRIBUTES =
[:position, :meta, :content, :type, :sub_type].freeze
- UNMODIFIABLE_ATTRIBUTES =
[:id, :story_id].freeze
- ATTRIBUTES =
(MODIFIABLE_ATTRIBUTES + UNMODIFIABLE_ATTRIBUTES).freeze
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#errors ⇒ Object
Returns the value of attribute errors.
Instance Method Summary collapse
- #api_attributes ⇒ Object
- #attributes ⇒ Object
-
#attributes=(attributes) ⇒ Object
Assigns the provided attributes to the StoryItem object.
-
#destroy ⇒ true, false
Executes a DELETE request to the API with the StoryItem ID and the stories api_key.
-
#initialize(attributes = {}) ⇒ StoryItem
constructor
A new instance of StoryItem.
- #new_record? ⇒ Boolean
-
#save ⇒ true, false
Executes a POST request when the StoryItem hasn’t been persisted to the API, otherwise it execute a PATCH request.
- #to_json ⇒ Object
-
#update_attributes(attributes = {}) ⇒ true, false
Updates the StoryItems attributes and persists it to the API.
Methods included from Request
#delete, #get, #patch, #post, #put
Constructor Details
#initialize(attributes = {}) ⇒ StoryItem
Returns a new instance of StoryItem.
34 35 36 37 38 39 40 |
# File 'lib/supplejack/story_item.rb', line 34 def initialize(attributes={}) @attributes = attributes.try(:deep_symbolize_keys) || {} @api_key = @attributes[:api_key] self. ||= {} self.attributes = @attributes end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
32 33 34 |
# File 'lib/supplejack/story_item.rb', line 32 def api_key @api_key end |
#errors ⇒ Object
Returns the value of attribute errors.
31 32 33 |
# File 'lib/supplejack/story_item.rb', line 31 def errors @errors end |
Instance Method Details
#api_attributes ⇒ Object
56 57 58 |
# File 'lib/supplejack/story_item.rb', line 56 def api_attributes retrieve_attributes(MODIFIABLE_ATTRIBUTES) end |
#attributes ⇒ Object
52 53 54 |
# File 'lib/supplejack/story_item.rb', line 52 def attributes retrieve_attributes(ATTRIBUTES) end |
#attributes=(attributes) ⇒ Object
Assigns the provided attributes to the StoryItem object
44 45 46 47 48 49 50 |
# File 'lib/supplejack/story_item.rb', line 44 def attributes=(attributes) attributes = attributes.try(:deep_symbolize_keys) || {} attributes.each do |attr, value| self.send("#{attr}=", value) if ATTRIBUTES.include?(attr) end end |
#destroy ⇒ true, false
Executes a DELETE request to the API with the StoryItem ID and the stories api_key
When the API returns an error response, the errors are available through the StoryItem#errors virtual attribute.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/supplejack/story_item.rb', line 105 def destroy return false if self.new_record? begin delete("/stories/#{story_id}/items/#{id}", {api_key: api_key}) Rails.cache.delete("/users/#{api_key}/stories") if Supplejack.enable_caching true rescue StandardError => e self.errors = e. false end end |
#new_record? ⇒ Boolean
60 61 62 |
# File 'lib/supplejack/story_item.rb', line 60 def new_record? id.nil? end |
#save ⇒ true, false
Executes a POST request when the StoryItem hasn’t been persisted to the API, otherwise it execute a PATCH request.
When the API returns a error response, the errors are available through the Story#errors virtual attribute.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/supplejack/story_item.rb', line 72 def save begin if self.new_record? self.attributes = post( "/stories/#{story_id}/items", {api_key: api_key}, {item: self.api_attributes} ) else self.attributes = patch( "/stories/#{story_id}/items/#{id}", params: {api_key: api_key}, payload: {item: self.api_attributes} ) end Rails.cache.delete("/users/#{self.api_key}/stories") if Supplejack.enable_caching true rescue StandardError => e self.errors = e. false end end |
#to_json ⇒ Object
131 132 133 |
# File 'lib/supplejack/story_item.rb', line 131 def to_json attributes.to_json end |
#update_attributes(attributes = {}) ⇒ true, false
Updates the StoryItems attributes and persists it to the API
125 126 127 128 129 |
# File 'lib/supplejack/story_item.rb', line 125 def update_attributes(attributes={}) self.attributes = attributes self.save end |