Class: SportsSouth::Inventory

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

Constant Summary collapse

API_URL =
'http://webservices.theshootingwarehouse.com/smart/inventory.asmx'

Constants inherited from Base

Base::CONTENT_TYPE, Base::TIMEOUT, Base::USER_AGENT

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Inventory

Returns a new instance of Inventory.



6
7
8
9
10
# File 'lib/sports_south/inventory.rb', line 6

def initialize(options = {})
  requires!(options, :username, :password)

  @options = options
end

Class Method Details

.all(chunk_size = 15, options = {}, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sports_south/inventory.rb', line 12

def self.all(chunk_size = 15, options = {}, &block)
  requires!(options, :username, :password)

  if options[:last_updated].present?
    options[:last_updated] = options[:last_updated].strftime('%Y-%m-%dT%H:%M:00.00%:z')
  else
    options[:last_updated] = '1990-09-25T14:15:47-04:00'
  end

  options[:last_item] ||= '-1'

  new(options).all(chunk_size, &block)
end

.get(item_identifier, options = {}) ⇒ Object



26
27
28
29
# File 'lib/sports_south/inventory.rb', line 26

def self.get(item_identifier, options = {})
  requires!(options, :username, :password)
  new(options).get(item_identifier)
end

.quantity(chunk_size = 100, options = {}, &block) ⇒ Object



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

def self.quantity(chunk_size = 100, options = {}, &block)
  requires!(options, :username, :password)

  if options[:last_updated].present?
    options[:last_updated] = options[:last_updated].to_s("yyyy-MM-ddTHH:mm:sszzz")
  else
    options[:last_updated] = '1990-09-25T14:15:47-04:00'
  end

  options[:last_item] ||= '-1'

  new(options).all(chunk_size, &block)
end

Instance Method Details

#all(chunk_size, &block) ⇒ Object



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/sports_south/inventory.rb', line 31

def all(chunk_size, &block)
  chunker = SportsSouth::Chunker.new(chunk_size)
  http, request = get_http_and_request(API_URL, '/IncrementalOnhandUpdate')

  request.set_form_data(form_params = form_params(@options).merge({
    SinceDateTime: @options[:last_updated],
    LastItem:      @options[:last_item].to_s
  }))

  response  = http.request(request)
  xml_doc   = Nokogiri::XML(sanitize_response(response))

  xml_doc.css('Onhand').map do |item|
    if chunker.is_full?
      yield(chunker.chunk)

      chunker.reset!
    else
      chunker.add(self.map_hash(item))
    end
  end

  if chunker.chunk.count > 0
    yield(chunker.chunk)
  end
end

#get(item_identifier) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/sports_south/inventory.rb', line 102

def get(item_identifier)
  http, request = get_http_and_request(API_URL, '/OnhandInquiry')

  request.set_form_data(form_params(@options).merge({ ItemNumber: item_identifier }))

  response = http.request(request)
  xml_doc  = Nokogiri::XML(sanitize_response(response))
end

#quantity(chunk_size, &block) ⇒ Object



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
# File 'lib/sports_south/inventory.rb', line 72

def quantity(chunk_size, &block)
  chunker = SportsSouth::Chunker.new(chunk_size)
  http, request = get_http_and_request(API_URL, '/IncrementalOnhandUpdate')

  request.set_form_data(form_params = form_params(@options).merge({
    SinceDateTime: @options[:last_updated],
    LastItem:      @options[:last_item].to_s
  }))

  response  = http.request(request)
  xml_doc   = Nokogiri::XML(sanitize_response(response))

  xml_doc.css('Onhand').map do |item|
    if chunker.is_full?
      yield(chunker.chunk)

      chunker.reset!
    else
      chunker.add({
        item_identifier: content_for(node, 'I'),
        quantity: content_for(node, 'Q').to_i
      })
    end
  end

  if chunker.chunk.count > 0
    yield(chunker.chunk)
  end
end