Class: Edi::AmazonVc::ListingMessageProcessor
- Inherits:
-
BaseEdiService
- Object
- BaseService
- BaseEdiService
- Edi::AmazonVc::ListingMessageProcessor
- Defined in:
- app/services/edi/amazon_vc/listing_message_processor.rb
Constant Summary
Constants included from Edi::AddressAbbreviator
Edi::AddressAbbreviator::MAX_LENGTH
Instance Attribute Summary
Attributes inherited from BaseEdiService
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) ⇒ Object
- #get_amazon_json_patches(catalog_item_or_variation, attribute_actions: nil) ⇒ 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, #options, #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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/services/edi/amazon_vc/listing_message_processor.rb', line 46 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, requirements: 'LISTING_PRODUCT_ONLY', # do offers separately, then use 'LISTING_PRODUCT_ONLY' and 'LISTING_OFFER_ONLY' in separate put messages for offers sku: sku_to_use, attributes: get_amazon_json_attributes(catalog_item_or_variation) } 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:) } 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
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/services/edi/amazon_vc/listing_message_processor.rb', line 28 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) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'app/services/edi/amazon_vc/listing_message_processor.rb', line 106 def get_amazon_json_attributes(catalog_item_or_variation) # 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) amazon_product_listing_generator.build_attributes end |
#get_amazon_json_patches(catalog_item_or_variation, attribute_actions: nil) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/services/edi/amazon_vc/listing_message_processor.rb', line 87 def get_amazon_json_patches(catalog_item_or_variation, attribute_actions: nil) # 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) amazon_product_listing_generator.get_patches end |
#process(catalog_item_or_variation, http_method: 'PATCH', attribute_actions: nil, use_fba_sku: false) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'app/services/edi/amazon_vc/listing_message_processor.rb', line 5 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 ecl end |