Class: YahooStock::Result::HashFormat

Inherits:
YahooStock::Result show all
Defined in:
lib/yahoo_stock/result/hash_format.rb

Overview

DESCRIPTION:

Convert results as an array of key value pairs

USAGE

YahooStock::Result::HashFormat.new(“data as string”)‘of’, ‘keys’].output The number of keys in the block array must be equal to the values expected to be returned

Mostly will be used as a separate strategy for formatting results

Instance Method Summary collapse

Methods inherited from YahooStock::Result

#store

Constructor Details

#initialize(data, stock_symbols, &block) ⇒ HashFormat

Returns a new instance of HashFormat.



12
13
14
15
16
17
# File 'lib/yahoo_stock/result/hash_format.rb', line 12

def initialize(data, stock_symbols, &block)
  @data = YahooStock::Result::ArrayFormat.new(data).output
  @stock_symbols = stock_symbols
  @keys = yield
  super(self)
end

Instance Method Details

#keysObject



31
32
33
# File 'lib/yahoo_stock/result/hash_format.rb', line 31

def keys
  @keys.collect{|key| key.to_s.gsub(/\s/,'_').downcase.to_sym}
end

#outputObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/yahoo_stock/result/hash_format.rb', line 19

def output
  data = Hash.new 
  @data.each_with_index do |datum, index|
    row_values = {}
    datum.each_with_index do |item, i|
      row_values[keys[i]] = item
    end
    data[@stock_symbols[index]] = row_values
  end
  data
end