Class: WebCrawler::View::Csv

Inherits:
Base
  • Object
show all
Defined in:
lib/web_crawler/view/csv.rb

Instance Attribute Summary

Attributes inherited from Base

#input

Instance Method Summary collapse

Methods inherited from Base

#draw, #render

Constructor Details

#initialize(input, options = { }) ⇒ Csv

Returns a new instance of Csv.



5
6
7
8
9
10
11
12
13
# File 'lib/web_crawler/view/csv.rb', line 5

def initialize(input, options = { })
  in_group_of_num = options.delete(:in_group_of)
  input = input.first.in_groups_of(in_group_of_num) if in_group_of_num && input.size == 1
  headers = options.delete(:headers) || input.select { |i| i.is_a? Hash }.max_by(&:size).keys
rescue NoMethodError
ensure
  input = input.dup.unshift(headers) unless headers.nil?
  super(input, options)
end

Instance Method Details

#format(item) ⇒ Object



15
16
17
18
# File 'lib/web_crawler/view/csv.rb', line 15

def format(item)
  values = item.respond_to?(:values) ? item.values : item.to_a
  values.to_csv(@options[:csv] || {})
end