Class: Stock

Inherits:
Object
  • Object
show all
Defined in:
lib/stock.rb

Overview

Represents a singular stock on Yahoo! Finance.

Author:

Since:

  • 1.0.0

Version:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol) ⇒ Stock

Creates a new Stock object with the given symbol

Parameters:

  • symbol (String)

    the stocks symbol

Since:

  • 1.0.0



18
19
20
# File 'lib/stock.rb', line 18

def initialize(symbol)
  @symbol = symbol
end

Instance Attribute Details

#symbolString (readonly)

Returns The Stock’s symbol.

Returns:

  • (String)

    The Stock’s symbol

Since:

  • 1.0.0



12
13
14
# File 'lib/stock.rb', line 12

def symbol
  @symbol
end

Instance Method Details

#dObject

Gets the stock’s data from Yahoo!

Returns:

  • JSON of the Symbol’s data from Yahoo! Finance.

Raises:

  • RuntimeError if the Stock symbol can’t be found on Yahoo! Finance.

Since:

  • 1.0.0



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