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, #dividends, #fundamentals, #historicals, #instruments, #investment_profile, #markets, #news, #orders, #place_buy_order, #place_order, #place_sell_order, #positions, #quote_data, #sp500_down, #sp500_up, #splits, #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
# 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

  configuration
  setup
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.



29
30
31
32
33
34
35
# File 'lib/robinhood-ruby/rest/client.rb', line 29

def method_missing(method_name, *args, &block)
  if .respond_to?(method_name)
    .send(method_name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#api_urlObject

Returns the value of attribute api_url.



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

def api_url
  @api_url
end

#endpointsObject

Returns the value of attribute endpoints.



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

def endpoints
  @endpoints
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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/robinhood-ruby/rest/client.rb', line 45

def configuration()
  @api_url = 'https://api.robinhood.com/';

  @endpoints = {
    "login":                    'api-token-auth/',
    "investment_profile":       'user/investment_profile/',
    "accounts":                 'accounts/',
    "ach_iav_auth":             'ach/iav/auth/',
    "ach_relationships":        'ach/relationships/',
    "ach_transfers":            'ach/transfers/',
    "ach_deposit_schedules":    "ach/deposit_schedules/",
    "applications":             'applications/',
    "dividends":                'dividends/',
    "edocuments":               'documents/',
    "instruments":              'instruments/',
    "margin_upgrade":           'margin/upgrades/',
    "markets":                  'markets/',
    "notifications":            'notifications/',
    "notifications_devices":    "notifications/devices/",
    "orders":                   'orders/',
    "cancel_order":             'orders/', #API expects https://api.robinhood.com/orders/{{orderId}}/cancel/
    "password_reset":           'password_reset/request/',
    "quotes":                   'quotes/',
    "document_requests":        'upload/document_requests/',
    "user":                     'user/',
    
    "user_additional_info":     "user/additional_info/",
    "user_basic_info":          "user/basic_info/",
    "user_employment":          "user/employment/",
    "user_investment_profile":  "user/investment_profile/",

    "watchlists":               'watchlists/',
    "positions":                'positions/',
    "fundamentals":             'fundamentals/',
    "sp500_up":                 'midlands/movers/sp500/?direction=up',
    "sp500_down":               'midlands/movers/sp500/?direction=down',
    "news":                     'midlands/news/'
  }

  @is_init = false
  
  @private = {
    "session":     {},
    "account":     nil,
    "username":    nil,
    "password":    nil,
    "headers":     nil,
    "auth_token":  nil
  }

  @api = {}
end

#http_request(url) ⇒ Object



107
108
109
110
# File 'lib/robinhood-ruby/rest/client.rb', line 107

def http_request(url)
  request = Util::Request.new(@private)
  request.http_request(url)
end

#inspectObject

:nodoc:



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

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

#loginObject



112
113
114
115
116
117
# File 'lib/robinhood-ruby/rest/client.rb', line 112

def 
  url = URI(@api_url + "api-token-auth/")
  response = http_request(url)
  @private[:auth_token] = JSON.parse(response.read_body)["token"]
  @private[:account] = ["results"][0]["url"]
end

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

Returns:

  • (Boolean)


37
38
39
40
41
42
43
# File 'lib/robinhood-ruby/rest/client.rb', line 37

def respond_to?(method_name, include_private=false)
  if .respond_to?(method_name, include_private)
    true
  else
    super
  end
end

#setupObject



98
99
100
101
102
103
104
105
# File 'lib/robinhood-ruby/rest/client.rb', line 98

def setup
  @private[:username] = @options[:username];
  @private[:password] = @options[:password];

  if @private[:auth_token].nil?
    
  end
end