Class: Edi::Amazon::JsonListingGenerator::Attributes::BaseAttribute

Inherits:
Object
  • Object
show all
Defined in:
app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb

Constant Summary collapse

RUBY_UNIT_TO_AMAZON_UNIT =
{
  'ft' => 'feet',
  'in' => 'inches',
  'cm' => 'centimeters',
  'm' => 'meters',
  'mm' => 'millimeters',
  'degF' => 'degrees_fahrenheit',
  'degC' => 'degrees_celsius',
  'kg' => 'kilograms',
  'g' => 'grams',
  'A' => 'amps',
  'W' => 'watts',
  'V' => 'volts'
}.with_indifferent_access.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(catalog_item:, variation: nil, language_tag: nil, enum_mapper: nil, marketplace_id: nil, value_attribute_name: nil, fba: nil) ⇒ BaseAttribute

Returns a new instance of BaseAttribute.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 22

def initialize(catalog_item:, variation: nil, language_tag: nil, enum_mapper: nil, marketplace_id: nil, value_attribute_name: nil, fba: nil)
  @value_attribute_name = value_attribute_name || :value
  @attribute_name = self.class.name.demodulize.tableize.singularize
  @catalog_item = catalog_item
  @variation = variation
  # A variation's item will simply be the first item in the variation
  @item = variation&.items&.first || catalog_item.item
  @language_tag = language_tag
  @language_tag ||= begin
    locale = @catalog_item.amazon_locales.first
    locale.to_s.tr('-', '_').presence
  end
  @enum_mapper = enum_mapper
  @marketplace_id = marketplace_id || @catalog_item.catalog.amazon_marketplace.marketplace_identifier # MUST be present
  @marketplace = AmazonMarketplace.find_by(marketplace_identifier: @marketplace_id)
  @marketplace_country_iso = @marketplace.country.iso
  @fba = fba
  @amazon_schema = @catalog_item.amazon_schema
end

Instance Attribute Details

#amazon_schemaObject (readonly)

Returns the value of attribute amazon_schema.



18
19
20
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 18

def amazon_schema
  @amazon_schema
end

#attribute_nameObject (readonly)

Returns the value of attribute attribute_name.



18
19
20
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 18

def attribute_name
  @attribute_name
end

#catalog_itemObject (readonly)

Returns the value of attribute catalog_item.



18
19
20
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 18

def catalog_item
  @catalog_item
end

#enum_mapperObject (readonly)

Returns the value of attribute enum_mapper.



18
19
20
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 18

def enum_mapper
  @enum_mapper
end

#fbaObject (readonly)

Returns the value of attribute fba.



18
19
20
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 18

def fba
  @fba
end

#itemObject (readonly)

Returns the value of attribute item.



18
19
20
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 18

def item
  @item
end

#language_tagObject (readonly)

Returns the value of attribute language_tag.



18
19
20
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 18

def language_tag
  @language_tag
end

#marketplaceObject (readonly)

Returns the value of attribute marketplace.



18
19
20
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 18

def marketplace
  @marketplace
end

#marketplace_country_isoObject (readonly)

Returns the value of attribute marketplace_country_iso.



18
19
20
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 18

def marketplace_country_iso
  @marketplace_country_iso
end

#marketplace_idObject (readonly)

Returns the value of attribute marketplace_id.



18
19
20
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 18

def marketplace_id
  @marketplace_id
end

#value_attribute_nameObject (readonly)

Returns the value of attribute value_attribute_name.



18
19
20
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 18

def value_attribute_name
  @value_attribute_name
end

#variationObject (readonly)

Returns the value of attribute variation.



18
19
20
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 18

def variation
  @variation
end

Instance Method Details

#buildObject

Raises:

  • (NotImplementedError)


42
43
44
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 42

def build
  raise NotImplementedError, "#{self.class.name} needs to implement build"
end

#fetch_unit(attribute) ⇒ Object

Proxy accessory method for spec based units



65
66
67
68
69
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 65

def fetch_unit(attribute)
  u = item.spec_unit(:"amazon_#{attribute}")
  u ||= item.spec_unit(attribute)
  map_unit(u)
end

#fetch_value(attribute, spec_only: false, enum_path: nil, output_unit: nil) ⇒ Object

Proxy accessory method for values



53
54
55
56
57
58
59
60
61
62
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 53

def fetch_value(attribute, spec_only: false, enum_path: nil, output_unit: nil)
  v = item.spec_value(:"amazon_#{attribute}", output_unit:) unless attribute.to_s.start_with?('amazon_')
  v = item.spec_value(attribute, output_unit:) if v.nil?
  unless spec_only
    v = catalog_item.send(attribute) if v.nil? && catalog_item.respond_to?(attribute)
    v = catalog_item.item.send(attribute) if v.nil? && catalog_item.item.respond_to?(attribute)
  end
  # Take a shot to match to an enum if present
  enum_mapper&.enum_value(enum_path || "#{attribute}.value", v) || v
end

#map_unit(unit) ⇒ Object



71
72
73
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 71

def map_unit(unit)
  RUBY_UNIT_TO_AMAZON_UNIT[unit]
end

#valueObject



46
47
48
49
50
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 46

def value
  # By default, we use the class name as symbol and try to fetch value on it
  # enum_path: 'batteries_required.value'
  fetch_value attribute_name, enum_path: "#{attribute_name}.#{value_attribute_name}"
end