Module: Tastytrade::Authentication

Includes:
Util
Included in:
Client
Defined in:
lib/tastytrade/authentication.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

response_success?

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



8
9
10
# File 'lib/tastytrade/authentication.rb', line 8

def access_token
  @access_token
end

#access_token_expires_atObject (readonly)

Returns the value of attribute access_token_expires_at.



8
9
10
# File 'lib/tastytrade/authentication.rb', line 8

def access_token_expires_at
  @access_token_expires_at
end

#authorization_codeObject (readonly)

Returns the value of attribute authorization_code.



8
9
10
# File 'lib/tastytrade/authentication.rb', line 8

def authorization_code
  @authorization_code
end

#client_idObject (readonly)

Returns the value of attribute client_id.



8
9
10
# File 'lib/tastytrade/authentication.rb', line 8

def client_id
  @client_id
end

#redirect_uriObject (readonly)

Returns the value of attribute redirect_uri.



8
9
10
# File 'lib/tastytrade/authentication.rb', line 8

def redirect_uri
  @redirect_uri
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



8
9
10
# File 'lib/tastytrade/authentication.rb', line 8

def refresh_token
  @refresh_token
end

#secretObject (readonly)

Returns the value of attribute secret.



8
9
10
# File 'lib/tastytrade/authentication.rb', line 8

def secret
  @secret
end

Instance Method Details

#default_headersObject



11
12
13
# File 'lib/tastytrade/authentication.rb', line 11

def default_headers
  { 'Content-Type': 'application/x-www-form-urlencoded' }
end

#refresh_access_tokenObject

Refresh tokens never expire per TastyTrade docs.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tastytrade/authentication.rb', line 45

def refresh_access_token
  options = {
    body: {
      'grant_type' => 'refresh_token',
      'refresh_token' => refresh_token,
      'client_secret' => secret
    },
    headers: default_headers
  }

  response = HTTParty.post(
    "#{Tastytrade::BASE_URL}/oauth/token",
    options
  )

  update_tokens(response)
  response
end

#request_access_token(authorization_grant_code) ⇒ Object

Returns a hash with access_token, refresh_token, expires_in, etc. Used for trusted partner applications that have obtained an authorization code.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tastytrade/authentication.rb', line 17

def request_access_token(authorization_grant_code)
  options = {
    body: {
      'grant_type' => 'authorization_code',
      'code' => authorization_grant_code,
      'client_id' => client_id,
      'client_secret' => secret,
      'redirect_uri' => redirect_uri
    },
    headers: default_headers
  }

  response = HTTParty.post(
    "#{Tastytrade::BASE_URL}/oauth/token",
    options
  )

  unless response_success?(response)
    raise Tastytrade::APIError.new(
      "Unable to retrieve access tokens from API - #{response.code} - #{response.body}"
    )
  end

  update_tokens(response)
  response
end