rsr_group 1.6.2 → 1.7.0

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: fa4198a0176561bf852aee7ca7c1df677009644b
4
- data.tar.gz: 84c44fffbc967de623172cc301bb347ae252b297
3
+ metadata.gz: b8e45f8036e006550b14071c2cf15167571e8803
4
+ data.tar.gz: 53dbdea34eac71093fe91859e06ab73b117ece41
5
5
  SHA512:
6
- metadata.gz: 2a2a11c324f55c6638aaad729fd83365d4cb624ff67636c7d949efcde5b91078cce856addca1345fb54783bc6dc79254c32c7ed1d276b0ecec029052a1d9acb3
7
- data.tar.gz: 689d5eea5422d3fe95b71c71c21ece566d596cd9896fb961f3e354c11970b38f92964a5447fb35995979cd90bf6fc40fdec889fcd87fab330fe5c8f2909e8c1a
6
+ metadata.gz: f0095e4f51ec8fdfe06425ff99c2bb73957817a1eab7014f99592080bdd83cd2171cc0a99c68c081225031ea58a8084b9a0559d4810230ac740975ee960009ed
7
+ data.tar.gz: b5dce8d75c8a82d4e821523596de3ac763e712cddc565682c43a911f7a6726444bb9ed1b174f50d5ed18cbe66fe7f3357092a6353763b3015190b54d3a6e1b61
data/lib/rsr_group.rb CHANGED
@@ -7,6 +7,7 @@ require 'smarter_csv'
7
7
 
8
8
  require 'rsr_group/base'
9
9
  require 'rsr_group/constants'
10
+ require 'rsr_group/chunker'
10
11
  require 'rsr_group/data_row'
11
12
  require 'rsr_group/department'
12
13
  require 'rsr_group/inventory'
@@ -0,0 +1,34 @@
1
+ module RsrGroup
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
@@ -30,11 +30,25 @@ module RsrGroup
30
30
  new(options).map_prices
31
31
  end
32
32
 
33
+ def self.map_prices_as_chunks(size = 15, options = {})
34
+ requires!(options, :username, :password)
35
+ new(options).map_prices_as_chunks(size) do |chunk|
36
+ yield(chunk)
37
+ end
38
+ end
39
+
33
40
  def self.quantities(options = {})
34
41
  requires!(options, :username, :password)
35
42
  new(options).quantities
36
43
  end
37
44
 
45
+ def self.quantities_as_chunks(size = 15, options = {})
46
+ requires!(options, :username, :password)
47
+ new(options).quantities_as_chunks(size) do |chunk|
48
+ yield(chunk)
49
+ end
50
+ end
51
+
38
52
  def all
39
53
  items = []
40
54
 
@@ -138,40 +152,42 @@ module RsrGroup
138
152
 
139
153
  def process_as_chunks(size)
140
154
  connect(@options) do |ftp|
141
- chunk = []
142
- item_count = 1
155
+ chunker = RsrGroup::Chunker.new(size)
143
156
  temp_csv_file = Tempfile.new
144
157
 
158
+ # Is this a key dealer?
145
159
  if ftp.nlst.include?(KEYDEALER_DIR)
146
160
  ftp.chdir(KEYDEALER_DIR)
161
+
162
+ # Pull from the FTP and save as a temp file
147
163
  ftp.getbinaryfile(KEYDEALER_FILENAME, temp_csv_file.path)
148
164
  else
149
165
  ftp.chdir(INVENTORY_DIR)
166
+
167
+ # Pull from the FTP and save as a temp file
150
168
  ftp.getbinaryfile(INVENTORY_FILENAME, temp_csv_file.path)
151
169
  end
152
170
 
153
- line_count = File.readlines(temp_csv_file).size
171
+ # total_count is the number of lines in the file
172
+ chunker.total_count = File.readlines(temp_csv_file).size
154
173
 
155
174
  CSV.readlines(temp_csv_file, col_sep: ';', quote_char: "\x00").to_enum.with_index(1).each do |row, current_line|
156
- chunk << process_row(row)
157
175
 
158
- # Return chunk if we have reached the end of the file
159
- if line_count == current_line
160
- yield(chunk)
161
- end
176
+ if chunker.is_full?
177
+ yield(chunker.chunk)
162
178
 
163
- # Return if chuck is full
164
- if item_count == size
165
- yield(chunk)
179
+ chunker.reset
180
+ elsif chunker.is_complete?
181
+ yield(chunker.chunk)
166
182
 
167
- chunk = []
168
- item_count = 1
183
+ break
169
184
  else
170
- item_count += 1
185
+ chunker.add(process_row(row))
171
186
  end
172
187
  end
173
188
 
174
189
  temp_csv_file.unlink
190
+ ftp.close
175
191
  end
176
192
  end
177
193
 
@@ -199,6 +215,46 @@ module RsrGroup
199
215
  rows
200
216
  end
201
217
 
218
+ def map_prices_as_chunks(size)
219
+ connect(@options) do |ftp|
220
+ chunker = RsrGroup::Chunker.new(size)
221
+ temp_csv_file = Tempfile.new
222
+
223
+ # Is this a key dealer?
224
+ if ftp.nlst.include?(KEYDEALER_DIR)
225
+ ftp.chdir(KEYDEALER_DIR)
226
+ else
227
+ ftp.chdir(INVENTORY_DIR)
228
+ end
229
+
230
+ # Pull from the FTP and save as a temp file
231
+ ftp.getbinaryfile(MAP_FILENAME, temp_csv_file.path)
232
+
233
+ # total_count is hte number of lines in the file
234
+ chunker.total_count = File.readlines(temp_csv_file).size
235
+
236
+ CSV.readlines(temp_csv_file).each do |row|
237
+ if chunker.is_full?
238
+ yield(chunker.chunk)
239
+
240
+ chunker.reset
241
+ elsif chunker.is_complete?
242
+ yield(chunker.chunk)
243
+
244
+ break
245
+ else
246
+ chunker.add({
247
+ stock_number: row[0].strip,
248
+ map_price: row[1]
249
+ })
250
+ end
251
+ end
252
+
253
+ temp_csv_file.unlink
254
+ ftp.close
255
+ end
256
+ end
257
+
202
258
  def quantities
203
259
  rows = []
204
260
 
@@ -224,6 +280,47 @@ module RsrGroup
224
280
  rows
225
281
  end
226
282
 
283
+ def quantities_as_chunks(size)
284
+ connect(@options) do |ftp|
285
+ chunker = RsrGroup::Chunker.new(size)
286
+ temp_csv_file = Tempfile.new
287
+
288
+ # Is this a key dealer?
289
+ if ftp.nlst.include?(KEYDEALER_DIR)
290
+ ftp.chdir(KEYDEALER_DIR)
291
+ else
292
+ ftp.chdir(INVENTORY_DIR)
293
+ end
294
+
295
+ # Pull from the FTP and save as a temp file
296
+ ftp.getbinaryfile(QTY_FILENAME, temp_csv_file.path)
297
+
298
+ # total_count is hte number of lines in the file
299
+ chunker.total_count = File.readlines(temp_csv_file).size
300
+
301
+ CSV.readlines(temp_csv_file).each do |row|
302
+ if chunker.is_full?
303
+ yield(chunker.chunk)
304
+
305
+ chunker.reset
306
+ elsif chunker.is_complete?
307
+ yield(chunker.chunk)
308
+
309
+ break
310
+ else
311
+ chunker.add({
312
+ stock_number: row[0].rstrip,
313
+ quantity: row[1].to_i
314
+ })
315
+ end
316
+ end
317
+
318
+ temp_csv_file.unlink
319
+ ftp.close
320
+ end
321
+ end
322
+
323
+
227
324
  private
228
325
 
229
326
  def sanitize(data)
@@ -1,3 +1,3 @@
1
1
  module RsrGroup
2
- VERSION = '1.6.2'
2
+ VERSION = '1.7.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rsr_group
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.2
4
+ version: 1.7.0
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-03-27 00:00:00.000000000 Z
11
+ date: 2017-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: smarter_csv
@@ -101,6 +101,7 @@ files:
101
101
  - config.rb.example
102
102
  - lib/rsr_group.rb
103
103
  - lib/rsr_group/base.rb
104
+ - lib/rsr_group/chunker.rb
104
105
  - lib/rsr_group/constants.rb
105
106
  - lib/rsr_group/data_row.rb
106
107
  - lib/rsr_group/department.rb