Class: GunBroker::User

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

Instance Method Summary collapse

Constructor Details

#initialize(username, auth_options = {}) ⇒ User

Returns a new instance of User.

Parameters:

  • username (String)
  • auth_options (Hash) (defaults to: {})

    Requires either a :password or :token.

Options Hash (auth_options):

  • :password (String)

    The User's GunBroker.com password.

  • :token (String)

    An existing access token previously obtained by calling #authenticate! with a username/password.



23
24
25
26
27
# File 'lib/gun_broker/user.rb', line 23

def initialize(username, auth_options = {})
  @username = username
  @password = auth_options[:password] || auth_options['password']
  @token    = auth_options[:token]    || auth_options['token']
end

Instance Attribute Details

#tokenString (readonly)

Returns The User's GunBroker access token obtained by calling #authenticate! or nil if not authenticated.

Returns:

  • (String)

    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

#usernameString (readonly)

Returns The User's GunBroker.com username.

Returns:

  • (String)

    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

Note:

POST /Users/AccessToken

Authenticates with the GunBroker API server and saves the returned access #token.

Returns:

  • (String)

    The access #token used in subsequent requests.

Raises:



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.

Returns:

  • (Boolean)

    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.

Parameters:

  • buyer_id (Integer, String)

    GunBroker user ID.

Returns:

  • (Hash)

    From the JSON response.

Raises:



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_infoHash

Note:

GET /Users/ContactInfo

Returns the User's contact information.

Returns:

  • (Hash)

    From the JSON response.

Raises:



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!

Note:

DELETE /Users/AccessToken

Sends a DELETE request to deactivate the current access #token.

Returns:

  • (true)

    Explicitly returns true unless an exception is raised.

Raises:



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

#idString

Returns the GunBroker.com user ID. Uses #contact_info to get the user details (therefore, the User must be authenticated).

Returns:

  • (String)

    The User's GunBroker.com user ID.

Raises:



33
34
35
# File 'lib/gun_broker/user.rb', line 33

def id
  contact_info['userID']
end

#itemsItemsDelegate

Used to scope Item actions by GunBroker::User. See the ItemsDelegate docs.

Returns:



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(options = {})
  @items_as_pages_delegate ||= ItemsAsPagesDelegate.new(self, options)
end

#ordersOrdersDelegate

Used to scope Order actions by GunBroker::User. See the OrdersDelegate docs.

Returns:



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(options = {})
  @orders_as_pages_delegate ||= OrdersAsPagesDelegate.new(self, options)
end