Class: QuestradeApi::REST::Base
- Inherits:
-
Object
- Object
- QuestradeApi::REST::Base
- Includes:
- Util
- Defined in:
- lib/questrade_api/rest/base.rb
Overview
Direct Known Subclasses
Account, Activity, Balance, Candle, Execution, Market, Option, OptionQuote, Order, Position, Quote, StrategyQuote, Symbol, Time
Constant Summary collapse
- BASE_ENDPOINT =
'/v1'.freeze
Instance Attribute Summary collapse
-
#account_id ⇒ Object
Returns the value of attribute account_id.
-
#authorization ⇒ Object
Returns the value of attribute authorization.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#data ⇒ Object
Returns the value of attribute data.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#id ⇒ Object
Returns the value of attribute id.
-
#raw_body ⇒ Object
Returns the value of attribute raw_body.
Class Method Summary collapse
-
.connection(params = {}) ⇒ Faraday
Builds a new Faraday connection to access endpoint.
- .fetch(params = {}) ⇒ Object
- .post(params = {}) ⇒ Object
Instance Method Summary collapse
- #build_attributes(response) ⇒ Object protected
- #build_data(data) ⇒ Object protected
- #fetch(params = {}) ⇒ Object protected
-
#initialize(authorization) ⇒ Base
constructor
Initialize an instance of QuestradeApi::REST::Base.
- #url ⇒ String
Constructor Details
#initialize(authorization) ⇒ Base
Initialize an instance of QuestradeApi::REST::Base
19 20 21 22 23 24 25 26 |
# File 'lib/questrade_api/rest/base.rb', line 19 def initialize() @authorization = # TODO: Review this later @connection = self.class.connection(url: url, access_token: @authorization.access_token) end |
Instance Attribute Details
#account_id ⇒ Object
Returns the value of attribute account_id.
12 13 14 |
# File 'lib/questrade_api/rest/base.rb', line 12 def account_id @account_id end |
#authorization ⇒ Object
Returns the value of attribute authorization.
12 13 14 |
# File 'lib/questrade_api/rest/base.rb', line 12 def @authorization end |
#connection ⇒ Object
Returns the value of attribute connection.
12 13 14 |
# File 'lib/questrade_api/rest/base.rb', line 12 def connection @connection end |
#data ⇒ Object
Returns the value of attribute data.
12 13 14 |
# File 'lib/questrade_api/rest/base.rb', line 12 def data @data end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
12 13 14 |
# File 'lib/questrade_api/rest/base.rb', line 12 def endpoint @endpoint end |
#id ⇒ Object
Returns the value of attribute id.
12 13 14 |
# File 'lib/questrade_api/rest/base.rb', line 12 def id @id end |
#raw_body ⇒ Object
Returns the value of attribute raw_body.
12 13 14 |
# File 'lib/questrade_api/rest/base.rb', line 12 def raw_body @raw_body end |
Class Method Details
.connection(params = {}) ⇒ Faraday
Builds a new Faraday connection to access endpoint.
40 41 42 43 44 45 46 47 48 |
# 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 |
.fetch(params = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# 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 = {}) ⇒ Object
88 89 90 91 92 93 94 95 |
# 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 |
Instance Method Details
#build_attributes(response) ⇒ Object (protected)
57 58 59 |
# File 'lib/questrade_api/rest/base.rb', line 57 def build_attributes(response) @raw_body = JSON.parse(response.body) end |
#build_data(data) ⇒ Object (protected)
52 53 54 55 |
# 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 = {}) ⇒ Object (protected)
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# 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 |
#url ⇒ String
29 30 31 |
# File 'lib/questrade_api/rest/base.rb', line 29 def url .url end |