Class: Peddler::Inventory::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/peddler/inventory.rb

Overview

This is an inventory batch.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport) ⇒ Batch

Returns a new instance of Batch.



22
23
24
25
# File 'lib/peddler/inventory.rb', line 22

def initialize(transport)
  @transport = transport
  @batch = []
end

Instance Attribute Details

#batchObject

Returns the value of attribute batch.



20
21
22
# File 'lib/peddler/inventory.rb', line 20

def batch
  @batch
end

#idObject (readonly)

Returns the value of attribute id.



19
20
21
# File 'lib/peddler/inventory.rb', line 19

def id
  @id
end

Instance Method Details

#<<(item) ⇒ Object

Adds an item to inventory.



72
73
74
# File 'lib/peddler/inventory.rb', line 72

def <<(item)
  @batch << item
end

#file_content(type = :long) ⇒ Object

Returns upload file string.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/peddler/inventory.rb', line 59

def file_content(type=:long)
  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
  out
end

#upload(params = {}) ⇒ Object

Uploads batch to Amazon.

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/peddler/inventory.rb', line 28

def upload(params={})
  raise PeddlerError.new('Batch already uploaded') unless @id.nil?
  @transport.legacize_request
  @transport.path << 'catalog-upload/'
  
  case params[:method].to_s
  when 'modify'
    @transport.path << 'modify-only'
    @transport.body = file_content(:short)
  when 'purge'
    @transport.path << 'purge-replace'
    @transport.body = file_content
  else
    @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