Module: Particle::Client::Tokens
- Included in:
- Particle::Client
- Defined in:
- lib/particle/client/tokens.rb
Overview
Client methods for the Particle authentication/token API
Instance Method Summary collapse
-
#create_token(username, password, options = {}) ⇒ Token
Authenticate with Particle and create a new token.
-
#login(username, password, options = {}) ⇒ Token
Authenticate with Particle and start using the token on the client right away.
-
#remove_token(username, password, target) ⇒ boolean
Remove a Particle token.
-
#token(target = {}) ⇒ Token
Create a domain model for a Particle token.
-
#tokens(username, password) ⇒ Array<Token>
List all Particle tokens for the account.
Instance Method Details
#create_token(username, password, options = {}) ⇒ Token
Authenticate with Particle and create a new token
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/particle/client/tokens.rb', line 53 def create_token(username, password, = {}) data = URI.encode_www_form({ grant_type: 'password', # specified by docs username: username, password: password }.merge()) = { headers: { content_type: "application/x-www-form-urlencoded" }, username: 'particle', # specified by docs password: 'particle' # specified by docs } result = request(:post, Token.create_path, data, ) result[:token] = result.delete(:access_token) token(result) end |
#login(username, password, options = {}) ⇒ Token
Authenticate with Particle and start using the token on the client right away
79 80 81 82 83 |
# File 'lib/particle/client/tokens.rb', line 79 def login(username, password, = {}) token = create_token(username, password, ) self.access_token = token token end |
#remove_token(username, password, target) ⇒ boolean
Remove a Particle token
93 94 95 96 97 98 99 100 |
# File 'lib/particle/client/tokens.rb', line 93 def remove_token(username, password, target) = { username: username, password: password } result = request(:delete, token(target).path, "", ) result[:ok] end |
#token(target = {}) ⇒ Token
Create a domain model for a Particle token
16 17 18 19 20 21 22 |
# File 'lib/particle/client/tokens.rb', line 16 def token(target = {}) if target.is_a? Token target else Token.new(self, target) end end |
#tokens(username, password) ⇒ Array<Token>
List all Particle tokens for the account
31 32 33 34 35 36 37 38 39 |
# File 'lib/particle/client/tokens.rb', line 31 def tokens(username, password) = { username: username, password: password } request(:get, Token.list_path, "", ).map do |attributes| token(attributes) end end |