class QuestradeApi::REST::Base
@author Bruno Meira <goesmeira@gmail.com>
Constants
- BASE_ENDPOINT
Attributes
account_id[RW]
connection[RW]
data[RW]
endpoint[RW]
id[RW]
raw_body[RW]
Public Class Methods
connection(params = {})
click to toggle source
Builds a new Faraday connection to access endpoint.
@param params [Hash] for connection. @option params [String] :url of endpoint. @option params [String] :access_token to call endpoint.
@return [Faraday] Object with attributes set up to call proper endpoint.
# File lib/questrade_api/rest/base.rb, line 40 def self.connection(params = {}) Faraday.new(params[:url]) do |faraday| # faraday.response :logger faraday.adapter Faraday.default_adapter faraday.headers['Content-Type'] = 'application/json' faraday.headers['User-Agent'] = "QuestradeApi v#{QuestradeApi::VERSION}" faraday.headers['Authorization'] = "Bearer #{params[:access_token]}" end end
new(authorization)
click to toggle source
Initialize an instance of QuestradeApi::REST::Base
@param authorization [Object] to access API. Any object that responds to url and access_token.
# File lib/questrade_api/rest/base.rb, line 19 def initialize(authorization) @authorization = authorization # TODO: Review this later @connection = self.class.connection(url: url, access_token: @authorization.access_token) end
Protected Class Methods
fetch(params = {})
click to toggle source
# File lib/questrade_api/rest/base.rb, line 76 def fetch(params = {}) connection = connection(params) connection.get do |req| req.path = params[:endpoint] params.fetch(:params, []).each do |key, value| req.params[key] = value end end end
post(params = {})
click to toggle source
# File lib/questrade_api/rest/base.rb, line 88 def post(params = {}) connection = connection(params) connection.post do |req| req.path = params[:endpoint] req.body = params[:body] if params[:body] end end
Public Instance Methods
url()
click to toggle source
@return [String]
# File lib/questrade_api/rest/base.rb, line 29 def url authorization.url end
Protected Instance Methods
build_attributes(response)
click to toggle source
# File lib/questrade_api/rest/base.rb, line 57 def build_attributes(response) @raw_body = JSON.parse(response.body) end
build_data(data)
click to toggle source
# File lib/questrade_api/rest/base.rb, line 52 def build_data(data) hash = hash_to_snakecase(data) @data = OpenStruct.new(hash) end
fetch(params = {})
click to toggle source
# File lib/questrade_api/rest/base.rb, line 61 def fetch(params = {}) response = @connection.get do |req| req.path = params[:endpoint] || self.class.endpoint params.fetch(:params, []).each do |key, value| req.params[key] = value end end build_attributes(response) if response.status == 200 response end