Class: Uncomtrade::Helpers::Iterator

Inherits:
Object
  • Object
show all
Defined in:
lib/uncomtrade/helpers/iterator.rb

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ Iterator

Returns a new instance of Iterator.



6
7
8
# File 'lib/uncomtrade/helpers/iterator.rb', line 6

def initialize(array)
  @array = array
end

Instance Method Details

#cherry_pick(selectors) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/uncomtrade/helpers/iterator.rb', line 10

def cherry_pick(selectors)
  if !selectors.empty?
    selectors = syms_to_strings(selectors)
    array.map do |data|
      hash = create_hash(data, selectors)
    end
  else
    array
  end
end

#to_csv(csv_path, selectors) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/uncomtrade/helpers/iterator.rb', line 21

def to_csv(csv_path, selectors)
  CSV.open(csv_path, "wb") do |csv|
    picked_array = cherry_pick(selectors)
    csv << picked_array.first.keys
    picked_array.each do |hash|
      csv << hash.values
    end
  end
end