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'
CATALOG_CODES =
{
  'S' => :special,
  'C' => :closeout,
  'F' => :flyer,
  'B' => :buyers_special,
  'N' => :net_price,
}
ITEM_TYPES =
{
  '1' => :handgun,
  '2' => :long_gun,
  '3' => :accessory,
  '4' => :ammunition,
  '5' => :optics,
  '6' => :archery,
  '7' => :reloading,
  '8' => :suppressor,
}

Constants inherited from Base

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

Class Method Summary collapse

Class Method Details

.all(options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sports_south/inventory.rb', line 25

def self.all(options = {})
  requires!(options, :username, :password, :source, :customer_number)

  options[:last_update] ||= '1/1/1990' # Return full catalog.
  options[:last_item]   ||= '-1'       # Return all items.

  http, request = get_http_and_request(API_URL, '/DailyItemUpdate')

  request.set_form_data(form_params(options).merge({
    LastUpdate: options[:last_update],
    LastItem:   options[:last_item].to_s,
  }))

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

  raise SportsSouth::NotAuthenticated if not_authenticated?(xml_doc)

  xml_doc.css('Table').map { |item| map_hash(item, mode: options[:mode]) }
end

.get_text(item_number, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sports_south/inventory.rb', line 45

def self.get_text(item_number, options = {})
  requires!(options, :username, :password, :source, :customer_number)

  http, request = get_http_and_request(API_URL, '/GetText')

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

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

  raise SportsSouth::NotAuthenticated if not_authenticated?(xml_doc)

  {
    item_number:  item_number,
    catalog_text: content_for(xml_doc, 'CATALOGTEXT')
  }
end

.incremental_onhand_update(options = {}) ⇒ Object

Pass an optional ‘:since` option (YYYY-MM-DDTHH:mm:ss.mss-HH:00) to get items updated since that timestamp.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/sports_south/inventory.rb', line 123

def self.incremental_onhand_update(options = {})
  requires!(options, :username, :password, :source, :customer_number)

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

  http, request = get_http_and_request(API_URL, '/IncrementalOnhandUpdate')

  request.set_form_data(form_params(options).merge({ SinceDateTime: options[:since] }))

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

  raise SportsSouth::NotAuthenticated if not_authenticated?(xml_doc)

  xml_doc.css('Onhand').map do |item|
    {
      item_number:      content_for(item, 'I'),
      quantity:         content_for(item, 'Q'),
      quantity_changed: content_for(item, 'D'),
      catalog_price:    content_for(item, 'P'),
      customer_price:   content_for(item, 'C'),
    }
  end
end

.inquiry(item_number, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sports_south/inventory.rb', line 63

def self.inquiry(item_number, options = {})
  requires!(options, :username, :password, :source, :customer_number)

  http, request = get_http_and_request(API_URL, '/OnhandInquiry')

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

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

  raise SportsSouth::NotAuthenticated if not_authenticated?(xml_doc)

  {
    item_number:      content_for(xml_doc, 'I'),
    quantity_on_hand: content_for(xml_doc, 'Q').to_i,
    catalog_price:    content_for(xml_doc, 'P'),
    customer_price:   content_for(xml_doc, 'C'),
  }
end

.list_new_text(options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sports_south/inventory.rb', line 83

def self.list_new_text(options = {})
  requires!(options, :username, :password, :source, :customer_number)
  options[:since] ||= (Time.now - 86400).strftime('%m/%d/%Y')

  http, request = get_http_and_request(API_URL, '/ListNewText')
  request.set_form_data(form_params(options).merge({ DateFrom: options[:since] }))

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

  xml_doc.css('Table').map do |item|
    {
      item_number: content_for(item, 'ITEMNO'),
      text: content_for(item, 'TEXT')
    }
  end
end

.onhand_update(options = {}) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/sports_south/inventory.rb', line 147

def self.onhand_update(options = {})
  requires!(options, :username, :password, :source, :customer_number)

  http, request = get_http_and_request(API_URL, '/OnhandUpdate')

  request.set_form_data(form_params(options))

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

  raise SportsSouth::NotAuthenticated if not_authenticated?(xml_doc)

  xml_doc.css('Table').map do |item|
    {
      item_number:    content_for(item, 'I'),
      quantity:       content_for(item, 'Q').to_i,
      catalog_price:  content_for(item, 'P'),
      customer_price: content_for(item, 'C'),
    }
  end
end

.onhand_update_by_csv(item_numbers, options = {}) ⇒ Object

This method accepts an Array of item_numbers.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/sports_south/inventory.rb', line 101

def self.onhand_update_by_csv(item_numbers, options = {})
  requires!(options, :username, :password, :source, :customer_number)

  http, request = get_http_and_request(API_URL, '/OnhandUpdatebyCSV')

  request.set_form_data(form_params(options).merge({ CSVItems: item_numbers.join(',') }))

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

  raise SportsSouth::NotAuthenticated if not_authenticated?(xml_doc)

  xml_doc.css('Table').map do |item|
    {
      item_number: content_for(item, 'I'),
      quantity: content_for(item, 'Q'),
      price: content_for(item, 'P'),
    }
  end
end