Class: ShopifyTransporter::Shopify::Product

Inherits:
Record
  • Object
show all
Extended by:
AttributesHelpers
Defined in:
lib/shopify_transporter/shopify/product.rb

Constant Summary collapse

TOP_LEVEL_ATTRIBUTES =
%w(
  handle title body_html vendor product_type tags template_suffix published_scope published published_at
  option1_name option1_value option2_name option2_value option3_name option3_value variant_sku
  metafields_global_title_tag metafields_global_description_tag
).freeze
VARIANT_PREFIX =
'variant_'
VARIANT_ATTRIBUTES =
%w(
  sku grams inventory_tracker inventory_qty inventory_policy
  fulfillment_service price compare_at_price requires_shipping
  taxable barcode weight weight_unit tax_code
).freeze

Constants inherited from Record

Record::METAFIELD_ATTRIBUTES, Record::METAFIELD_PREFIX

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AttributesHelpers

add_prefix, attributes_present?, delete_empty_attributes, drop_prefix, map_from_key_to_val, normalize_keys, normalize_string, rename_fields, shopify_metafield_hash

Methods inherited from Record

has_values?, #initialize, #metafield_row_values

Constructor Details

This class inherits a constructor from ShopifyTransporter::Shopify::Record

Class Method Details

.columnsObject



41
42
43
44
45
46
# File 'lib/shopify_transporter/shopify/product.rb', line 41

def columns
  @columns ||= header.parse_csv.map do |header_column|
    header_column = 'product_type' if header_column == 'Type'
    normalize_string(header_column)
  end
end

.headerObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/shopify_transporter/shopify/product.rb', line 27

def header
  [
    'Handle', 'Title', 'Body (HTML)', 'Vendor', 'Type', 'Tags', 'Template Suffix', 'Published Scope',
    'Published', 'Published At', 'Option1 Name', 'Option1 Value', 'Option2 Name', 'Option2 Value',
    'Option3 Name', 'Option3 Value', 'Variant SKU', 'Metafields Global Title Tag',
    'Metafields Global Description Tag', 'Metafield Namespace', 'Metafield Key', 'Metafield Value',
    'Metafield Value Type', 'Variant Grams', 'Variant Inventory Tracker', 'Variant Inventory Qty',
    'Variant Inventory Policy', 'Variant Fulfillment Service', 'Variant Price', 'Variant Compare At Price',
    'Variant Requires Shipping', 'Variant Taxable', 'Variant Barcode', 'Image Attachment', 'Image Src',
    'Image Position', 'Image Alt Text', 'Variant Image', 'Variant Weight', 'Variant Weight Unit',
    'Variant Tax Code'
  ].to_csv
end

.keysObject



48
49
50
# File 'lib/shopify_transporter/shopify/product.rb', line 48

def keys
  %w(handle).freeze
end

Instance Method Details

#image_row_valuesObject



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/shopify_transporter/shopify/product.rb', line 97

def image_row_values
  return [] if record_hash['images'].blank?

  record_hash['images'].map do |image_hash|
    image = {
      'image_src' => image_hash['src'],
      'image_position' => image_hash['position'],
      'image_alt_text' => image_hash['alt'],
    }
    row_values_from(image) if self.class.has_values?(image)
  end.compact
end

#to_csvObject



53
54
55
56
57
58
59
60
61
# File 'lib/shopify_transporter/shopify/product.rb', line 53

def to_csv
  CSV.generate do |csv|
    csv << top_level_row_values
    metafield_row_values.each { |row| csv << row }
    variant_row_values.each { |row| csv << row }
    variant_metafield_row_values.each { |row| csv << row }
    image_row_values.each { |row| csv << row }
  end
end

#top_level_row_valuesObject



63
64
65
66
67
68
69
70
71
# File 'lib/shopify_transporter/shopify/product.rb', line 63

def top_level_row_values
  base_hash.merge(record_hash.slice(*TOP_LEVEL_ATTRIBUTES)).tap do |product_hash|
    next if record_hash['options'].blank?

    product_hash['option1_name'] = record_hash['options'][0]['name']
    product_hash['option2_name'] = record_hash['options'][1]['name']
    product_hash['option3_name'] = record_hash['options'][2]['name']
  end.values
end

#variant_metafield_row_valuesObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/shopify_transporter/shopify/product.rb', line 84

def variant_metafield_row_values
  return [] if record_hash['variants'].blank?
  record_hash['variants'].flat_map do |variant_hash|
    next if variant_hash['metafields'].blank?
    variant_hash['metafields'].map do |metafield_hash|
      metafield = metafield_hash.slice(*METAFIELD_ATTRIBUTES)
      metafield.transform_keys! { |k| "#{METAFIELD_PREFIX}#{k}" }
      metafield.merge!(variant_option_hash(variant_hash))
      row_values_from(metafield) if self.class.has_values?(metafield)
    end.compact
  end.compact
end

#variant_option_hash(variant_hash) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/shopify_transporter/shopify/product.rb', line 110

def variant_option_hash(variant_hash)
  {
    'option1_value' => variant_hash['option1'],
    'option2_value' => variant_hash['option2'],
    'option3_value' => variant_hash['option3'],
  }
end

#variant_row_valuesObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/shopify_transporter/shopify/product.rb', line 73

def variant_row_values
  return [] if record_hash['variants'].blank?
  record_hash['variants'].map do |variant_hash|
    variant = variant_hash.slice(*VARIANT_ATTRIBUTES)
    variant.transform_keys! { |k| "#{VARIANT_PREFIX}#{k}" }
    variant.merge!(variant_option_hash(variant_hash))
    variant['variant_image'] = variant_hash['variant_image'] && variant_hash['variant_image']['src']
    row_values_from(variant)
  end
end