lipseys 0.3.0 → 1.0.0.pre

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: d5d5a48bba48849118345bc5003c5e977e1a2134
4
- data.tar.gz: 314670efa9e926b32119710c2ca09654ba54b789
3
+ metadata.gz: ddd0c7601d721690c5ea57300000e699c276222d
4
+ data.tar.gz: e30fe902d6dfd3c4e1a1916c089f1c331e022fc0
5
5
  SHA512:
6
- metadata.gz: 5ca79581dd4a12db3b625302fe2fbb542f3069a96523fe5afcfac12d233a661c67f40c994aefeea38af317ef7b4dcf84be7796d497ef1afafafe6e3a4f35b54a
7
- data.tar.gz: 94e383501b78bd0149bde36736555b9d31100f84a456759166328a0e3c8931ad32ef69248d93f536f023ed3fcaae869a9db7a8e60e89d3e47c9025b1b2eabe73
6
+ metadata.gz: 977adbdbfd24e9ea9fbe987b366e340ee26818ff960a334d627fc23f1a343de4e77e843c9cf6affc008e0b5a0495cab5a2a7586ba9662360971e1e25594612f0
7
+ data.tar.gz: 9de57b2580570a934be5de2b34aa2256477edf191546dfd64f274cc492153afbf650b82b0da23413f71d1426f1197a531ad5c185007a07e6c0e77da6e38095e4
data/lib/lipseys.rb CHANGED
@@ -13,6 +13,9 @@ require 'lipseys/invoice'
13
13
  require 'lipseys/order'
14
14
  require 'lipseys/user'
15
15
 
16
+ require 'lipseys/chunker'
17
+ require 'lipseys/parser'
18
+
16
19
  module Lipseys
17
20
  class NotAuthenticated < StandardError; end
18
21
  end
data/lib/lipseys/base.rb CHANGED
@@ -43,5 +43,21 @@ module Lipseys
43
43
  msg =~ /Login failed/i
44
44
  end
45
45
 
46
+ def stream_to_tempfile(api_url, params)
47
+ tempfile = Tempfile.new
48
+ uri = URI(api_url)
49
+ uri.query = URI.encode_www_form(params)
50
+
51
+ Net::HTTP.get_response(uri) do |response|
52
+ File.open(tempfile, 'w') do |file|
53
+ response.read_body do |chunk|
54
+ file.write(chunk.force_encoding('UTF-8'))
55
+ end
56
+ end
57
+ end
58
+
59
+ tempfile
60
+ end
61
+
46
62
  end
47
63
  end
@@ -74,7 +74,51 @@ module Lipseys
74
74
  end
75
75
 
76
76
  def all
77
- get_items
77
+ params = {
78
+ email: @email,
79
+ pass: @password
80
+ }
81
+ tempfile = stream_to_tempfile(API_URL, params)
82
+
83
+ items = Array.new
84
+
85
+ Lipseys::Parser.parse(tempfile, 'Item') do |node|
86
+ items.push(map_hash(node))
87
+ end
88
+
89
+ items
90
+ end
91
+
92
+ def self.all_as_chunks(size, options = {}, &block)
93
+ new(options).all_as_chunks(size, &block)
94
+ end
95
+
96
+ def all_as_chunks(size, &block)
97
+ params = {
98
+ email: @email,
99
+ pass: @password
100
+ }
101
+ chunker = Lipseys::Chunker.new(size)
102
+ tempfile = stream_to_tempfile(API_URL, params)
103
+
104
+ Lipseys::Parser.parse(tempfile, 'Item') do |node|
105
+ if chunker.is_full?
106
+ yield(chunker.chunk)
107
+
108
+ chunker.reset
109
+ else
110
+ chunker.add(map_hash(node))
111
+ end
112
+ end
113
+
114
+ # HACK-david
115
+ # since we can't get a count of the items without reading the file
116
+ # Let's just check to see if we have any left in the chunk
117
+ if chunker.chunk.count > 0
118
+ yield(chunker.chunk)
119
+ end
120
+
121
+ tempfile.unlink
78
122
  end
79
123
 
80
124
  def self.firearms(options = {})
@@ -117,69 +161,73 @@ module Lipseys
117
161
 
118
162
  xml_doc = get_response_xml(API_URL, params)
119
163
 
120
- items = []
164
+ items = Array.new
121
165
 
122
166
  xml_doc.css('LipseysCatalog/Item').each do |item|
123
- items << {
124
- item_number: content_for(item, 'ItemNo'),
125
- description_1: content_for(item, 'Desc1'),
126
- description_2: content_for(item, 'Desc2'),
127
- upc: content_for(item, 'UPC'),
128
- manufacturer_model_number: content_for(item, 'MFGModelNo'),
129
- msrp: content_for(item, 'MSRP'),
130
- model: content_for(item, 'Model'),
131
- caliber: content_for(item, 'Caliber'),
132
- manufacturer: content_for(item, 'MFG'),
133
- type: content_for(item, 'Type'),
134
- action: content_for(item, 'Action'),
135
- barrel: content_for(item, 'Barrel'),
136
- capacity: content_for(item, 'Capacity'),
137
- finish: content_for(item, 'Finish'),
138
- length: content_for(item, 'Length'),
139
- receiver: content_for(item, 'Receiver'),
140
- safety: content_for(item, 'Safety'),
141
- sights: content_for(item, 'Sights'),
142
- stock_frame_grips: content_for(item, 'StockFrameGrips'),
143
- magazine: content_for(item, 'Magazine'),
144
- weight: content_for(item, 'Weight'),
145
- image: "http://www.lipseys.net/images/#{content_for(item, 'Image')}",
146
- chamber: content_for(item, 'Chamber'),
147
- drilled_tapped: (content_for(item, 'DrilledTapped') == 'Y'),
148
- rate_of_twist: content_for(item, 'RateOfTwist'),
149
- item_type: content_for(item, 'ItemType'),
150
- feature_1: content_for(item, 'Feature1'),
151
- feature_2: content_for(item, 'Feature2'),
152
- feature_3: content_for(item, 'Feature3'),
153
- shipping_weight: content_for(item, 'ShippingWeight'),
154
- bound_book: {
155
- model: content_for(item, 'BoundBookModel'),
156
- type: content_for(item, 'BoundBookType'),
157
- manufacturer: content_for(item, 'BoundBookMFG'),
158
- },
159
- nfa: {
160
- thread_pattern: content_for(item, 'NFAThreadPattern'),
161
- attach_method: content_for(item, 'NFAAttachMethod'),
162
- baffle: content_for(item, 'NFABaffle'),
163
- can_disassemble: (content_for(item, 'NFACanDisassemble') == 'Y'),
164
- construction: content_for(item, 'NFAConstruction'),
165
- db_reduction: content_for(item, 'NFAdbReduction'),
166
- diameter: content_for(item, 'NFADiameter'),
167
- form_3_caliber: content_for(item, 'NFAForm3Caliber'),
168
- },
169
- optic: {
170
- magnification: content_for(item, 'Magnification'),
171
- maintube: content_for(item, 'Maintube'),
172
- objective: content_for(item, 'Objective'),
173
- adjustable_objective: (content_for(item, 'AdjustableObjective') == 'Y'),
174
- optic_adjustments: content_for(item, 'OpticAdjustments'),
175
- reticle: content_for(item, 'Reticle'),
176
- illuminated_reticle: (content_for(item, 'IlluminatedReticle') == 'Y'),
177
- }
178
- }
167
+ items.push(map_hash(item))
179
168
  end
180
169
 
181
170
  items
182
171
  end
183
172
 
173
+ def map_hash(node)
174
+ {
175
+ item_number: content_for(node, 'ItemNo'),
176
+ description_1: content_for(node, 'Desc1'),
177
+ description_2: content_for(node, 'Desc2'),
178
+ upc: content_for(node, 'UPC'),
179
+ manufacturer_model_number: content_for(node, 'MFGModelNo'),
180
+ msrp: content_for(node, 'MSRP'),
181
+ model: content_for(node, 'Model'),
182
+ caliber: content_for(node, 'Caliber'),
183
+ manufacturer: content_for(node, 'MFG'),
184
+ type: content_for(node, 'Type'),
185
+ action: content_for(node, 'Action'),
186
+ barrel: content_for(node, 'Barrel'),
187
+ capacity: content_for(node, 'Capacity'),
188
+ finish: content_for(node, 'Finish'),
189
+ length: content_for(node, 'Length'),
190
+ receiver: content_for(node, 'Receiver'),
191
+ safety: content_for(node, 'Safety'),
192
+ sights: content_for(node, 'Sights'),
193
+ stock_frame_grips: content_for(node, 'StockFrameGrips'),
194
+ magazine: content_for(node, 'Magazine'),
195
+ weight: content_for(node, 'Weight'),
196
+ image: "http://www.lipseys.net/images/#{content_for(node, 'Image')}",
197
+ chamber: content_for(node, 'Chamber'),
198
+ drilled_tapped: (content_for(node, 'DrilledTapped') == 'Y'),
199
+ rate_of_twist: content_for(node, 'RateOfTwist'),
200
+ item_type: content_for(node, 'ItemType'),
201
+ feature_1: content_for(node, 'Feature1'),
202
+ feature_2: content_for(node, 'Feature2'),
203
+ feature_3: content_for(node, 'Feature3'),
204
+ shipping_weight: content_for(node, 'ShippingWeight'),
205
+ bound_book: {
206
+ model: content_for(node, 'BoundBookModel'),
207
+ type: content_for(node, 'BoundBookType'),
208
+ manufacturer: content_for(node, 'BoundBookMFG'),
209
+ },
210
+ nfa: {
211
+ thread_pattern: content_for(node, 'NFAThreadPattern'),
212
+ attach_method: content_for(node, 'NFAAttachMethod'),
213
+ baffle: content_for(node, 'NFABaffle'),
214
+ can_disassemble: (content_for(node, 'NFACanDisassemble') == 'Y'),
215
+ construction: content_for(node, 'NFAConstruction'),
216
+ db_reduction: content_for(node, 'NFAdbReduction'),
217
+ diameter: content_for(node, 'NFADiameter'),
218
+ form_3_caliber: content_for(node, 'NFAForm3Caliber'),
219
+ },
220
+ optic: {
221
+ magnification: content_for(node, 'Magnification'),
222
+ maintube: content_for(node, 'Maintube'),
223
+ objective: content_for(node, 'Objective'),
224
+ adjustable_objective: (content_for(node, 'AdjustableObjective') == 'Y'),
225
+ optic_adjustments: content_for(node, 'OpticAdjustments'),
226
+ reticle: content_for(node, 'Reticle'),
227
+ illuminated_reticle: (content_for(node, 'IlluminatedReticle') == 'Y'),
228
+ }
229
+ }
230
+ end
231
+
184
232
  end
185
233
  end
@@ -0,0 +1,34 @@
1
+ module Lipseys
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
+ 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
@@ -28,7 +28,51 @@ module Lipseys
28
28
  end
29
29
 
30
30
  def all
31
- get_items
31
+ params = {
32
+ email: @email,
33
+ pass: @password
34
+ }
35
+ tempfile = stream_to_tempfile(API_URL, params)
36
+
37
+ items = Array.new
38
+
39
+ Lipseys::Parser.parse(tempfile, 'Item') do |node|
40
+ items.push(map_hash(node))
41
+ end
42
+
43
+ items
44
+ end
45
+
46
+ def self.all_as_chunks(size, options = {}, &block)
47
+ new(options).all_as_chunks(size, &block)
48
+ end
49
+
50
+ def all_as_chunks(size, &block)
51
+ params = {
52
+ email: @email,
53
+ pass: @password
54
+ }
55
+ chunker = Lipseys::Chunker.new(size)
56
+ tempfile = stream_to_tempfile(API_URL, params)
57
+
58
+ Lipseys::Parser.parse(tempfile, 'Item') do |node|
59
+ if chunker.is_full?
60
+ yield(chunker.chunk)
61
+
62
+ chunker.reset
63
+ else
64
+ chunker.add(map_hash(node))
65
+ end
66
+ end
67
+
68
+ # HACK-david
69
+ # since we can't get a count of the items without reading the file
70
+ # Let's just check to see if we have any left in the chunk
71
+ if chunker.chunk.count > 0
72
+ yield(chunker.chunk)
73
+ end
74
+
75
+ tempfile.unlink
32
76
  end
33
77
 
34
78
  def self.firearms(options = {})
@@ -71,23 +115,27 @@ module Lipseys
71
115
 
72
116
  xml_doc = get_response_xml(API_URL, params)
73
117
 
74
- items = []
118
+ items = Array.new
75
119
 
76
120
  xml_doc.css('LipseysInventoryPricing/Item').each do |item|
77
- items << {
78
- item_number: content_for(item, 'ItemNo'),
79
- upc: content_for(item, 'UPC'),
80
- manufacturer_model_number: content_for(item, 'MFGModelNo'),
81
- quantity_on_hand: content_for(item, 'QtyOnHand'),
82
- allocation: (content_for(item, 'Allocation') == 'Y'),
83
- price: content_for(item, 'Price'),
84
- on_sale: (content_for(item, 'OnSale') == 'Y'),
85
- retail_map: content_for(item, 'RetailMAP')
86
- }
121
+ items.push(map_hash(item))
87
122
  end
88
123
 
89
124
  items
90
125
  end
91
126
 
127
+ def map_hash(node)
128
+ {
129
+ item_number: content_for(node, 'ItemNo'),
130
+ upc: content_for(node, 'UPC'),
131
+ manufacturer_model_number: content_for(node, 'MFGModelNo'),
132
+ quantity_on_hand: content_for(node, 'QtyOnHand'),
133
+ allocation: (content_for(node, 'Allocation') == 'Y'),
134
+ price: content_for(node, 'Price'),
135
+ on_sale: (content_for(node, 'OnSale') == 'Y'),
136
+ retail_map: content_for(node, 'RetailMAP')
137
+ }
138
+ end
139
+
92
140
  end
93
141
  end
@@ -0,0 +1,25 @@
1
+ module Lipseys
2
+ class Parser
3
+
4
+ attr_reader :file
5
+
6
+ def initialize(file)
7
+ @file = file
8
+ end
9
+
10
+ def self.parse(file, node_name, &block)
11
+ new(file).parse(node_name, &block)
12
+ end
13
+
14
+ def parse(node_name, &block)
15
+ File.open(@file) do |file|
16
+ Nokogiri::XML::Reader.from_io(file).each do |node|
17
+ if node.name == node_name and node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT
18
+ yield(Nokogiri::XML(node.outer_xml))
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module Lipseys
2
- VERSION = "0.3.0"
2
+ VERSION = "1.0.0.pre"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lipseys
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.0.0.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dale Campbell
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-31 00:00:00.000000000 Z
11
+ date: 2017-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -101,9 +101,11 @@ files:
101
101
  - lib/lipseys.rb
102
102
  - lib/lipseys/base.rb
103
103
  - lib/lipseys/catalog.rb
104
+ - lib/lipseys/chunker.rb
104
105
  - lib/lipseys/inventory.rb
105
106
  - lib/lipseys/invoice.rb
106
107
  - lib/lipseys/order.rb
108
+ - lib/lipseys/parser.rb
107
109
  - lib/lipseys/soap_client.rb
108
110
  - lib/lipseys/user.rb
109
111
  - lib/lipseys/version.rb
@@ -123,9 +125,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
125
  version: '0'
124
126
  required_rubygems_version: !ruby/object:Gem::Requirement
125
127
  requirements:
126
- - - ">="
128
+ - - ">"
127
129
  - !ruby/object:Gem::Version
128
- version: '0'
130
+ version: 1.3.1
129
131
  requirements: []
130
132
  rubyforge_project:
131
133
  rubygems_version: 2.5.1