Class: Supplejack::User
- Inherits:
-
Object
- Object
- Supplejack::User
- Extended by:
- Request
- Defined in:
- lib/supplejack/user.rb
Overview
The User
class represents a User on the Supplejack API
A User instance can have a relationship to many UserSet objects, this relationship is managed through the UserSetRelation class which adds ActiveRecord like behaviour to the relationship.
A User object can have the following values:
-
id
-
name
-
username
-
email
-
api_key
-
encrypted_password
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#encrypted_password ⇒ Object
readonly
Returns the value of attribute encrypted_password.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#regenerate_api_key ⇒ Object
Returns the value of attribute regenerate_api_key.
-
#sets_attributes ⇒ Object
readonly
Returns the value of attribute sets_attributes.
-
#use_own_api_key ⇒ Object
Returns the value of attribute use_own_api_key.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Class Method Summary collapse
-
.create(attributes = {}) ⇒ Supplejack::User
Executes a POST request to the API with the user attributes to create a User object.
-
.find(id) ⇒ Supplejack::User
Executes a GET request to the API to find the user with the provided ID.
Instance Method Summary collapse
-
#api_attributes ⇒ Hash
Returns a Hash of attributes which will be sent with the POST request.
-
#destroy ⇒ true, false
Execures a DELETE request to the API to remove the User object.
-
#initialize(attributes = {}) ⇒ User
constructor
A new instance of User.
-
#regenerate_api_key? ⇒ true/false
Returns true/false depending whether it will regenerate it’s API Key.
-
#save ⇒ true, false
Executes a PUT request to the API with the user attributes.
-
#use_own_api_key? ⇒ true/false
Returns true/false depending whether it will use it’s own API Key for managing sets.
Methods included from Request
Constructor Details
#initialize(attributes = {}) ⇒ User
Returns a new instance of User.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/supplejack/user.rb', line 30 def initialize(attributes={}) @attributes = attributes.try(:symbolize_keys) || {} @api_key = @attributes[:api_key] || @attributes[:authentication_token] @id = @attributes[:id] @sets_attributes = @attributes[:sets] @use_own_api_key = @attributes[:use_own_api_key] || false @regenerate_api_key = @attributes[:regenerate_api_key] || false [:name, :username, :email, :encrypted_password].each do |attr| self.instance_variable_set("@#{attr}", @attributes[attr]) end end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
27 28 29 |
# File 'lib/supplejack/user.rb', line 27 def api_key @api_key end |
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
27 28 29 |
# File 'lib/supplejack/user.rb', line 27 def attributes @attributes end |
#email ⇒ Object (readonly)
Returns the value of attribute email.
27 28 29 |
# File 'lib/supplejack/user.rb', line 27 def email @email end |
#encrypted_password ⇒ Object (readonly)
Returns the value of attribute encrypted_password.
27 28 29 |
# File 'lib/supplejack/user.rb', line 27 def encrypted_password @encrypted_password end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
27 28 29 |
# File 'lib/supplejack/user.rb', line 27 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
27 28 29 |
# File 'lib/supplejack/user.rb', line 27 def name @name end |
#regenerate_api_key ⇒ Object
Returns the value of attribute regenerate_api_key.
28 29 30 |
# File 'lib/supplejack/user.rb', line 28 def regenerate_api_key @regenerate_api_key end |
#sets_attributes ⇒ Object (readonly)
Returns the value of attribute sets_attributes.
27 28 29 |
# File 'lib/supplejack/user.rb', line 27 def sets_attributes @sets_attributes end |
#use_own_api_key ⇒ Object
Returns the value of attribute use_own_api_key.
28 29 30 |
# File 'lib/supplejack/user.rb', line 28 def use_own_api_key @use_own_api_key end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
27 28 29 |
# File 'lib/supplejack/user.rb', line 27 def username @username end |
Class Method Details
.create(attributes = {}) ⇒ Supplejack::User
Executes a POST request to the API with the user attributes to create a User object.
127 128 129 130 |
# File 'lib/supplejack/user.rb', line 127 def self.create(attributes={}) response = post("/users", {}, {user: attributes}) new(response["user"]) end |
.find(id) ⇒ Supplejack::User
Executes a GET request to the API to find the user with the provided ID.
118 119 120 121 |
# File 'lib/supplejack/user.rb', line 118 def self.find(id) response = get("/users/#{id}") new(response["user"]) end |
Instance Method Details
#api_attributes ⇒ Hash
Returns a Hash of attributes which will be sent with the POST request.
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/supplejack/user.rb', line 84 def api_attributes attrs = {} [:name, :username, :email, :encrypted_password].each do |attr| value = self.public_send(attr) attrs[attr] = value if value.present? end attrs[:sets] = @sets_attributes if @sets_attributes.present? attrs[:authentication_token] = nil if regenerate_api_key? attrs end |
#destroy ⇒ true, false
Execures a DELETE request to the API to remove the User object.
70 71 72 73 74 75 76 77 78 |
# File 'lib/supplejack/user.rb', line 70 def destroy begin id_or_api_key = self.id || self.api_key self.class.delete("/users/#{id_or_api_key}") return true rescue StandardError => e return false end end |
#regenerate_api_key? ⇒ true/false
Returns true/false depending whether it will regenerate it’s API Key
110 111 112 |
# File 'lib/supplejack/user.rb', line 110 def regenerate_api_key? !!@regenerate_api_key end |
#save ⇒ true, false
Executes a PUT request to the API with the user attributes
56 57 58 59 60 61 62 63 64 |
# File 'lib/supplejack/user.rb', line 56 def save begin updated_user = self.class.put("/users/#{self.api_key}", {}, self.api_attributes) @api_key = updated_user['user']['api_key'] if regenerate_api_key? return true rescue StandardError => e return false end end |
#use_own_api_key? ⇒ true/false
Returns true/false depending whether it will use it’s own API Key for managing sets
102 103 104 |
# File 'lib/supplejack/user.rb', line 102 def use_own_api_key? !!@use_own_api_key end |