Class: Particle::Token

Inherits:
Model
  • Object
show all
Defined in:
lib/particle/token.rb

Overview

Domain model for one Particle token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

attribute_reader, #attributes

Constructor Details

#initialize(client, attributes) ⇒ Token

Returns a new instance of Token.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/particle/token.rb', line 5

def initialize(client, attributes)
  @client = client
  @attributes =
    if attributes.is_a? String
      { token: attributes }
    else
      # Consider attributes loaded when passed in through constructor
      @loaded = true
      attributes
    end
end

Class Method Details

.create_pathObject



68
69
70
# File 'lib/particle/token.rb', line 68

def self.create_path
  "/oauth/token"
end

.list_pathObject



64
65
66
# File 'lib/particle/token.rb', line 64

def self.list_path
  "v1/access_tokens"
end

Instance Method Details

#create(username, password) ⇒ Object

Create 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



51
52
53
# File 'lib/particle/token.rb', line 51

def create(username, password)
  @client.create_token(username, password)
end

#get_attributesObject

Tokens can’t be loaded. What you see is what you get…



27
28
29
30
# File 'lib/particle/token.rb', line 27

def get_attributes
  @loaded = true
  @attributes
end

#inspectString

Text representation of the token, masking the secret part

Returns:

  • (String)


35
36
37
38
39
40
41
42
43
44
# File 'lib/particle/token.rb', line 35

def inspect
  inspected = super

  # Only show last 4 of token, secret
  if id
    inspected = inspected.gsub! id, "#{'*'*36}#{id[36..-1]}"
  end

  inspected
end

#pathObject



72
73
74
# File 'lib/particle/token.rb', line 72

def path
  "/v1/access_tokens/#{access_token}"
end

#remove(username, password) ⇒ Object

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



60
61
62
# File 'lib/particle/token.rb', line 60

def remove(username, password)
  @client.remove_token(username, password, self)
end

#tokenObject Also known as: id, access_token

The id of the token



18
19
20
# File 'lib/particle/token.rb', line 18

def token
  @attributes[:token]
end