Class: Stock
- Inherits:
-
Object
- Object
- Stock
- Defined in:
- lib/stock.rb
Overview
Represents a singular stock on Yahoo! Finance.
Instance Attribute Summary collapse
-
#symbol ⇒ String
readonly
The Stock’s symbol.
Instance Method Summary collapse
-
#d ⇒ Object
Gets the stock’s data from Yahoo!.
-
#initialize(symbol) ⇒ Stock
constructor
Creates a new Stock object with the given symbol.
Constructor Details
#initialize(symbol) ⇒ Stock
Creates a new Stock object with the given symbol
18 19 20 |
# File 'lib/stock.rb', line 18 def initialize(symbol) @symbol = symbol end |
Instance Attribute Details
#symbol ⇒ String (readonly)
Returns The Stock’s symbol.
12 13 14 |
# File 'lib/stock.rb', line 12 def symbol @symbol end |
Instance Method Details
#d ⇒ Object
Gets the stock’s data from Yahoo!
28 29 30 31 32 33 34 35 36 |
# File 'lib/stock.rb', line 28 def d html = open("https://finance.yahoo.com/quote/#{@symbol}", &:read).freeze json = JSON.parse(between(html, 'root.App.main = ', ";\n}(this));")) json = json['context']['dispatcher']['stores'] symbol = json['PageStore']['pageData']['symbol'] return json['StreamDataStore']['quoteData'][symbol].to_json if symbol != nil raise RuntimeError, "Stock #{@symbol} doesn't exist." end |