Class: Supplejack::User

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Request

delete, get, post, put

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_keyObject (readonly)

Returns the value of attribute api_key.



27
28
29
# File 'lib/supplejack/user.rb', line 27

def api_key
  @api_key
end

#attributesObject (readonly)

Returns the value of attribute attributes.



27
28
29
# File 'lib/supplejack/user.rb', line 27

def attributes
  @attributes
end

#emailObject (readonly)

Returns the value of attribute email.



27
28
29
# File 'lib/supplejack/user.rb', line 27

def email
  @email
end

#encrypted_passwordObject (readonly)

Returns the value of attribute encrypted_password.



27
28
29
# File 'lib/supplejack/user.rb', line 27

def encrypted_password
  @encrypted_password
end

#idObject (readonly)

Returns the value of attribute id.



27
28
29
# File 'lib/supplejack/user.rb', line 27

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



27
28
29
# File 'lib/supplejack/user.rb', line 27

def name
  @name
end

#regenerate_api_keyObject

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_attributesObject (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_keyObject

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

#usernameObject (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.

Returns:

  • (Supplejack::User)

    A Supplejack::User object with recently created id and api_key.



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.

Returns:



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_attributesHash

Returns a Hash of attributes which will be sent with the POST request.

Returns:

  • (Hash)

    A hash of field names and their values



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

#destroytrue, false

Execures a DELETE request to the API to remove the User object.

Returns:

  • (true, false)

    True if the API returned a success response, false if not.



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

Returns:

  • (true/false)

    False by default, true when intentionally set.



110
111
112
# File 'lib/supplejack/user.rb', line 110

def regenerate_api_key?
  !!@regenerate_api_key
end

#savetrue, false

Executes a PUT request to the API with the user attributes

Returns:

  • (true, false)

    True if the API returned a success response, false if not.



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

Returns:

  • (true/false)

    False by default, true when intentionally set at user initialization.



102
103
104
# File 'lib/supplejack/user.rb', line 102

def use_own_api_key?
  !!@use_own_api_key
end