Class: Infusionsoft::Rest::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/infusionsoft/rest/token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token_params) ⇒ Token

Returns a new instance of Token.



11
12
13
14
15
16
17
18
19
# File 'lib/infusionsoft/rest/token.rb', line 11

def initialize(token_params)
  @access_token = token_params[:access_token] || token_params["access_token"]
  @refresh_token = token_params[:refresh_token] || token_params["refresh_token"]
  @expiration = token_params[:expiration] if token_params[:expiration]

  if token_params[:expires_in] || token_params["expires_in"]
    @expiration = Time.now + (token_params[:expires_in] || token_params["expires_in"])
  end
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



9
10
11
# File 'lib/infusionsoft/rest/token.rb', line 9

def access_token
  @access_token
end

#expirationObject

Returns the value of attribute expiration.



9
10
11
# File 'lib/infusionsoft/rest/token.rb', line 9

def expiration
  @expiration
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



9
10
11
# File 'lib/infusionsoft/rest/token.rb', line 9

def refresh_token
  @refresh_token
end

Class Method Details

.auth_url(client_id, redirect_uri) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/infusionsoft/rest/token.rb', line 26

def auth_url(client_id, redirect_uri)
  params = {
    client_id: client_id,
    redirect_uri: redirect_uri,
    scope: 'full',
    response_type: 'code'
  }

  uri = URI::HTTPS.build({host: "signin.infusionsoft.com", path: "/app/oauth/authorize", query: URI.encode_www_form(params)})
  uri.to_s
end

.get_token(client_id, client_secret, redirect_uri, code) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/infusionsoft/rest/token.rb', line 56

def get_token(client_id, client_secret, redirect_uri, code)
  params = {
    client_id: client_id,
    client_secret: client_secret,
    redirect_uri: redirect_uri,
    code: code,
    grant_type: 'authorization_code',
  }

  response = RestClient.post("https://api.infusionsoft.com/token", params)
rescue RestClient::ExceptionWithResponse => e
  # TODO: what to do here
  false
else
  token_params = JSON.parse(response.body)
  self.new(token_params)
end

.refresh(client_id, client_secret, refresh_token) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/infusionsoft/rest/token.rb', line 38

def refresh(client_id, client_secret, refresh_token)
  params = {
    grant_type: 'refresh_token',
    refresh_token: refresh_token,
  }

  header = { "Authorization": "Basic " + Base64.urlsafe_encode64("#{client_id}:#{client_secret}")}

  response = RestClient.post("https://api.infusionsoft.com/token", params, header)

rescue RestClient::ExceptionWithResponse => e
  #TODO what to do here?
  false
else
  token_params = JSON.parse(response.body)
  self.new(token_params)
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/infusionsoft/rest/token.rb', line 21

def valid?
  Time.now < expiration
end