Class: YahooFinance

Inherits:
Object
  • Object
show all
Defined in:
lib/yahoofinance-typhoeus.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(concurrent_connections = 20, memoize_requests = false) ⇒ YahooFinance

Create a new YahooFinance object with the specified maximum number of concurrent connections. The default is 20.

If you will be repeating the same query more than once, you can cache the responses from Yahoo by setting memoize_requests to true.



31
32
33
34
# File 'lib/yahoofinance-typhoeus.rb', line 31

def initialize(concurrent_connections=20, memoize_requests=false)
  @hydra = Typhoeus::Hydra.new(:max_concurrency => concurrent_connections)
  @hydra.disable_memoization unless memoize_requests
end

Class Method Details

.quick_query(symbol, start_date, end_date) ⇒ Object

Convenience method for one-off quick queries of a given symbol and date range.

Note that as this is intended for one-off runs, it is not as efficient as queueing them up yourself and running them in parallel.

Returns the raw CSV response data from Yahoo.



78
79
80
81
82
83
84
# File 'lib/yahoofinance-typhoeus.rb', line 78

def self.quick_query(symbol, start_date, end_date)
  yf = self.new
  result = nil
  yf.add_query(symbol, start_date, end_date) {|response| result = response }
  yf.run
  result
end

Instance Method Details

#add_query(symbol, start_date, end_date, &callback) ⇒ Object

Adds a query for the given symbol over the given date range to the queue. The given block is used as a callback that will be called with 1 argument, the response, upon successful completion. Note that no queries will actually execute until #run is called.

The dates can be either Date objects or strings. If they're strings, they will be fed to Date.parse.

Raises a SymbolNotFoundException if querying that symbol produces a 404 error on Yahoo. Raises a YahooFinanceException for any other problems.

Otherwise, your block will be called with the body of the response, the raw data from Yahoo!, as its argument.



59
60
61
62
63
64
65
66
# File 'lib/yahoofinance-typhoeus.rb', line 59

def add_query(symbol, start_date, end_date, &callback)
  start_date = Date.parse(start_date) unless start_date.is_a?(Date)
  end_date = Date.parse(end_date) unless end_date.is_a?(Date)

  @hydra.queue(make_request(symbol, start_date, end_date, callback))

  true
end

#runObject

Run any pending historic data queries in the queue and potentially execute any defined callbacks. Blocking: will not return until the entire queue has run.



39
40
41
# File 'lib/yahoofinance-typhoeus.rb', line 39

def run
  @hydra.run
end