class QuestradeApi::Authorization

@author Bruno Meira <goesmeira@gmail.com>

Constants

MODE

Attributes

connection[R]
data[R]
mode[R]

Public Class Methods

new(params, mode = :practice) click to toggle source

Initialize an instance of QuestradeApi::Client.

@note Only access_token, api_server, and refresh_token are needed for this gem.

@param params [Hash] for Questrade authorization. @option params [String] :access_token Access token used to access API. @option params [String] :api_server Endpoint used to access API. Example: 'apiXX.iq.questrade.com/', where X is a number. @option params [Integer] :expires_in How much time the access token is valid. @option params [String] :refresh_token Token used to get a new access token. @option params [String] :token_type Token type.

@param mode [Symbol] accessed on Questrade, `:live` or `:practice`

# File lib/questrade_api/authorization.rb, line 26
def initialize(params, mode = :practice)
  raise 'Mode must be :live or :practice' unless MODE.include?(mode)

  @mode = mode
  @connection = build_connection

  build_data(params)
end

Public Instance Methods

access_token() click to toggle source

Returns the authorized access token.

# File lib/questrade_api/authorization.rb, line 55
def access_token
  data.access_token
end
live?() click to toggle source

Checks if selected mode is live.

@return [Boolean]

# File lib/questrade_api/authorization.rb, line 67
def live?
  mode == :live
end
refresh_token() click to toggle source

Uses refresh_token to fetch a new valid access token.

@note data will be populated accordingly, if call is successful.

@return The result of the call.

# File lib/questrade_api/authorization.rb, line 40
def refresh_token
  response = connection.get do |req|
    req.params[:grant_type] = 'refresh_token'
    req.params[:refresh_token] = data.refresh_token
  end

  if response.status == 200
    raw_body = JSON.parse(response.body)
    build_data(raw_body)
  end

  response
end
url() click to toggle source

Returns the server associated with the authorized access token.

# File lib/questrade_api/authorization.rb, line 60
def url
  data.api_server
end

Private Instance Methods

build_connection() click to toggle source
# File lib/questrade_api/authorization.rb, line 86
def build_connection
  Faraday.new(login_url) do |faraday|
    faraday.response :logger
    faraday.adapter Faraday.default_adapter
    faraday.headers['Content-Type'] = 'application/json'
  end
end
build_data(data) click to toggle source
# File lib/questrade_api/authorization.rb, line 81
def build_data(data)
  hash = hash_to_snakecase(data)
  @data = OpenStruct.new(hash)
end
login_url() click to toggle source
# File lib/questrade_api/authorization.rb, line 73
def login_url
  if live?
    'https://login.questrade.com/oauth2/token'
  else
    'https://practicelogin.questrade.com/oauth2/token'
  end
end