module Robinhood::ApiModule
The API module check for methods that require login.
The API module instance methods
Attributes
errors[RW]
Public Class Methods
before(*names)
click to toggle source
Your code goes hereā¦
# File lib/robinhood/api.rb, line 7 def self.before(*names) names.each do |name| m = instance_method(name) define_method(name) do |*args, &block| if token_compliant?(name) m.bind(self).call(*args, &block) else puts 'You have to run the login(<username>, <password>) method ' \ "or a Robinhood.new instance before running the #{name} method" exit 1 end end end end
new()
click to toggle source
# File lib/robinhood/api.rb, line 35 def initialize; end
Public Instance Methods
instruments(symbol)
click to toggle source
# File lib/robinhood/api.rb, line 54 def instruments(symbol) if symbol.include?('-') raw_response = HTTParty.get( "#{endpoints[:instruments]}#{symbol}/", headers: headers ) else raw_response = HTTParty.get( endpoints[:instruments], query: { 'query' => symbol.upcase }, headers: headers ) end JSON.parse(raw_response.body) end
login(username, password)
click to toggle source
# File lib/robinhood/api.rb, line 37 def login(username, password) raw_response = HTTParty.post( endpoints[:login], body: { 'password' => password, 'username' => username }, headers: headers ) response = JSON.parse(raw_response.body) if response['token'] response = response['token'] @headers['Authorization'] = "Token #{response}" end response end
positions(account_number, instrument_id = nil)
click to toggle source
# File lib/robinhood/api.rb, line 78 def positions(account_number, instrument_id = nil) url = "https://api.robinhood.com/accounts/#{account_number}/positions" url = "#{url}/#{instrument_id}/" if instrument_id raw_response = HTTParty.get(url, headers: headers) JSON.parse(raw_response.body) end
quote(symbol)
click to toggle source
# File lib/robinhood/api.rb, line 70 def quote(symbol) raw_response = HTTParty.get( "https://api.robinhood.com/quotes/#{symbol}/", headers: headers ) JSON.parse(raw_response.body) end
Private Instance Methods
endpoints()
click to toggle source
# File lib/robinhood/api.rb, line 87 def endpoints { login: 'https://api.robinhood.com/api-token-auth/', investment_profile: 'https://api.robinhood.com/user/investment_profile/', accounts: 'https://api.robinhood.com/accounts/', ach_iav_auth: 'https://api.robinhood.com/ach/iav/auth/', ach_relationships: 'https://api.robinhood.com/ach/relationships/', ach_transfers: 'https://api.robinhood.com/ach/transfers/', applications: 'https://api.robinhood.com/applications/', dividends: 'https://api.robinhood.com/dividends/', edocuments: 'https://api.robinhood.com/documents/', instruments: 'https://api.robinhood.com/instruments/', margin_upgrade: 'https://api.robinhood.com/margin/upgrades/', markets: 'https://api.robinhood.com/markets/', notifications: 'https://api.robinhood.com/notifications/', orders: 'https://api.robinhood.com/orders/', password_reset: 'https://api.robinhood.com/password_reset/request/', quotes: 'https://api.robinhood.com/quotes/', document_requests: 'https://api.robinhood.com/upload/document_requests/', user: 'https://api.robinhood.com/user/', watchlists: 'https://api.robinhood.com/watchlists/' } end
headers()
click to toggle source
# File lib/robinhood/api.rb, line 111 def headers @headers ||= { 'Accept' => 'application/json' } end
token_compliant?(method)
click to toggle source
# File lib/robinhood/api.rb, line 117 def token_compliant?(method) if methodlist.include?(method) if headers.key?('Authorization') true else false end else true end end