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

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

Overview

Service object: base attribute.

Constant Summary collapse

RUBY_UNIT_TO_AMAZON_UNIT =

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.



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

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.



21
22
23
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 21

def amazon_schema
  @amazon_schema
end

#attribute_nameObject (readonly)

Returns the value of attribute attribute_name.



21
22
23
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 21

def attribute_name
  @attribute_name
end

#catalog_itemObject (readonly)

Returns the value of attribute catalog_item.



21
22
23
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 21

def catalog_item
  @catalog_item
end

#enum_mapperObject (readonly)

Returns the value of attribute enum_mapper.



21
22
23
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 21

def enum_mapper
  @enum_mapper
end

#fbaObject (readonly)

Returns the value of attribute fba.



21
22
23
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 21

def fba
  @fba
end

#itemObject (readonly)

Returns the value of attribute item.



21
22
23
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 21

def item
  @item
end

#language_tagObject (readonly)

Returns the value of attribute language_tag.



21
22
23
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 21

def language_tag
  @language_tag
end

#marketplaceObject (readonly)

Returns the value of attribute marketplace.



21
22
23
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 21

def marketplace
  @marketplace
end

#marketplace_country_isoObject (readonly)

Returns the value of attribute marketplace_country_iso.



21
22
23
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 21

def marketplace_country_iso
  @marketplace_country_iso
end

#marketplace_idObject (readonly)

Returns the value of attribute marketplace_id.



21
22
23
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 21

def marketplace_id
  @marketplace_id
end

#value_attribute_nameObject (readonly)

Returns the value of attribute value_attribute_name.



21
22
23
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 21

def value_attribute_name
  @value_attribute_name
end

#variationObject (readonly)

Returns the value of attribute variation.



21
22
23
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 21

def variation
  @variation
end

Instance Method Details

#buildObject

Raises:

  • (NotImplementedError)


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

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

#fetch_unit(attribute) ⇒ Object

Proxy accessory method for spec based units



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

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



56
57
58
59
60
61
62
63
64
65
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 56

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



74
75
76
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 74

def map_unit(unit)
  RUBY_UNIT_TO_AMAZON_UNIT[unit]
end

#valueObject



49
50
51
52
53
# File 'app/services/edi/amazon/json_listing_generator/attributes/base_attribute.rb', line 49

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