Class: GunBroker::User

Inherits:
Object
  • Object
show all
Defined in:
lib/gun_broker/user.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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



7
8
9
10
11
# File 'lib/gun_broker/user.rb', line 7

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

#tokenObject (readonly)

Returns the value of attribute token



5
6
7
# File 'lib/gun_broker/user.rb', line 5

def token
  @token
end

#usernameObject (readonly)

Returns the value of attribute username



4
5
6
# File 'lib/gun_broker/user.rb', line 4

def username
  @username
end

Instance Method Details

#authenticate!Object



13
14
15
16
# File 'lib/gun_broker/user.rb', line 13

def authenticate!
  response = GunBroker::API.post('/Users/AccessToken', { username: @username, password: @password })
  @token = response['accessToken']
end

#deauthenticate!Object

Sends a DELETE request to deactivate the current access token.



19
20
21
22
23
# File 'lib/gun_broker/user.rb', line 19

def deauthenticate!
  GunBroker::API.delete('/Users/AccessToken', {}, { 'X-AccessToken' => @token })
  @token = nil
  true  # Explicit `true` so this method won't return the `nil` set above.
end

#itemsObject



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

def items
  response = GunBroker::API.get('/Items', { 'SellerName' => @username }, { 'X-AccessToken' => @token })
  response['results'].map { |result| GunBroker::Item.new(result) }
end

#sold_itemsObject



35
36
37
38
# File 'lib/gun_broker/user.rb', line 35

def sold_items
  response = GunBroker::API.get('/ItemsSold', {}, { 'X-AccessToken' => @token })
  response['results'].map { |result| GunBroker::Item.new(result) }
end

#unsold_itemsObject



30
31
32
33
# File 'lib/gun_broker/user.rb', line 30

def unsold_items
  response = GunBroker::API.get('/ItemsUnsold', {}, { 'X-AccessToken' => @token })
  response['results'].map { |result| GunBroker::Item.new(result) }
end