Class: FlowcommerceSpree::ImportExperienceItems

Inherits:
Object
  • Object
show all
Defined in:
app/services/flowcommerce_spree/import_experience_items.rb

Overview

A service object to import the data for product variants belonging to a flow.io Experience

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(zone, client: FlowcommerceSpree.client, organization: ORGANIZATION) ⇒ Object



6
7
8
# File 'app/services/flowcommerce_spree/import_experience_items.rb', line 6

def self.run(zone, client: FlowcommerceSpree.client, organization: ORGANIZATION)
  new(zone, client: client, organization: organization).run
end

Instance Method Details

#runObject



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
# File 'app/services/flowcommerce_spree/import_experience_items.rb', line 10

def run
  page_size  = 100
  offset     = 0
  items      = []
  total = 0

  while offset == 0 || items.length != 0
    # show current list size
    @logger.info "\nGetting items: #{@experience_key.green}, rows #{offset} - #{offset + page_size}"

    begin
      items = @client.experiences
                     .get_items(@organization, experience: @experience_key, limit: page_size, offset: offset)
    rescue Io::Flow::V0::HttpClient::PreconditionException => e
      @logger.info "flow.io API error: #{e.message}"
      break
    end

    offset += page_size
    log_str = +''

    items.each do |item|
      total += 1
      item_hash = item.to_hash
      next unless (variant = Spree::Variant.find_by(sku: item_hash.delete(:number)))

      variant.flow_import_item(item_hash, experience_key: @experience_key)

      log_str << "#{variant.sku}, "
    end
    @logger.info log_str
  end

  @logger.info "\nData for #{total.to_s.green} products was imported."
end