Class: GunBroker::User
- Inherits:
-
Object
- Object
- GunBroker::User
- Includes:
- TokenHeader
- Defined in:
- lib/gun_broker/user.rb,
lib/gun_broker/user/items_delegate.rb,
lib/gun_broker/user/orders_delegate.rb,
lib/gun_broker/user/items_as_pages_delegate.rb,
lib/gun_broker/user/orders_as_pages_delegate.rb
Overview
Represents a GunBroker User.
Defined Under Namespace
Classes: ItemsAsPagesDelegate, ItemsDelegate, OrdersAsPagesDelegate, OrdersDelegate
Instance Attribute Summary collapse
-
#token ⇒ String
readonly
The User's GunBroker access token obtained by calling #authenticate! or
nil
if not authenticated. -
#username ⇒ String
readonly
The User's GunBroker.com username.
Instance Method Summary collapse
-
#authenticate! ⇒ String
Authenticates with the GunBroker API server and saves the returned access #token.
-
#authenticated? ⇒ Boolean
true
if the current credentials are valid. -
#buyer_info(buyer_id) ⇒ Hash
Returns contact information for the given
buyer_id
(GunBroker User ID). -
#contact_info ⇒ Hash
Returns the User's contact information.
-
#deauthenticate! ⇒ true
(also: #revoke_access_token!)
Sends a DELETE request to deactivate the current access #token.
-
#id ⇒ String
Returns the GunBroker.com user ID.
-
#initialize(username, auth_options = {}) ⇒ User
constructor
A new instance of User.
- #items ⇒ ItemsDelegate
-
#items_as_pages(options = {}) ⇒ ItemsAsPagesDelegate
Used to scope ItemsAsPage actions by User.
- #orders ⇒ OrdersDelegate
-
#orders_as_pages(options = {}) ⇒ OrdersAsPagesDelegate
Used to scope OrdersAsPage actions by User.
Constructor Details
#initialize(username, auth_options = {}) ⇒ User
Returns a new instance of User.
23 24 25 26 27 |
# File 'lib/gun_broker/user.rb', line 23 def initialize(username, = {}) @username = username @password = [:password] || ['password'] @token = [:token] || ['token'] end |
Instance Attribute Details
#token ⇒ String (readonly)
Returns The User's GunBroker access token obtained by calling #authenticate! or nil
if not authenticated.
17 18 19 |
# File 'lib/gun_broker/user.rb', line 17 def token @token end |
#username ⇒ String (readonly)
Returns The User's GunBroker.com username.
14 15 16 |
# File 'lib/gun_broker/user.rb', line 14 def username @username end |
Instance Method Details
#authenticate! ⇒ String
42 43 44 45 |
# File 'lib/gun_broker/user.rb', line 42 def authenticate! response = GunBroker::API.post('/Users/AccessToken', { username: @username, password: @password }) @token = response['accessToken'] end |
#authenticated? ⇒ Boolean
Returns true
if the current credentials are valid.
48 49 50 51 52 53 54 55 |
# File 'lib/gun_broker/user.rb', line 48 def authenticated? return false unless has_credentials? return !!(authenticate!) if has_password? return !!(contact_info) if has_token? # #contact_info requires a valid token, so use that as a check. false rescue GunBroker::Error::NotAuthorized false end |
#buyer_info(buyer_id) ⇒ Hash
Returns contact information for the given buyer_id
(GunBroker User ID).
The User must be involved in a transaction for the API method to return a response.
73 74 75 |
# File 'lib/gun_broker/user.rb', line 73 def buyer_info(buyer_id) GunBroker::API.get('/Users/ContactInfo', { 'UserID' => buyer_id }, token_header(@token)) end |
#contact_info ⇒ Hash
GET /Users/ContactInfo
Returns the User's contact information.
81 82 83 |
# File 'lib/gun_broker/user.rb', line 81 def contact_info GunBroker::API.get('/Users/ContactInfo', { 'UserName' => @username }, token_header(@token)) end |
#deauthenticate! ⇒ true Also known as: revoke_access_token!
61 62 63 64 65 |
# File 'lib/gun_broker/user.rb', line 61 def deauthenticate! GunBroker::API.delete('/Users/AccessToken', {}, token_header(@token)) @token = nil true # Explicit `true` so this method won't return the `nil` set above. end |
#id ⇒ String
Returns the GunBroker.com user ID. Uses #contact_info to get the user details (therefore, the User must be authenticated).
33 34 35 |
# File 'lib/gun_broker/user.rb', line 33 def id contact_info['userID'] end |
#items ⇒ ItemsDelegate
Used to scope Item actions by GunBroker::User. See the ItemsDelegate docs.
88 89 90 |
# File 'lib/gun_broker/user.rb', line 88 def items @items_delegate ||= ItemsDelegate.new(self) end |
#items_as_pages(options = {}) ⇒ ItemsAsPagesDelegate
Used to scope ItemsAsPage actions by GunBroker::User. See the ItemsAsPagesDelegate docs.
95 96 97 |
# File 'lib/gun_broker/user.rb', line 95 def items_as_pages( = {}) @items_as_pages_delegate ||= ItemsAsPagesDelegate.new(self, ) end |
#orders ⇒ OrdersDelegate
Used to scope Order actions by GunBroker::User. See the OrdersDelegate docs.
102 103 104 |
# File 'lib/gun_broker/user.rb', line 102 def orders @orders_delegate ||= OrdersDelegate.new(self) end |
#orders_as_pages(options = {}) ⇒ OrdersAsPagesDelegate
Used to scope OrdersAsPage actions by GunBroker::User. See the OrdersAsPagesDelegate docs.
109 110 111 |
# File 'lib/gun_broker/user.rb', line 109 def orders_as_pages( = {}) @orders_as_pages_delegate ||= OrdersAsPagesDelegate.new(self, ) end |