Class: Robinhood::REST::Client

Inherits:
API
  • Object
show all
Defined in:
lib/robinhood-ruby/rest/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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)
  @options = args.last.is_a?(Hash) ? args.pop : {}

  @options[:username] = args[0] || Robinhood.username
  @options[:password] = args[1] || Robinhood.password
  @options[:username] = (args.size > 2 && args[2].is_a?(String) ? args[2] : args[0]) || Robinhood.username

  if @options[:username].nil? || @options[:password].nil?
    raise ArgumentError, "Account username and password are required"
  end
  
  setup_headers
  configuration
  
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 .respond_to?(method_name)
    .send(method_name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



4
5
6
# File 'lib/robinhood-ruby/rest/client.rb', line 4

def headers
  @headers
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/robinhood-ruby/rest/client.rb', line 4

def options
  @options
end

#privateObject

Returns the value of attribute private.



4
5
6
# File 'lib/robinhood-ruby/rest/client.rb', line 4

def private
  @private
end

#tokenObject

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

#configurationObject



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

#inspectObject

:nodoc:



22
23
24
# File 'lib/robinhood-ruby/rest/client.rb', line 22

def inspect # :nodoc:
  "<Robinhood::REST::Client @username=#{@options[:username]}>"
end

#loginObject



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 
  @private[:username] = @options[:username]
  @private[:password] = @options[: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] = ["results"][0]["url"]
    end
  end
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (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 .respond_to?(method_name, include_private)
    true
  else
    super
  end
end

#setup_headersObject



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