Class: Blastramp::InventoryCountQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/blastramp/inventory_count_query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ InventoryCountQuery

Returns a new instance of InventoryCountQuery.



6
7
8
# File 'lib/blastramp/inventory_count_query.rb', line 6

def initialize(session)
  @session = session
end

Instance Attribute Details

#session ⇒ Object (readonly)

Associations



4
5
6
# File 'lib/blastramp/inventory_count_query.rb', line 4

def session
  @session
end

Instance Method Details

#build(values = {}) ⇒ Object

Returns a new, unpersisted Blastramp::InventoryCount



11
12
13
14
15
# File 'lib/blastramp/inventory_count_query.rb', line 11

def build(values = {})
  inventory_count = Blastramp::InventoryCount.new(values)
  # inventory_count.session = session
  inventory_count
end

#find(sku) ⇒ Object



17
18
19
20
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 'lib/blastramp/inventory_count_query.rb', line 17

def find(sku)
  # Get a list of InventoryCounts from Blastramp
  endpoint = "http://www.ioperate.net/ws/inventory/inventoryws.asmx?wsdl"
  response = session.request endpoint, 'InventoryCountQuery' do
    http.headers["SOAPAction"] = "http://chrome52/webservices/InventoryCountQuery" 
    
    soap.body = {
      'VendorCode' => session.vendor_code,
      'VendorAccessKey' => session.vendor_access_key,
      'Sku' => sku
    }
  end
  
  if (response.to_hash[:result] == 'SUCCESS')      
    # Make sure we always have an array of handles even if the result only contains one
    handles = [response[:query_results][:counts][:inventory_count]].flatten.reject(&:blank?)
  
    # Create partial InventoryCount entities
    handles.collect do |handle|
      inventory_count = build(handle)
      inventory_count
    end
  else
    if (response.to_hash[:error_number] == '20')
      raise(SKUDoesNotExistError.new)
    else
      raise(AuthenticationFailure.new)
    end
  end

end

#find_by_whid(sku, whid) ⇒ Object



49
50
51
52
53
# File 'lib/blastramp/inventory_count_query.rb', line 49

def find_by_whid(sku, whid)
  counts = self.find(sku)
  counts = counts.select{|i| i.whid == whid}
  count = counts[0]
end