lib/peddler/inventory.rb in peddler-0.1.3 vs lib/peddler/inventory.rb in peddler-0.2.0
- old
+ new
@@ -2,13 +2,17 @@
module Inventory
module Queue
# Returns number of inventory uploads queued at Amazon
def self.count(transport)
transport.legacize_request
- transport.path << "manual-reports/get-pending-uploads-count"
+ transport.path << 'manual-reports/get-pending-uploads-count'
res = transport.execute_request
- Peddler::Handlers::XMLHandler.decode_response(res).to_i
+ if res =~ /^<PendingUploadsCount>(.*)<\/PendingUploadsCount>$/
+ $1.to_i
+ else
+ nil
+ end
end
end
# This is an inventory batch.
class Batch
@@ -20,77 +24,105 @@
@batch = []
end
# Uploads batch to Amazon.
def upload(params={})
- raise PeddlerError.new("Batch already uploaded") unless @id.nil?
+ raise PeddlerError.new('Batch already uploaded') unless @id.nil?
@transport.legacize_request
- @transport.path << "catalog-upload/"
+ @transport.path << 'catalog-upload/'
+
case params[:method].to_s
- when "modify"
- @transport.path << "modify-only"
+ when 'modify'
+ @transport.path << 'modify-only'
@transport.body = file_content(:short)
- when "purge"
- @transport.path << "purge-replace"
+ when 'purge'
+ @transport.path << 'purge-replace'
@transport.body = file_content
else
- @transport.path << "add-modify-delete"
+ @transport.path << 'add-modify-delete'
@transport.body = file_content
end
params.delete(:method)
+
params = defaultize(params)
@transport.headers.merge!(params)
res = @transport.execute_request
+
if res =~ /^<BatchID>(.*)<\/BatchID>$/
@id = $1
else
@id = 0
end
true
end
- # Reformats parameters and mixes in some defaults.
- def defaultize(params)
- { :upload_for => "Marketplace",
- :file_format =>"TabDelimited",
- :asin_match_create => "Y",
- :asinate => "Y",
- :batch_id => "Y",
- :email => "Y"}.each_pair{ |key, value| params[key] = value unless params[key] }
- # Some Amazon dimwit figured he'd spell this differently
- if params[:enable_expedited_shipping]
- params["enable-expedited-shipping"] = params[:enable_expedited_shipping]
- params.delete(:enable_expedited_shipping)
- else
- params["enable-expedited-shipping"] = "Y"
- end
- params
- end
-
+ # Returns upload file string.
def file_content(type=:long)
- return @file_content if @file_content
case type
when :long
out = "product-id\tproduct-id-type\titem-condition\tprice\tsku\tquantity\tadd-delete\twill-ship-internationally\texpedited-shipping\titem-note\titem-is-marketplace\tfulfillment-center-id\titem-name\titem-description\tcategory1\timage-url\tshipping-fee\tbrowse-path\tstorefront-feature\tboldface\tasin1\tasin2\tasin3\r\n"
@batch.each{ |item| out << item.to_s }
when :short
out = "sku\tprice\tquantity\r\n"
@batch.each{ |item| out << item.to_s(:short) }
end
- @file_content = out
+ out
end
-
- def file_content=(file_content)
- @file_content = file_content
- end
-
+
+ # Adds an item to inventory.
def <<(item)
@batch << item
end
+
+ private
+
+ def defaultize(params)
+ params = {
+ :upload_for => 'Marketplace',
+ :file_format => 'TabDelimited',
+ :asin_match_create => 'Y',
+ :asinate => 'Y',
+ :batch_id => 'Y',
+ :email => 'Y'
+ }.merge(params)
+
+ # Some Amazon dimwit figured he'd rather not camelize this one
+ if params[:enable_expedited_shipping]
+ params['enable-expedited-shipping'] = params[:enable_expedited_shipping]
+ params.delete(:enable_expedited_shipping)
+ else
+ params['enable-expedited-shipping'] = 'Y'
+ end
+
+ params
+ end
end
+ # This is an inventory item.
class Item
- attr_accessor :product_id, :product_id_type, :item_condition, :price, :sku, :quantity, :add_delete, :will_ship_internationally, :expedited_shipping, :item_note, :item_is_marketplace, :fulfillment_center_id, :item_name, :item_description, :category1, :image_url, :shipping_fee, :browse_path, :storefront_feature, :boldface, :asin1, :asin2, :asin3
+ attr_accessor :product_id,
+ :product_id_type,
+ :item_condition,
+ :price,
+ :sku,
+ :quantity,
+ :add_delete,
+ :will_ship_internationally,
+ :expedited_shipping,
+ :item_note,
+ :item_is_marketplace,
+ :fulfillment_center_id,
+ :item_name,
+ :item_description,
+ :category1,
+ :image_url,
+ :shipping_fee,
+ :browse_path,
+ :storefront_feature,
+ :boldface,
+ :asin1,
+ :asin2,
+ :asin3
def initialize(params={})
params.each_pair{ |key, value| send("#{key.to_s}=", value) }
end
\ No newline at end of file