Module: Robinhood::ApiModule

Includes:
HTTParty
Included in:
Api
Defined in:
lib/robinhood/api.rb,
lib/robinhood/api.rb

Overview

The API module instance methods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



33
34
35
# File 'lib/robinhood/api.rb', line 33

def errors
  @errors
end

Class Method Details

.before(*names) ⇒ Object

Your code goes here…



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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

Instance Method Details

#initializeObject



35
# File 'lib/robinhood/api.rb', line 35

def initialize; end

#instruments(symbol) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/robinhood/api.rb', line 37

def (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) ⇒ Object



78
79
80
81
82
83
# File 'lib/robinhood/api.rb', line 78

def positions(, 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) ⇒ Object



70
71
72
73
74
75
76
# 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