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'
ITEM_NODE_NAME =
'Onhand'

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.



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

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

  @options = options
end

Class Method Details

.all(options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sports_south/inventory.rb', line 22

def self.all(options = {})
  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
end

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



36
37
38
39
# File 'lib/sports_south/inventory.rb', line 36

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

.get_quantity_file(options = {}) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/sports_south/inventory.rb', line 13

def self.get_quantity_file(options = {})
  requires!(options, :username, :password)

  options[:last_updated] = '1990-09-25T14:15:47-04:00'
  options[:last_item]    = '-1'

  new(options).get_quantity_file
end

.quantity(options = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sports_south/inventory.rb', line 91

def self.quantity(options = {})
  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
end

Instance Method Details

#allObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sports_south/inventory.rb', line 41

def all
  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
  }))

  items    = []
  tempfile = download_to_tempfile(http, request)

  tempfile.rewind

  Nokogiri::XML::Reader.from_io(tempfile).each do |reader|
    next unless reader.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT
    next unless reader.name == ITEM_NODE_NAME

    node = Nokogiri::XML.parse(reader.outer_xml)

    _map_hash = map_hash(node.css(ITEM_NODE_NAME))

    items << _map_hash unless _map_hash.nil?
  end

  tempfile.close
  tempfile.unlink

  items
end

#get(item_identifier) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/sports_south/inventory.rb', line 105

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

#get_quantity_fileObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sports_south/inventory.rb', line 71

def get_quantity_file
  tempfile      = Tempfile.new
  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|
    tempfile.puts("#{content_for(item, 'I')},#{content_for(item, 'Q')}")
  end

  tempfile.close
  tempfile.path
end