Class: Robinhood::REST::Client
- Defined in:
- lib/robinhood-ruby/rest/client.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#options ⇒ Object
Returns the value of attribute options.
-
#private ⇒ Object
Returns the value of attribute private.
-
#token ⇒ Object
Returns the value of attribute token.
Instance Method Summary collapse
- #configuration ⇒ Object
-
#initialize(*args) ⇒ Client
constructor
A new instance of Client.
-
#inspect ⇒ Object
:nodoc:.
- #login ⇒ Object
-
#method_missing(method_name, *args, &block) ⇒ Object
Delegate account methods from the client.
- #respond_to?(method_name, include_private = false) ⇒ Boolean
- #setup_headers ⇒ Object
Methods inherited from API
#account, #buy, #cancel_order, #dividends, #fundamentals, #historicals, #instruments, #investment_profile, #limit_buy, #limit_sell, #markets, #news, #orders, #positions, #quote_data, #sell, #sp500_down, #sp500_up, #splits, #stop_loss_sell, #user, #watchlists
Constructor Details
#initialize(*args) ⇒ Client
Returns a new instance of Client.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/robinhood-ruby/rest/client.rb', line 6 def initialize(*args) = args.last.is_a?(Hash) ? args.pop : {} [:username] = args[0] || Robinhood.username [:password] = args[1] || Robinhood.password [:username] = (args.size > 2 && args[2].is_a?(String) ? args[2] : args[0]) || Robinhood.username if [:username].nil? || [:password].nil? raise ArgumentError, "Account username and password are required" end setup_headers configuration login end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Delegate account methods from the client. This saves having to call client.account every time for resources on the default account.
30 31 32 33 34 35 36 |
# File 'lib/robinhood-ruby/rest/client.rb', line 30 def method_missing(method_name, *args, &block) if account.respond_to?(method_name) account.send(method_name, *args, &block) else super end end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
4 5 6 |
# File 'lib/robinhood-ruby/rest/client.rb', line 4 def headers @headers end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/robinhood-ruby/rest/client.rb', line 4 def end |
#private ⇒ Object
Returns the value of attribute private.
4 5 6 |
# File 'lib/robinhood-ruby/rest/client.rb', line 4 def private @private end |
#token ⇒ Object
Returns the value of attribute token.
4 5 6 |
# File 'lib/robinhood-ruby/rest/client.rb', line 4 def token @token end |
Instance Method Details
#configuration ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/robinhood-ruby/rest/client.rb', line 46 def configuration() @api_url = "https://api.robinhood.com/" @is_init = false @private = { "session": {}, "account": nil, "username": nil, "password": nil, "headers": nil, "auth_token": nil } @api = {} end |
#inspect ⇒ Object
:nodoc:
22 23 24 |
# File 'lib/robinhood-ruby/rest/client.rb', line 22 def inspect # :nodoc: "<Robinhood::REST::Client @username=#{@options[:username]}>" end |
#login ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/robinhood-ruby/rest/client.rb', line 75 def login @private[:username] = [:username] @private[:password] = [:password] if @private[:auth_token].nil? raw_response = HTTParty.post( @api_url + "api-token-auth/", body: { "password" => @private[:password], "username" => @private[:username] }.as_json, headers: @headers ) response = JSON.parse(raw_response.body) if response["non_field_errors"] puts response["non_field_errors"] false elsif response["token"] @private[:auth_token] = response["token"] @headers["Authorization"] = "Token " + @private[:auth_token].to_s @private[:account] = account["results"][0]["url"] end end end |
#respond_to?(method_name, include_private = false) ⇒ Boolean
38 39 40 41 42 43 44 |
# File 'lib/robinhood-ruby/rest/client.rb', line 38 def respond_to?(method_name, include_private=false) if account.respond_to?(method_name, include_private) true else super end end |
#setup_headers ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/robinhood-ruby/rest/client.rb', line 63 def setup_headers @headers ||= { "Accept" => "*/*", "Accept-Encoding" => "gzip, deflate", "Accept-Language" => "en;q=1, fr;q=0.9, de;q=0.8, ja;q=0.7, nl;q=0.6, it;q=0.5", "Content-Type" => "application/x-www-form-urlencoded; charset=utf-8", "X-Robinhood-API-Version" => "1.0.0", "Connection" => "keep-alive", "User-Agent" => "Robinhood/823 (iPhone; iOS 7.1.2; Scale/2.00)", } end |