Class: Supplejack::StoryItem

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.meta ||= {}
  self.attributes = @attributes
end

Instance Attribute Details

#api_keyObject (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

#errorsObject

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_attributesObject



56
57
58
# File 'lib/supplejack/story_item.rb', line 56

def api_attributes
  retrieve_attributes(MODIFIABLE_ATTRIBUTES)
end

#attributesObject



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

#destroytrue, 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.

Returns:

  • (true, false)

    True if the API response was successful, false if not.



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.message

    false
  end
end

#new_record?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/supplejack/story_item.rb', line 60

def new_record?
  id.nil?
end

#savetrue, 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.

Returns:

  • (true, false)

    True if the API response was successful, false if not.



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.message

    false
  end
end

#to_jsonObject



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

Returns:

  • (true, false)

    True if the API response was successful, false if not.



125
126
127
128
129
# File 'lib/supplejack/story_item.rb', line 125

def update_attributes(attributes={})
  self.attributes = attributes

  self.save
end