Class: SportsSouth::Catalog

Inherits:
Base
  • Object
show all
Defined in:
lib/sports_south/catalog.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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Catalog

Returns a new instance of Catalog.



25
26
27
28
29
30
31
# File 'lib/sports_south/catalog.rb', line 25

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

  @options    = options
  @categories = SportsSouth::Category.all(options)
  @brands     = SportsSouth::Brand.all(options)
end

Class Method Details

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



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sports_south/catalog.rb', line 33

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

  if options[:last_updated].present?
    options[:last_updated] = options[:last_updated].strftime("%-m/%-d/%Y")
  else
    options[:last_updated] ||= '1/1/1990'
  end

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

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

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



47
48
49
50
51
# File 'lib/sports_south/catalog.rb', line 47

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

  new(options, :username, :password)
end

Instance Method Details

#all(chunk_size, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sports_south/catalog.rb', line 53

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

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

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

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

      chunker.reset!
    else
      chunker.add(map_hash(item, @options[:full_product].present?))
    end
  end

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

#get_description(item_number) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sports_south/catalog.rb', line 80

def get_description(item_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))

  content_for(xml_doc, 'CATALOGTEXT')
end