Class: Edi::Amazon::ListingMessageProcessor
- Inherits:
-
BaseEdiService
- Object
- BaseService
- BaseEdiService
- Edi::Amazon::ListingMessageProcessor
- Defined in:
- app/services/edi/amazon/listing_message_processor.rb
Overview
Service object: listing message processor.
Defined Under Namespace
Classes: Result
Constant Summary
Constants included from Edi::AddressAbbreviator
Edi::AddressAbbreviator::MAX_LENGTH
Instance Attribute Summary
Attributes inherited from BaseEdiService
Attributes inherited from BaseService
Instance Method Summary collapse
- #build_json(catalog_item_or_variation, http_method: 'PATCH', attribute_actions: nil, prefetch_media: false, use_fba_sku: false) ⇒ Object
-
#collect_media_locations(hash) ⇒ Object
Provided an attribute hash will find all the media locations.
- #get_amazon_json_attributes(catalog_item_or_variation, fba: false) ⇒ Object
- #get_amazon_json_patches(catalog_item_or_variation, attribute_actions: nil, fba: false) ⇒ Object
- #process(catalog_item_or_variation, http_method: 'PATCH', attribute_actions: nil, use_fba_sku: false) ⇒ Object
Methods inherited from BaseEdiService
#duplicate_po_already_notified?, #initialize, #mark_duplicate_po_as_notified, #report_order_creation_issues, #safe_process_edi_communication_log
Methods included from Edi::AddressAbbreviator
#abbreviate_street, #collect_street_originals, #record_address_abbreviation_notes
Methods inherited from BaseService
#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger
Constructor Details
This class inherits a constructor from Edi::BaseEdiService
Instance Method Details
#build_json(catalog_item_or_variation, http_method: 'PATCH', attribute_actions: nil, prefetch_media: false, use_fba_sku: false) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'app/services/edi/amazon/listing_message_processor.rb', line 68 def build_json(catalog_item_or_variation, http_method: 'PATCH', attribute_actions: nil, prefetch_media: false, use_fba_sku: false) # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#patchlistingsitem # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#patchoperation sku_to_use = catalog_item_or_variation.reported_vendor_sku(orchestrator.partner) sku_to_use = catalog_item_or_variation.amazon_fba_sku if use_fba_sku listing_hash = case http_method when 'PUT' # we use PUT to create a new listing from attributes on a pending onboard amazon item, or fully update an existing listing from attributes # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#put-listings2021-08-01itemsselleridsku { productType: catalog_item_or_variation.amazon_product_type_in_effect, # PRODUCT-ONLY submission: create/update the product without the offer. # get_amazon_json_attributes omits offer/fulfillment attributes # (BaseGenerator::OFFER_ATTRIBUTES) so we don't send data Amazon would reject # with INVALID_ATTRIBUTE (90000900) on a product-only PUT. The offer + inventory # are submitted separately via the price_advice / inventory_advice messages. requirements: 'LISTING_PRODUCT_ONLY', sku: sku_to_use, attributes: get_amazon_json_attributes(catalog_item_or_variation, fba: use_fba_sku) } else # we use PATCH operations to partially update listing attributes on an onboarded amazon item # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#patchlistingsitem { productType: catalog_item_or_variation.amazon_product_type_in_effect, sku: sku_to_use, patches: get_amazon_json_patches(catalog_item_or_variation, attribute_actions:, fba: use_fba_sku) } end # At this point, it is a very good idea to PING the media urls so that they are warmed up and cached # and ready to serve, the amazon api is very very sensitive and has aggressive timeouts if prefetch_media media_locations = collect_media_locations(listing_hash) media_locations.each do |url| response = HTTP.head(url) logger.info "Prefetching Media URL #{url}: #{response.status}" rescue StandardError => e logger.error "Error while prefetching Media URL #{url}" logger.error e end end listing_hash.to_json end |
#collect_media_locations(hash) ⇒ Object
Provided an attribute hash will find all the media locations
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/services/edi/amazon/listing_message_processor.rb', line 50 def collect_media_locations(hash) media_locations = [] hash.each do |key, value| if key == :media_location media_locations << value elsif value.is_a?(Hash) media_locations.concat(collect_media_locations(value)) elsif value.is_a?(Array) value.each do |element| media_locations.concat(collect_media_locations(element)) if element.is_a?(Hash) end end end media_locations end |
#get_amazon_json_attributes(catalog_item_or_variation, fba: false) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'app/services/edi/amazon/listing_message_processor.rb', line 134 def get_amazon_json_attributes(catalog_item_or_variation, fba: false) # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#putlistingsitem # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#listingsitemputrequest # "attributes": { # "size": [ # { # "value": "Programmable", # "language_tag": "en_US", # "marketplace_id": "ATVPDKIKX0DER" # } # ], # "brand": [ # { # "value": "WarmlyYours", # "language_tag": "en_US", # "marketplace_id": "ATVPDKIKX0DER" # } # ], # "color": [ # { # "value": "White", # "language_tag": "en_US", # "marketplace_id": "ATVPDKIKX0DER" # } # ], # "style": [ # { # "value": "Programmable", # "language_tag": "en_US", # "marketplace_id": "ATVPDKIKX0DER" # } # ],... amazon_product_listing_generator = catalog_item_or_variation.amazon_json_generator(marketplace_id: orchestrator.marketplace, fba:, language_tag: orchestrator.try(:language_tag), business_price_available: orchestrator.try(:business_price_available)) amazon_product_listing_generator.build_attributes end |
#get_amazon_json_patches(catalog_item_or_variation, attribute_actions: nil, fba: false) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/services/edi/amazon/listing_message_processor.rb', line 114 def get_amazon_json_patches(catalog_item_or_variation, attribute_actions: nil, fba: false) # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#listingsitempatchrequest # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#patchoperation # "patches": [ # { # "op": "replace", # "path": "/attributes/fulfillment_availability", # "value": [ # { # "fulfillment_channel_code": "DEFAULT", # "quantity": 10 # } # ] # } # ] amazon_product_listing_generator = catalog_item_or_variation.amazon_json_generator(attribute_actions:, marketplace_id: orchestrator.marketplace, fba:, language_tag: orchestrator.try(:language_tag), business_price_available: orchestrator.try(:business_price_available)) amazon_product_listing_generator.get_patches end |
#process(catalog_item_or_variation, http_method: 'PATCH', attribute_actions: nil, use_fba_sku: false) ⇒ Object
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 |
# File 'app/services/edi/amazon/listing_message_processor.rb', line 21 def process(catalog_item_or_variation, http_method: 'PATCH', attribute_actions: nil, use_fba_sku: false) # see: https://developer-docs.amazon.com/sp-api/docs/listings-items-api-v2021-08-01-reference#patchlistingsitem # see: https://developer-docs.amazon.com/sp-api/docs/building-listings-management-workflows-guide#should-i-submit-in-bulk-using-the-json_listings_feed-or-individually-with-the-listings-items-api # see: https://developer-docs.amazon.com/sp-api/docs/listings-feed-type-values#listings-feed ecl = nil EdiCommunicationLog.transaction do logger.info "Creating listing item message for partner #{orchestrator.partner}" data_json = build_json(catalog_item_or_variation, http_method:, attribute_actions:, prefetch_media: true, use_fba_sku:) ecl = EdiCommunicationLog.create_outbound_file_from_data( data: data_json, data_type: 'json', file_extension: 'cii', partner: orchestrator.partner, category: 'listing_data', resources: catalog_item_or_variation, file_info: {} ) end Result.ok(ecl) rescue StandardError => e catalog_item_id = catalog_item_or_variation.try(:id) || catalog_item_or_variation.try(:catalog_item_id) logger.error "ListingMessageProcessor#process failed for catalog_item #{catalog_item_id}: #{e.class} - #{e.}" logger.error e.backtrace&.first(5)&.join("\n") ErrorReporting.error(e, catalog_item_id: catalog_item_id, http_method: http_method, use_fba_sku: use_fba_sku) Result.fail("#{e.class}: #{e.}") end |