Class: Supplejack::UserSet
- Inherits:
-
Object
- Object
- Supplejack::UserSet
- Extended by:
- ActiveModel::Naming, Request
- Includes:
- ActiveModel::Conversion
- Defined in:
- lib/supplejack/user_set.rb
Overview
The UserSet
class represents a UserSet on the Supplejack API
A UserSet instance can have many Item objects, the relationship with the Item objects is managed through the ItemRelation class which adds ActiveRecord like behaviour to the relationship.
A Item object can have the following values:
-
id
-
name
-
description
-
privacy
-
priority
-
count
-
tags
Constant Summary collapse
- ATTRIBUTES =
[:id, :name, :description, :privacy, :url, :priority, :count, :tags, :tag_list, :homepage, :records, :created_at, :updated_at, :approved, :record]
- PRIVACY_STATES =
['public', 'hidden', 'private']
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the api_key from the UserSet or from the User which this set belongs to.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
-
.find(id, api_key = nil) ⇒ UserSet
Executes a GET request with the provided UserSet ID and initializes a UserSet object with the response from the API.
-
.homepage_sets ⇒ Array
Execute a GET request to the API /sets/home endpoint to retrieve all UserSet objects which have the :homepage flag set to true.
-
.model_name ⇒ Object
This method is normally provided by ActiveModel::Naming module, but here we have to override it so that in the Supplejack Website application it behaves as if the Supplejack::UserSet class was not under the Supplejack namespace.
-
.public_sets(options = {}) ⇒ Array
Executes a GET request to the API /sets/public endpoint to retrieve all public UserSet objects.
Instance Method Summary collapse
-
#api_attributes ⇒ Hash
Return a Hash of attributes which is used to extract the relevant UserSet attributes when creating or updating a UserSet.
-
#api_records ⇒ Array
Return a array of hashes with the format: 123, position: 1 Each hash represents a Supplejack::Item within the UserSet.
- #attributes ⇒ Object
-
#attributes=(attributes) ⇒ Object
Assigns the provided attributes to the UserSet object.
-
#destroy ⇒ true, false
Executes a DELETE request to the API with the UserSet ID and the user’s api_key.
- #favourite? ⇒ Boolean
-
#has_record?(record_id) ⇒ true, false
Convinience method to find out if a particular record_id is part of the UserSet.
- #hidden? ⇒ Boolean
-
#initialize(attributes = {}) ⇒ UserSet
constructor
A new instance of UserSet.
-
#items ⇒ ItemRelation
Initializes a ItemRelation class which adds behaviour to build, create and find items related to this particular UserSet instance.
-
#new_record? ⇒ true, false
Returns if the UserSet is persisted to the API or not.
-
#ordered_records_from_array(record_ids) ⇒ Array
Takes an ordered Array of record_ids and converts it into a Array of Hashes which the API expects, inferring the position value from the position of the record_id in the Array.
-
#owned_by?(user) ⇒ true, false
Compares the api_key of the user and the api_key assigned to the UserSet to find out if the user passed is the owner of the set.
- #persisted? ⇒ Boolean
-
#priority ⇒ Object
Returns the UserSet priority which defaults to 1.
- #private? ⇒ Boolean
- #public? ⇒ Boolean
-
#reload ⇒ Object
Fetches the UserSet information from the API again, in case it had changed.
-
#save ⇒ true, false
Executes a POST request when the UserSet hasn’t been persisted to the API, otherwise it executes a PUT request.
-
#set_record_id ⇒ Intger?
Returns the record id of the associated record in a safe manner.
-
#tag_list ⇒ Object
Returns a comma separated list of tags for this UserSet.
-
#update_attributes(attributes = {}) ⇒ true, false
Updates the UserSet attributes and persists it to the API.
-
#viewable_by?(user) ⇒ true, false
A UserSet is viewable by anyone if it’s public or hidden When the UserSet is private it’s only viewable by the owner.
Methods included from Request
Constructor Details
#initialize(attributes = {}) ⇒ UserSet
36 37 38 39 40 |
# File 'lib/supplejack/user_set.rb', line 36 def initialize(attributes={}) @attributes = attributes.try(:symbolize_keys) || {} @user = Supplejack::User.new(@attributes[:user]) self.attributes = @attributes end |
Instance Attribute Details
#api_key ⇒ Object
Returns the api_key from the UserSet or from the User which this set belongs to.
101 102 103 |
# File 'lib/supplejack/user_set.rb', line 101 def api_key @api_key end |
#errors ⇒ Object
Returns the value of attribute errors.
32 33 34 |
# File 'lib/supplejack/user_set.rb', line 32 def errors @errors end |
#user ⇒ Object
Returns the value of attribute user.
32 33 34 |
# File 'lib/supplejack/user_set.rb', line 32 def user @user end |
Class Method Details
.find(id, api_key = nil) ⇒ UserSet
Executes a GET request with the provided UserSet ID and initializes a UserSet object with the response from the API.
289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/supplejack/user_set.rb', line 289 def self.find(id, api_key=nil) begin response = get("/sets/#{id}") attributes = response["set"] || {} user_set = new(attributes) user_set.api_key = api_key if api_key.present? user_set rescue RestClient::ResourceNotFound => e raise Supplejack::SetNotFound, "UserSet with ID #{id} was not found" end end |
.homepage_sets ⇒ Array
Execute a GET request to the API /sets/home endpoint to retrieve all UserSet objects which have the :homepage flag set to true
324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/supplejack/user_set.rb', line 324 def self.homepage_sets path = "/sets/home" if Supplejack.enable_caching response = Rails.cache.fetch(path, expires_in: 1.day) do get(path) end else response = get(path) end sets_array = response["sets"] || [] user_sets = sets_array.map {|attrs| new(attrs) } end |
.model_name ⇒ Object
This method is normally provided by ActiveModel::Naming module, but here we have to override it so that in the Supplejack Website application it behaves as if the Supplejack::UserSet class was not under the Supplejack namespace.
This is useful when using a Supplejack::UserSet object in the Rails provided routes helpers. Example:
user_set_path(@user_set)
345 346 347 |
# File 'lib/supplejack/user_set.rb', line 345 def self.model_name ActiveModel::Name.new(self, nil, "UserSet") end |
.public_sets(options = {}) ⇒ Array
Executes a GET request to the API /sets/public endpoint to retrieve all public UserSet objects.
311 312 313 314 315 316 317 |
# File 'lib/supplejack/user_set.rb', line 311 def self.public_sets(={}) .reverse_merge!(page: 1, per_page: 100) response = get("/sets/public", ) sets_array = response["sets"] || [] user_sets = sets_array.map {|attrs| new(attrs) } Supplejack::PaginatedCollection.new(user_sets, [:page].to_i, [:per_page].to_i, response["total"].to_i) end |
Instance Method Details
#api_attributes ⇒ Hash
Return a Hash of attributes which is used to extract the relevant UserSet attributes when creating or updating a UserSet
56 57 58 59 60 61 62 63 |
# File 'lib/supplejack/user_set.rb', line 56 def api_attributes api_attributes = {} [:name, :description, :privacy, :priority, :tag_list, :homepage, :approved].each do |attr| api_attributes[attr] = self.send(attr) end api_attributes[:records] = self.api_records api_attributes end |
#api_records ⇒ Array
Return a array of hashes with the format: 123, position: 1 Each hash represents a Supplejack::Item within the UserSet
This array is used to send the UserSet items information to the API.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/supplejack/user_set.rb', line 72 def api_records records = self.records.is_a?(Array) ? self.records : [] records.map do |record_hash| record_hash = record_hash.try(:symbolize_keys) || {} if record_hash[:record_id] {record_id: record_hash[:record_id], position: record_hash[:position] } else nil end end.compact end |
#attributes ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/supplejack/user_set.rb', line 42 def attributes attributes = {} ATTRIBUTES.each do |attribute| value = self.send(attribute) attributes[attribute] = value if value.present? end attributes end |
#attributes=(attributes) ⇒ Object
Assigns the provided attributes to the UserSet object
180 181 182 183 184 185 186 |
# File 'lib/supplejack/user_set.rb', line 180 def attributes=(attributes) attributes = attributes.try(:symbolize_keys) || {} attributes.each do |attr, value| self.send("#{attr}=", value) if ATTRIBUTES.include?(attr) end self.records = ordered_records_from_array(attributes[:ordered_records]) if attributes[:ordered_records] end |
#destroy ⇒ true, false
Executes a DELETE request to the API with the UserSet ID and the user’s api_key
228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/supplejack/user_set.rb', line 228 def destroy if self.new_record? return false else begin self.class.delete("/sets/#{self.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 end |
#favourite? ⇒ Boolean
112 113 114 |
# File 'lib/supplejack/user_set.rb', line 112 def favourite? self.name == "Favourites" end |
#has_record?(record_id) ⇒ true, false
Convinience method to find out if a particular record_id is part of the UserSet
134 135 136 |
# File 'lib/supplejack/user_set.rb', line 134 def has_record?(record_id) !!self.items.detect {|i| i.record_id == record_id.to_i } end |
#hidden? ⇒ Boolean
124 125 126 |
# File 'lib/supplejack/user_set.rb', line 124 def hidden? self.privacy == "hidden" end |
#items ⇒ ItemRelation
Initializes a ItemRelation class which adds behaviour to build, create and find items related to this particular UserSet instance
89 90 91 |
# File 'lib/supplejack/user_set.rb', line 89 def items @items ||= ItemRelation.new(self) end |
#new_record? ⇒ true, false
Returns if the UserSet is persisted to the API or not.
220 221 222 |
# File 'lib/supplejack/user_set.rb', line 220 def new_record? self.id.blank? end |
#ordered_records_from_array(record_ids) ⇒ Array
Takes an ordered Array of record_ids and converts it into a Array of Hashes which the API expects, inferring the position value from the position of the record_id in the Array.
208 209 210 211 212 213 214 |
# File 'lib/supplejack/user_set.rb', line 208 def ordered_records_from_array(record_ids) records = [] record_ids.each_with_index do |id, index| records << {record_id: id, position: index+1} end records end |
#owned_by?(user) ⇒ true, false
Compares the api_key of the user and the api_key assigned to the UserSet to find out if the user passed is the owner of the set.
279 280 281 282 |
# File 'lib/supplejack/user_set.rb', line 279 def owned_by?(user) return false if user.try(:api_key).blank? || self.api_key.blank? user.try(:api_key) == self.api_key end |
#persisted? ⇒ Boolean
243 244 245 |
# File 'lib/supplejack/user_set.rb', line 243 def persisted? !new_record? end |
#priority ⇒ Object
Returns the UserSet priority which defaults to 1
95 96 97 |
# File 'lib/supplejack/user_set.rb', line 95 def priority @priority || 1 end |
#private? ⇒ Boolean
116 117 118 |
# File 'lib/supplejack/user_set.rb', line 116 def private? self.privacy == "private" end |
#public? ⇒ Boolean
120 121 122 |
# File 'lib/supplejack/user_set.rb', line 120 def public? self.privacy == "public" end |
#reload ⇒ Object
Fetches the UserSet information from the API again, in case it had changed. This can be useful if they items for a UserSet changed and you want the relation to have the most up to date items.
251 252 253 254 255 256 257 258 |
# File 'lib/supplejack/user_set.rb', line 251 def reload begin self.instance_variable_set("@items", nil) self.attributes = self.class.get("/sets/#{self.id}")["set"] rescue RestClient::ResourceNotFound => e raise Supplejack::SetNotFound, "UserSet with ID #{id} was not found" end end |
#save ⇒ true, false
Executes a POST request when the UserSet hasn’t been persisted to the API, otherwise it executes a PUT request.
When the API returns a error response, the errors are available through the UserSet#errors virtual attribute.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/supplejack/user_set.rb', line 153 def save begin if self.new_record? response = self.class.post("/sets", {api_key: self.api_key}, {set: self.api_attributes}) self.id = response["set"]["id"] else self.class.put("/sets/#{self.id}", {api_key: self.api_key}, {set: self.api_attributes}) end 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 |
#set_record_id ⇒ Intger?
Returns the record id of the associated record in a safe manner
141 142 143 |
# File 'lib/supplejack/user_set.rb', line 141 def set_record_id self.record['record_id'] if self.record end |
#tag_list ⇒ Object
Returns a comma separated list of tags for this UserSet
107 108 109 110 |
# File 'lib/supplejack/user_set.rb', line 107 def tag_list return @tag_list if @tag_list self..join(', ') if self. end |
#update_attributes(attributes = {}) ⇒ true, false
Updates the UserSet attributes and persists it to the API
173 174 175 176 |
# File 'lib/supplejack/user_set.rb', line 173 def update_attributes(attributes={}) self.attributes = attributes self.save end |
#viewable_by?(user) ⇒ true, false
A UserSet is viewable by anyone if it’s public or hidden When the UserSet is private it’s only viewable by the owner.
267 268 269 270 |
# File 'lib/supplejack/user_set.rb', line 267 def viewable_by?(user) return true if self.public? || self.hidden? self.owned_by?(user) end |