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

Instance Method Details

#create_token(username, password, options = {}) ⇒ Token

Authenticate with Particle and create a new token

Parameters:

  • username (String)

    The username (email) used to log in to the Particle Cloud API

  • password (String)

    The password used to log in to the Particle Cloud API

  • options (Hash) (defaults to: {})

    Optional Particle Cloud API options to create the token. :expires_in => How many seconds should the token last for?

    0 means a token that never expires
    

    :expires_at => Date and time when should the token expire :client => Particle OAuth client to use.

    Defaults to 'particle'
    

    :secret => Corresponding OAuth secret to use.

    Defaults to 'particle'
    

    :grant_type => Type of OAuth authentication flow to use

    Defaults to 'password'
    

Returns:

  • (Token)

    The token object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/particle/client/tokens.rb', line 59

def create_token(username, password, options = {})
  client = options.delete(:client) { 'particle' }
  secret = options.delete(:secret) { 'particle' }
  data = URI.encode_www_form({
    grant_type: 'password',
    username: username,
    password: password
  }.merge(options))
  http_options = {
    headers: { content_type: "application/x-www-form-urlencoded" },
    username: client,
    password: secret
  }
  result = request(:post, Token.create_path, data, http_options)
  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

Parameters:

  • username (String)

    The username (email) used to log in to the Particle Cloud API

  • password (String)

    The password used to log in to the Particle Cloud API

  • options (Hash) (defaults to: {})

    Additional Particle Cloud API options to create the token.

Returns:

  • (Token)

    The token object



87
88
89
90
91
# File 'lib/particle/client/tokens.rb', line 87

def (username, password, options = {})
  token = create_token(username, password, options)
  self.access_token = token
  token
end

#remove_token(username, password, target) ⇒ boolean

Remove a Particle token

Parameters:

  • username (String)

    The username (email) used to log in to the Particle Cloud API

  • password (String)

    The password used to log in to the Particle Cloud API

  • target (String, Token)

    An token id or Token object

Returns:

  • (boolean)

    true for success



101
102
103
104
105
106
107
108
# File 'lib/particle/client/tokens.rb', line 101

def remove_token(username, password, target)
  http_options = {
    username: username,
    password: password
  }
  result = request(:delete, token(target).path, "", http_options)
  result[:ok]
end

#token(target = {}) ⇒ Token

Create a domain model for a Particle token

Parameters:

  • target (String, Hash, Token) (defaults to: {})

    A token id, hash of attributes or Token object

Returns:

  • (Token)

    A token object to interact with



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

Parameters:

  • username (String)

    The username (email) used to log in to the Particle Cloud API

  • password (String)

    The password used to log in to the Particle Cloud API

Returns:

  • (Array<Token>)

    List of Particle tokens



31
32
33
34
35
36
37
38
39
# File 'lib/particle/client/tokens.rb', line 31

def tokens(username, password)
  http_options = {
    username: username,
    password: password
  }
  request(:get, Token.list_path, "", http_options).map do |attributes|
    token(attributes)
  end
end