class QuestradeClient::Client

Attributes

endpoint[R]
token[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/questrade_client/client.rb, line 39
def initialize(options = {})
  @endpoint = options[:endpoint].to_s
  @token = options[:token].to_s
  raise ArgumentError, ":endpoint can't be blank" if @endpoint.empty?
  raise ArgumentError, ":token can't be blank" if @token.empty?
end

Private Instance Methods

connection() click to toggle source

Returns a Faraday::Connection object

@return [Faraday::Connection]

# File lib/questrade_client/client.rb, line 76
def connection
  @connection ||= Faraday.new @endpoint do |f|
    f.request :json
    f.headers[:user_agent] = QuestradeClient::USER_AGENT
    f.headers['Authorization'] = "Bearer #{@token}"

    # f.response :logger
    f.response :mashify
    f.response :json, content_type: /\bjson$/

    f.adapter Faraday.default_adapter
  end
end
get(path, options = {}) click to toggle source
# File lib/questrade_client/client.rb, line 52
def get(path, options = {})
  request(:get, path, options)
end
post(path, data = {}) click to toggle source
# File lib/questrade_client/client.rb, line 56
def post(path, data = {})
  request(:post, path, data)
end
put(path, data = {}) click to toggle source
# File lib/questrade_client/client.rb, line 60
def put(path, data = {})
  request(:put, path, data)
end
request(method, path, data = {}) click to toggle source
# File lib/questrade_client/client.rb, line 64
def request(method, path, data = {})
  res = connection.send(method, "v1/#{path}", data)
  if res.success? && !res.body.nil? && !res.body.empty? && res.body != ' '
    res.body
  else
    res
  end
end