Class: Commercelayer::CLI::Exporters::Contentful

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/commercelayer/cli/exporters/contentful.rb

Instance Method Summary collapse

Methods included from Helpers

#commercelayer_client, #config_data, #config_data_template, #config_path

Instance Method Details

#export!Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/commercelayer/cli/exporters/contentful.rb', line 8

def export!
  last_product_reference = nil
  last_product_id = nil
  last_product_variants = []
  Commercelayer::Sku.order(:reference).all.each_total do |sku|
    puts "> #{sku.code}"
    if sku.reference != last_product_reference
      if last_product_id
        product = master.entries.find(last_product_id)
        product.update({
          reference: last_product_reference,
          variants: last_product_variants
        })
        product.publish
        last_product_variants = []
      end

      begin
        product = product_model.entries.create({
          reference: sku.reference,
          variants: []
        })
        last_product_reference = sku.reference
        last_product_id = product.id
      rescue => e
        puts e.inspect
        break
      end
    end

    begin
      variant = variant_model.entries.create({
        code: sku.code,
        name: sku.name,
        description: sku.description,
        image: image(sku)
      })
      variant.publish
      last_product_variants << variant
    rescue => e
      puts e.inspect
      break
    end
  end
end