class GunBroker::User

Represents a GunBroker User.

Attributes

token[R]

@return [String] The User's GunBroker access token obtained by calling {#authenticate!} or `nil` if not authenticated.

username[R]

@return [String] The User's GunBroker.com username.

Public Class Methods

new(username, auth_options = {}) click to toggle source

@param username [String] @param auth_options [Hash] Requires either a `:password` or `:token`. @option auth_options [String] :password The User's GunBroker.com password. @option auth_options [String] :token An existing access token previously obtained by calling {#authenticate!} with a username/password.

# 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

Public Instance Methods

authenticate!() click to toggle source

Authenticates with the GunBroker API server and saves the returned access {#token}. @note {API#post! POST} /Users/AccessToken @raise [GunBroker::Error::NotAuthorized] If the username/password is invalid. @raise [GunBroker::Error::RequestError] If there's an issue with the request (usually a `5xx` response). @return [String] The access {#token} used in subsequent requests.

# 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?() click to toggle source

@return [Boolean] `true` if the current credentials are valid.

# 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) click to toggle source

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. @param buyer_id [Integer, String] GunBroker user ID. @raise (see contact_info) @return (see contact_info)

# 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() click to toggle source

Returns the User's contact information. @note {API#get! GET} /Users/ContactInfo @raise [GunBroker::Error::RequestError] If there's an issue with the request (usually a `5xx` response). @return [Hash] From the JSON response.

# File lib/gun_broker/user.rb, line 81
def contact_info
  GunBroker::API.get('/Users/ContactInfo', { 'UserName' => @username }, token_header(@token))
end
deauthenticate!() click to toggle source

Sends a DELETE request to deactivate the current access {#token}. @note {API#delete! DELETE} /Users/AccessToken @raise [GunBroker::Error::RequestError] If there's an issue with the request (usually a `5xx` response). @return [true] Explicitly returns `true` unless an exception is raised.

# 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
Also aliased as: revoke_access_token!
id() click to toggle source

Returns the GunBroker.com user ID. Uses {User#contact_info} to get the user details (therefore, the User must be authenticated). @raise [GunBroker::Error::NotAuthorized] If the User has not yet been authenticated. @raise [GunBroker::Error::RequestError] If there's an issue with the request (usually a `5xx` response). @return [String] The User's GunBroker.com user ID.

# File lib/gun_broker/user.rb, line 33
def id
  contact_info['userID']
end
items() click to toggle source

(see ItemsDelegate) See the {ItemsDelegate} docs. @return [ItemsDelegate]

# File lib/gun_broker/user.rb, line 88
def items
  @items_delegate ||= ItemsDelegate.new(self)
end
items_as_pages(options = {}) click to toggle source

(see ItemsAsPagesDelegate) See the {ItemsAsPagesDelegate} docs. @return [ItemsAsPagesDelegate]

# File lib/gun_broker/user.rb, line 95
def items_as_pages(options = {})
  @items_as_pages_delegate ||= ItemsAsPagesDelegate.new(self, options)
end
orders() click to toggle source

(see OrdersDelegate) See the {OrdersDelegate} docs. @return [OrdersDelegate]

# File lib/gun_broker/user.rb, line 102
def orders
  @orders_delegate ||= OrdersDelegate.new(self)
end
orders_as_pages(options = {}) click to toggle source

(see OrdersAsPagesDelegate) See the {OrdersAsPagesDelegate} docs. @return [OrdersAsPagesDelegate]

# File lib/gun_broker/user.rb, line 109
def orders_as_pages(options = {})
  @orders_as_pages_delegate ||= OrdersAsPagesDelegate.new(self, options)
end
revoke_access_token!()
Alias for: deauthenticate!

Private Instance Methods

has_credentials?() click to toggle source

@return [Boolean] `true` if `@username` is present and either `@password` or `@token` is present.

# File lib/gun_broker/user.rb, line 116
def has_credentials?
  has_username? && (has_password? || has_token?)
end
has_password?() click to toggle source
# File lib/gun_broker/user.rb, line 120
def has_password?
  !@password.nil? && !@password.empty?
end
has_token?() click to toggle source
# File lib/gun_broker/user.rb, line 124
def has_token?
  !@token.nil? && !@token.empty?
end
has_username?() click to toggle source
# File lib/gun_broker/user.rb, line 128
def has_username?
  !@username.nil? && !@username.empty?
end