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, &block) ⇒ HashFormat

Returns a new instance of HashFormat


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

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

Instance Method Details

#keysObject


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

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

#outputObject


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

def output
  data = []
  @data.each do |datum|
    row_values = {}
    datum.each_with_index do |item, i|
      row_values[keys[i]] = item
    end
    data << row_values
  end
  data
end