zanders 2.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 87b6fba3d86fa736bca99aa0c64cddc262c1b8c0
4
- data.tar.gz: 108b978150d49336b6c697e8b108270a2c4db8a9
3
+ metadata.gz: 4e06bfb17dc0814d2d1ff1fcae78f30cde28071d
4
+ data.tar.gz: 066e53e422081dc2d25ed0fa8f0eb7721b6eb0b4
5
5
  SHA512:
6
- metadata.gz: 963c1d800361f0e04a18b22535ab3d153772813224772e2b5af3ab7a19f60602b69280fccac2f24ba5a3802ddd10217a623fba0e458fe2594266738f85b85043
7
- data.tar.gz: 65f26302cd660430a46064e4b1ecf7d3f16f8bca393ea3f66761bf6373c725ba64595b9740730fc6b133d26ea6e8b089c18ee1e5cc042471be813f337d759317
6
+ metadata.gz: d5b3277a7cec7dc88e8558b8746c2f3969c89aa21532fdbd6ded1c9c7a08ba54c4cb9268d7338f956a5182a4687b1a905400f53f70d9b7b0a168457eab03e1e1
7
+ data.tar.gz: fbe982215e5832915e4d914a10968905b40576972dcccf2ff972e584c675d9ae63a42b8e7f4d1c0579638e8d0c94f8285bc30edcde6b03aa1d32192ecd1a569c
data/lib/zanders/base.rb CHANGED
@@ -40,5 +40,19 @@ module Zanders
40
40
  end
41
41
  end
42
42
 
43
+ def content_for(xml_doc, field)
44
+ node = xml_doc.css(field).first
45
+
46
+ if node.nil?
47
+ nil
48
+ else
49
+ if node.css("DATA").present?
50
+ node.css("DATA").text.strip
51
+ else
52
+ node.content.strip
53
+ end
54
+ end
55
+ end
56
+
43
57
  end
44
58
  end
@@ -1,11 +1,10 @@
1
1
  module Zanders
2
2
  class Catalog < Base
3
3
 
4
- CATALOG_FILENAME = "zandersinv.csv"
4
+ CATALOG_FILENAME = "zandersinv.xml"
5
5
 
6
6
  def initialize(options = {})
7
7
  requires!(options, :username, :password)
8
-
9
8
  @options = options
10
9
  end
11
10
 
@@ -14,7 +13,9 @@ module Zanders
14
13
  new(options).all(chunk_size, &block)
15
14
  end
16
15
 
17
- def all(chunk_size, &block)
16
+ def all(chunk_size, &block)
17
+ chunker = Zanders::Chunker.new(chunk_size)
18
+
18
19
  connect(@options) do |ftp|
19
20
  begin
20
21
  csv_tempfile = Tempfile.new
@@ -22,27 +23,20 @@ module Zanders
22
23
  ftp.chdir(Zanders.config.ftp_directory)
23
24
  ftp.getbinaryfile(CATALOG_FILENAME, csv_tempfile.path)
24
25
 
25
- SmarterCSV.process(csv_tempfile, {
26
- :chunk_size => chunk_size,
27
- :convert_values_to_numeric => false,
28
- :key_mapping => {
29
- :available => :quantity,
30
- :desc1 => :short_description,
31
- :itemnumber => :item_identifier,
32
- :manufacturer => :brand,
33
- :mfgpnumber => :mfg_number,
34
- :mapprice => :map_price,
35
- :price1 => :price
36
- }
37
- }) do |chunk|
38
- chunk.each do |item|
39
- item[:name] = item[:short_description]
40
- item[:long_description] = "#{item[:short_description]} #{item[:desc2]}"
41
-
42
- item.except!(:desc2, :qty1, :qty2, :qty3, :price2, :price3)
26
+ xml_doc = Nokogiri::XML(csv_tempfile)
27
+
28
+ xml_doc.xpath("//ZandersDataOut").each do |item|
29
+ if chunker.is_full?
30
+ yield(chunker.chunk)
31
+
32
+ chunker.reset!
33
+ else
34
+ chunker.add(map_hash(item, @options[:full_product].present?))
43
35
  end
36
+ end
44
37
 
45
- yield(chunk)
38
+ if chunker.chunk.count > 0
39
+ yield(chunker.chunk)
46
40
  end
47
41
 
48
42
  csv_tempfile.unlink
@@ -52,5 +46,40 @@ module Zanders
52
46
  end
53
47
  end
54
48
 
49
+ protected
50
+
51
+ def map_hash(node, full_product = false)
52
+ features = self.map_features(node)
53
+
54
+ {
55
+ name: content_for(node, 'ITEMDESCRIPTION'),
56
+ upc: content_for(node, 'ITEMUPC'),
57
+ item_identifier: content_for(node, 'ITEMNO'),
58
+ quantity: content_for(node, 'ITEMQTYAVAIL'),
59
+ price: content_for(node, 'ITEMPRICE'),
60
+ short_description: content_for(node, 'ITEMDESCRIPTION'),
61
+ category: content_for(node, 'ITEMCATEGORYNAME'),
62
+ product_type: content_for(node, 'ITEMPRODUCTTYPE'),
63
+ mfg_number: content_for(node, 'ITEMMPN'),
64
+ weight: content_for(node, 'ITEMWEIGHT'),
65
+ map_price: content_for(node, 'ITEMMAPPRICE'),
66
+ brand: content_for(node, 'ITEMMANUFACTURER'),
67
+ features: features
68
+ }
69
+ end
70
+
71
+ def map_features(node)
72
+ features = Hash.new
73
+
74
+ node.css("ATTRIBUTE").each do |feature|
75
+ features[feature.css("TITLE").text.strip] = feature.css("DATA").text.strip
76
+ end
77
+
78
+ features.delete_if { |k, v| v.to_s.blank? }
79
+ features.transform_keys! { |k| k.gsub(/\s+/, '_').downcase }
80
+ features.transform_keys! { |k| k.gsub(/[^0-9A-Za-z \_]/, '') }
81
+ features.symbolize_keys!
82
+ end
83
+
55
84
  end
56
85
  end
@@ -0,0 +1,34 @@
1
+ module Zanders
2
+ class Chunker
3
+
4
+ attr_accessor :chunk, :total_count, :current_count, :size
5
+
6
+ def initialize(size, total_count = nil)
7
+ @size = size
8
+ @chunk = Array.new
9
+ @current_count = 0
10
+ @total_count = total_count
11
+ end
12
+
13
+ def add(row)
14
+ self.reset! if is_full?
15
+
16
+ @chunk.push(row)
17
+
18
+ @current_count += 1
19
+ end
20
+
21
+ def reset!
22
+ @chunk.clear
23
+ end
24
+
25
+ def is_full?
26
+ @chunk.count == @size
27
+ end
28
+
29
+ def is_complete?
30
+ @total_count == @current_count
31
+ end
32
+
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module Zanders
2
- VERSION = "2.0"
2
+ VERSION = "2.0.1"
3
3
  end
data/lib/zanders.rb CHANGED
@@ -5,6 +5,7 @@ require 'savon'
5
5
 
6
6
  require 'zanders/base'
7
7
  require 'zanders/soap_client'
8
+ require 'zanders/chunker'
8
9
 
9
10
  require 'zanders/user'
10
11
  require 'zanders/address'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zanders
3
3
  version: !ruby/object:Gem::Version
4
- version: '2.0'
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Knight
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-21 00:00:00.000000000 Z
11
+ date: 2017-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -116,6 +116,7 @@ files:
116
116
  - lib/zanders/address.rb
117
117
  - lib/zanders/base.rb
118
118
  - lib/zanders/catalog.rb
119
+ - lib/zanders/chunker.rb
119
120
  - lib/zanders/inventory.rb
120
121
  - lib/zanders/item.rb
121
122
  - lib/zanders/order.rb