class Stock
Represents a singular stock on Yahoo! Finance.
@author Marcello Sabino <marchsabino@gmail.com> @version 1.0.1 @since 1.0.0
Attributes
symbol[R]
@return [String] The Stock's symbol
Public Class Methods
new(symbol)
click to toggle source
Creates a new Stock
object with the given symbol
@param symbol [String] the stocks symbol
# File lib/stock.rb, line 18 def initialize(symbol) @symbol = symbol end
Public Instance Methods
d()
click to toggle source
Gets the stock's data from Yahoo!
@since 1.0.0 @raise RuntimeError if the Stock
symbol can't be found on Yahoo! Finance. @return JSON of the Symbol's data from Yahoo! Finance.
# 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
Private Instance Methods
between(str, start, ends)
click to toggle source
A private helper method to grab text in-between 2 strings.
@param str [String] string to search from @param start [String] where to start looking in-between @param ends [String] the end of the string to look in-between
# File lib/stock.rb, line 47 def between(str, start, ends) str[/#{Regexp.escape(start)}(.*?)#{Regexp.escape(ends)}/m, 1] end