rsmart_toolbox 0.12 → 0.13

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: 9df84131abe73171ab6f69048fdf14d029dd14c0
4
- data.tar.gz: 704b9cd5c78da9f72c3be53f062bcbf0f640a34c
3
+ metadata.gz: 6c0cd72de9171f05cb22855c6ff161a4e42a37dc
4
+ data.tar.gz: c588c12162f788f24667204bea50ea8887e6427b
5
5
  SHA512:
6
- metadata.gz: 3ebe50b00afa63097e166a83ac0ac170198e1d22e2ca1d2761077037f46206ab9007a629659bde252bdde87496975446b16c1273fba0cc6e70a81e7c2e6992be
7
- data.tar.gz: 4a8ac96dbd5f97dde57c4f84699e1c6209465205be08c0a5b3af1b1192882e531c52eef06622faeb81fbd98db86383a90aa6ea5f9f7edba6aca3c32a15e98a0c
6
+ metadata.gz: 331beb417a05b9fe82098b82f228c10cff957418be37449e52093fae7419ca4a147400b46d1b04f8d2ff280f834765137a9ad3b0e3582b0a0af63e408c6d2209
7
+ data.tar.gz: c83f554a58847ee106404111fba5fdab2896f354f3655df1f0f938df409d8ba0df801d92e92629cdbd7240013eda8a17a4b33758f6ef6892e389509620449325
checksums.yaml.gz.sig CHANGED
Binary file
@@ -262,6 +262,83 @@ module Rsmart::ETL
262
262
  end
263
263
  end
264
264
 
265
+ # Parse a SQL date from a String.
266
+ # @param [String] str the String to be parsed.
267
+ # @param [Hash] opt options Hash will be passed through to #parse_string.
268
+ # @return [String] the parsed date. nil or empty inputs will return '' by default.
269
+ # @see parse_string
270
+ def self.parse_date(str, opt={ valid_values: /^$|(\d{4}\-\d{2}\-\d{2}){1}/ })
271
+ opt[:valid_values] = /^$|(\d{4}\-\d{2}\-\d{2}){1}/ if opt[:valid_values].nil?
272
+ return parse_string str, opt
273
+ end
274
+
275
+ # Helper method for #parse_date which finds the value by column :name and mutates the SQL statement accordingly.
276
+ # @param [CSV::Row] row the CSV Row being parsed
277
+ # @param [String] insert_str the left side of the insert statement (i.e. columns)
278
+ # @param [String] values_str the right side of the insert statement (i.e. values)
279
+ # @param [Hash] opt options Hash will be passed through to #parse_date.
280
+ # @option opt [String] :name the name of the field being parsed. Required.
281
+ # @return [void]
282
+ # @raise [ArgumentError] :name is required.
283
+ # @see parse_date
284
+ # @see mutate_sql_stmt!
285
+ def self.parse_date!(row, insert_str, values_str, opt={})
286
+ raise ArgumentError, "opt[:name] is required!" unless opt[:name]
287
+ date = parse_date( row[ to_symbol( opt[:name] ) ], opt )
288
+ mutate_sql_stmt! insert_str, opt[:name], values_str, date
289
+ end
290
+
291
+ # Parse a SQL datetime from a String.
292
+ # @param [String] str the String to be parsed.
293
+ # @param [Hash] opt options Hash will be passed through to #parse_string.
294
+ # @return [String] the parsed datetime. nil or empty inputs will return '' by default.
295
+ # @see parse_string
296
+ def self.parse_datetime(str, opt={ valid_values: /^$|(\d{4}\-\d{2}\-\d{2}){1}\s(\d{2}:\d{2}:\d{2})?/ })
297
+ opt[:valid_values] = /^$|(\d{4}\-\d{2}\-\d{2}){1}\s(\d{2}:\d{2}:\d{2})?/ if opt[:valid_values].nil?
298
+ return parse_string str, opt
299
+ end
300
+
301
+ # Helper method for #parse_datetime which finds the value by column :name and mutates the SQL statement accordingly.
302
+ # @param [CSV::Row] row the CSV Row being parsed
303
+ # @param [String] insert_str the left side of the insert statement (i.e. columns)
304
+ # @param [String] values_str the right side of the insert statement (i.e. values)
305
+ # @param [Hash] opt options Hash will be passed through to #parse_datetime.
306
+ # @option opt [String] :name the name of the field being parsed. Required.
307
+ # @return [void]
308
+ # @raise [ArgumentError] :name is required.
309
+ # @see parse_datetime
310
+ # @see mutate_sql_stmt!
311
+ def self.parse_datetime!(row, insert_str, values_str, opt={})
312
+ raise ArgumentError, "opt[:name] is required!" unless opt[:name]
313
+ datetime = parse_datetime( row[ to_symbol( opt[:name] ) ], opt )
314
+ mutate_sql_stmt! insert_str, opt[:name], values_str, datetime
315
+ end
316
+
317
+ # Parse a SQL timestamp from a String.
318
+ # @param [String] str the String to be parsed.
319
+ # @param [Hash] opt options Hash will be passed through to #parse_string.
320
+ # @return [String] the parsed timestamp. nil or empty inputs will return '' by default.
321
+ # @see parse_string
322
+ def self.parse_timestamp(str, opt={ valid_values: /^$|(\d{4}\-\d{2}\-\d{2}){1}\s(\d{2}:\d{2}:\d{2})?/ })
323
+ return parse_datetime str, opt
324
+ end
325
+
326
+ # Helper method for #parse_timestamp which finds the value by column :name and mutates the SQL statement accordingly.
327
+ # @param [CSV::Row] row the CSV Row being parsed
328
+ # @param [String] insert_str the left side of the insert statement (i.e. columns)
329
+ # @param [String] values_str the right side of the insert statement (i.e. values)
330
+ # @param [Hash] opt options Hash will be passed through to #parse_timestamp.
331
+ # @option opt [String] :name the name of the field being parsed. Required.
332
+ # @return [void]
333
+ # @raise [ArgumentError] :name is required.
334
+ # @see parse_timestamp
335
+ # @see mutate_sql_stmt!
336
+ def self.parse_timestamp!(row, insert_str, values_str, opt={})
337
+ raise ArgumentError, "opt[:name] is required!" unless opt[:name]
338
+ timestamp = parse_datetime( row[ to_symbol( opt[:name] ) ], opt )
339
+ mutate_sql_stmt! insert_str, opt[:name], values_str, timestamp
340
+ end
341
+
265
342
  # Useful for parsing "flag" like values; i.e. usually single characters.
266
343
  # @param [String] str the String to be parsed.
267
344
  # @param [Hash] opt options Hash will be passed through to #parse_string.
@@ -16,5 +16,5 @@
16
16
 
17
17
  module Rsmart
18
18
  # The gem version number.
19
- VERSION = "0.12"
19
+ VERSION = "0.13"
20
20
  end
@@ -444,4 +444,78 @@ RSpec.describe "Rsmart::ETL" do
444
444
  end
445
445
  end
446
446
 
447
+ describe "#parse_date" do
448
+ it "parses valid date strings" do
449
+ expect(ETL.parse_date("2014-10-31")).to eq("2014-10-31")
450
+ expect(ETL.parse_date(nil)).to eq('')
451
+ expect(ETL.parse_date('')).to eq('')
452
+ end
453
+
454
+ it "raises a TextParseError if invalid dates are supplied" do
455
+ expect { ETL.parse_date("1-1-1") }.to raise_error(TextParseError)
456
+ expect { ETL.parse_date("123") }.to raise_error(TextParseError)
457
+ expect { ETL.parse_date("Q") }.to raise_error(TextParseError)
458
+ end
459
+ end
460
+
461
+ describe "#parse_date!" do
462
+ it "modifies the insert_str and values_str based on a CSV::Row match" do
463
+ insert_str = ""; values_str = ""; name = "INCORPORATED_DATE"
464
+ row = CSV::Row.new([name.downcase.to_sym], ['2014-10-31'], true)
465
+ ETL.parse_date!(row, insert_str, values_str, name: name)
466
+ expect(insert_str).to eq "#{name},"
467
+ expect(values_str).to eq "'2014-10-31',"
468
+ end
469
+ end
470
+
471
+ describe "#parse_datetime" do
472
+ it "parses valid datetime strings" do
473
+ expect(ETL.parse_datetime("2014-10-31 00:00:00")).to eq("2014-10-31 00:00:00")
474
+ expect(ETL.parse_datetime("1970-10-31 10:10:10")).to eq("1970-10-31 10:10:10")
475
+ expect(ETL.parse_datetime(nil)).to eq('')
476
+ expect(ETL.parse_datetime('')).to eq('')
477
+ end
478
+
479
+ it "raises a TextParseError if invalid dates are supplied" do
480
+ expect { ETL.parse_datetime("1970-10-31") }.to raise_error(TextParseError)
481
+ expect { ETL.parse_datetime("123") }.to raise_error(TextParseError)
482
+ expect { ETL.parse_datetime("Q") }.to raise_error(TextParseError)
483
+ end
484
+ end
485
+
486
+ describe "#parse_datetime!" do
487
+ it "modifies the insert_str and values_str based on a CSV::Row match" do
488
+ insert_str = ""; values_str = ""; name = "INCORPORATED_DATE"
489
+ row = CSV::Row.new([name.downcase.to_sym], ['1970-10-31 10:10:10'], true)
490
+ ETL.parse_datetime!(row, insert_str, values_str, name: name)
491
+ expect(insert_str).to eq "#{name},"
492
+ expect(values_str).to eq "'1970-10-31 10:10:10',"
493
+ end
494
+ end
495
+
496
+ describe "#parse_timestamp" do
497
+ it "parses valid datetime strings" do
498
+ expect(ETL.parse_timestamp("2014-10-31 00:00:00")).to eq("2014-10-31 00:00:00")
499
+ expect(ETL.parse_timestamp("1970-10-31 10:10:10")).to eq("1970-10-31 10:10:10")
500
+ expect(ETL.parse_timestamp(nil)).to eq('')
501
+ expect(ETL.parse_timestamp('')).to eq('')
502
+ end
503
+
504
+ it "raises a TextParseError if invalid dates are supplied" do
505
+ expect { ETL.parse_timestamp("1970-10-31") }.to raise_error(TextParseError)
506
+ expect { ETL.parse_timestamp("123") }.to raise_error(TextParseError)
507
+ expect { ETL.parse_timestamp("Q") }.to raise_error(TextParseError)
508
+ end
509
+ end
510
+
511
+ describe "#parse_timestamp!" do
512
+ it "modifies the insert_str and values_str based on a CSV::Row match" do
513
+ insert_str = ""; values_str = ""; name = "INCORPORATED_DATE"
514
+ row = CSV::Row.new([name.downcase.to_sym], ['1970-10-31 10:10:10'], true)
515
+ ETL.parse_timestamp!(row, insert_str, values_str, name: name)
516
+ expect(insert_str).to eq "#{name},"
517
+ expect(values_str).to eq "'1970-10-31 10:10:10',"
518
+ end
519
+ end
520
+
447
521
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rsmart_toolbox
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.12'
4
+ version: '0.13'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lance Speelmon
@@ -30,7 +30,7 @@ cert_chain:
30
30
  sKRWzEtHFamxQaIspOja5O4oQKiCbWa90fEuIoCtwyy1rQtL9VKoDTs4vZASXNuc
31
31
  F/lEyekXSjN36uTtlt4LkKLn/k7k5gRbt4+C9Q==
32
32
  -----END CERTIFICATE-----
33
- date: 2014-10-15 00:00:00.000000000 Z
33
+ date: 2014-10-30 00:00:00.000000000 Z
34
34
  dependencies:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: builder
metadata.gz.sig CHANGED
Binary file