Class: Distributator

Inherits:
CSV
  • Object
show all
Defined in:
lib/distributator.rb

Overview

REMINDER: An empty cell can return nil or ” #

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options = Hash.new) ⇒ Distributator

Returns a new instance of Distributator.



10
11
12
13
14
15
# File 'lib/distributator.rb', line 10

def initialize(data, options = Hash.new)
  options.merge!({ headers: true,
                   header_converters: [self.class.rename_header,
                    *options[:header_converters]] })
  super(data, options)
end

Class Method Details

.rename_headerObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/distributator.rb', line 17

def self.rename_header
  ->(header) do
    case header
    when /\A['"]?estimated_availability_date['"]?\Z/i
      'available_on'
    when /\A['"]?image_(\w+)_(\d+)['"]?\Z/
      # TODO: should this have a default value?
      lookup = { 'reference' => 'url', 'name' => 'title' }
      "images.#{$2}.#{lookup[$1]}"
    when /\A['"]?upc['"]?\Z/i
      'id'
    when /\A['"]?cost['"]?\Z/i
      'cost_price'
    when /\A['"]?product_title['"]?\Z/i
      'name'
    else
      header.gsub(/'|"/, '')
    end
  end
end