timetwister 0.3.0 → 0.4.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
  SHA256:
3
- metadata.gz: 9ab710e9b2068bc7945925206c7033765c9ab6c8c03a0dec28e8271e1888d322
4
- data.tar.gz: 8d37aba572fc43b434b5da1febff19e77d12155a744787aaada2a98d2748a262
3
+ metadata.gz: fc36713ba664a945f35f2174a7047646118b90e40ba78edede43e8a4efe891f8
4
+ data.tar.gz: b6861861782dae33194e0e4d601d316f6246b14baacb73e0b4ebe31f7007c007
5
5
  SHA512:
6
- metadata.gz: a8ab93f028d366e5dfd48a25c72768796eac9d586b6d03e698eb7e58022b37d9862577a7cccd5425f9749091167481e17d20cbe1197904bb6209602e262eae94
7
- data.tar.gz: 780ee2a7bb4575a385dcbf4d0a417e671f1da7d700115a547391546303ae189b7b96301268fecfeb8c16d088aca883cd100672f51167c8b7366651df665b8c0b
6
+ metadata.gz: 7880c0927a7a48d0cba0ac9dc52058fa3def90b986201d553fc0df9f288c7bd72c0fb2e71731892733db6ff138f1630a8edc6264fa4819106dabc7ef6fe181b7
7
+ data.tar.gz: b8d5517e5e2ae6c0457a1335f192e87d5b3b04623b6c04f499d7f98ec880bebb7bef0724fcbafb2ea3d4bd9745410bca5e216973bff95ba5ee8d8fa16daa14e4
@@ -0,0 +1,35 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+ strategy:
21
+ matrix:
22
+ ruby-version: ['2.3', '2.4', '2.5', '2.6', '2.7', '3.0']
23
+
24
+ steps:
25
+ - uses: actions/checkout@v3
26
+ - name: Set up Ruby
27
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
28
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
29
+ # uses: ruby/setup-ruby@v1
30
+ uses: ruby/setup-ruby@v1
31
+ with:
32
+ ruby-version: ${{ matrix.ruby-version }}
33
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
34
+ - name: Run tests
35
+ run: bundle exec rspec
@@ -56,7 +56,17 @@ class Parser
56
56
  if c[:proc]
57
57
  # clone string to avoid changing it via in-place methods used in Procs
58
58
  work_string = @string.clone
59
- c[:proc].call(work_string, c[:arg])
59
+
60
+ # using a begin/rescue to catch dates that Date.parse errors on
61
+ begin
62
+ c[:proc].call(work_string, c[:arg])
63
+
64
+ # ArgumentError for pre-2.7.0 DateTime errors,
65
+ # Date::Error for 2.7.0 and later
66
+ rescue ArgumentError || Date::Error
67
+ @dates = { :original_string => @string, :index_dates => [], :keydate => nil, :keydate_z => nil, :date_start => nil, :date_end => nil,
68
+ :date_start_full => nil, :date_end_full => nil, :inclusive_range => nil, :certainty => nil }
69
+ end
60
70
  end
61
71
  break
62
72
  end
@@ -236,11 +236,7 @@ class Utilities
236
236
  ]
237
237
 
238
238
  # transform seasons to months
239
- string.gsub!(/[Ww]inter/, " January 1 - March 20 ")
240
- string.gsub!(/[Ss]pring/, " March 20 - June 21 ")
241
- string.gsub!(/[Ss]ummer/, " June 21 - September 23 ")
242
- string.gsub!(/[Aa]utumn/, " September 23 - December 22 ")
243
- string.gsub!(/[Ff]all/, " September 23 - December 22 ")
239
+ string = replace_seasons(string)
244
240
 
245
241
  # remove days of the week
246
242
  dow = [/[Ss]unday,?\s+/, /[Mm]onday,?\s+/, /[Tt]uesday,?\s+/, /[Ww]ednesday,?\s+/, /[Tt]hursday,?\s+/, /[Ff]riday,?\s+/, /[Ss]aturday,?\s+/]
@@ -258,6 +254,28 @@ class Utilities
258
254
  return string
259
255
  end
260
256
 
257
+ def self.replace_seasons(string)
258
+ seasons = { 'win[^\s]*' => ['January 1', 'March 20'],
259
+ 'spr[^\s]*' => ['March 20', 'June 21'],
260
+ 'sum[^\s]*' => ['June 21', 'September 23'],
261
+ 'aut[^\s]*' => ['September 23', 'December 31'],
262
+ 'fal[^\s]*' => ['September 23', 'December 31']}
263
+
264
+ # if we're working with a range, we need to be a little careful, so that we don't create sub-ranges in a string
265
+ is_range = string.match(/[^-]+?-[^-]+?/)
266
+
267
+ seasons.each do |season, dates|
268
+ regex = Regexp.new(season, Regexp::IGNORECASE)
269
+
270
+ # is the season the beginning or the end of a range?
271
+ new_date = (is_range ? (string.match(Regexp.new(season + '.+?-', Regexp::IGNORECASE)) ? dates[0] : dates[1]) : dates.join(' - '))
272
+
273
+ string = string.gsub(regex, new_date)
274
+ end
275
+
276
+ return string
277
+ end
278
+
261
279
  # Removes the first 4-digit number found in the string and returns it
262
280
  def self.extract_year(string)
263
281
  year = string.match(/\d{4}/).to_s
@@ -1,3 +1,3 @@
1
1
  module Timetwister
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/spec/dates_spec.rb CHANGED
@@ -46,7 +46,6 @@ describe Timetwister do
46
46
  end
47
47
 
48
48
  it "parses ranges of full dates" do
49
-
50
49
  forms = ["July 4 1776 - March 1 1789", "4 July 1776 - 1 March 1789", "1776 July 4 - 1789 March 1", "1776 4 July - 1789 1 March", "1776 4 July to 1789 1 March"]
51
50
  forms.each do |f|
52
51
  date = Timetwister.parse(f)
@@ -344,7 +343,7 @@ describe Timetwister do
344
343
  end
345
344
  end
346
345
 
347
- it "parses seasons" do
346
+ it "parses single seasons" do
348
347
  date = Timetwister.parse("Winter 1776")
349
348
  expect(date[0][:date_start]).to eq("1776-01-01")
350
349
  expect(date[0][:date_start_full]).to eq("1776-01-01")
@@ -354,6 +353,15 @@ describe Timetwister do
354
353
  expect(date[0][:index_dates]).to eq([1776])
355
354
  expect(date[0][:test_data]).to eq("310")
356
355
 
356
+ date = Timetwister.parse("win. 1776")
357
+ expect(date[0][:date_start]).to eq("1776-01-01")
358
+ expect(date[0][:date_start_full]).to eq("1776-01-01")
359
+ expect(date[0][:date_end]).to eq("1776-03-20")
360
+ expect(date[0][:date_end_full]).to eq("1776-03-20")
361
+ expect(date[0][:inclusive_range]).to eq(true)
362
+ expect(date[0][:index_dates]).to eq([1776])
363
+ expect(date[0][:test_data]).to eq("310")
364
+
357
365
  date = Timetwister.parse("1776 Spring")
358
366
  expect(date[0][:date_start]).to eq("1776-03-20")
359
367
  expect(date[0][:date_start_full]).to eq("1776-03-20")
@@ -363,6 +371,15 @@ describe Timetwister do
363
371
  expect(date[0][:index_dates]).to eq([1776])
364
372
  expect(date[0][:test_data]).to eq("310")
365
373
 
374
+ date = Timetwister.parse("1776 Spr")
375
+ expect(date[0][:date_start]).to eq("1776-03-20")
376
+ expect(date[0][:date_start_full]).to eq("1776-03-20")
377
+ expect(date[0][:date_end]).to eq("1776-06-21")
378
+ expect(date[0][:date_end_full]).to eq("1776-06-21")
379
+ expect(date[0][:inclusive_range]).to eq(true)
380
+ expect(date[0][:index_dates]).to eq([1776])
381
+ expect(date[0][:test_data]).to eq("310")
382
+
366
383
  date = Timetwister.parse("1776 Summer")
367
384
  expect(date[0][:date_start]).to eq("1776-06-21")
368
385
  expect(date[0][:date_start_full]).to eq("1776-06-21")
@@ -372,14 +389,61 @@ describe Timetwister do
372
389
  expect(date[0][:index_dates]).to eq([1776])
373
390
  expect(date[0][:test_data]).to eq("310")
374
391
 
392
+ date = Timetwister.parse("1776 Summ")
393
+ expect(date[0][:date_start]).to eq("1776-06-21")
394
+ expect(date[0][:date_start_full]).to eq("1776-06-21")
395
+ expect(date[0][:date_end]).to eq("1776-09-23")
396
+ expect(date[0][:date_end_full]).to eq("1776-09-23")
397
+ expect(date[0][:inclusive_range]).to eq(true)
398
+ expect(date[0][:index_dates]).to eq([1776])
399
+ expect(date[0][:test_data]).to eq("310")
400
+
375
401
  date = Timetwister.parse("Fall 1776")
376
402
  expect(date[0][:date_start]).to eq("1776-09-23")
377
403
  expect(date[0][:date_start_full]).to eq("1776-09-23")
378
- expect(date[0][:date_end]).to eq("1776-12-22")
379
- expect(date[0][:date_end_full]).to eq("1776-12-22")
404
+ expect(date[0][:date_end]).to eq("1776-12-31")
405
+ expect(date[0][:date_end_full]).to eq("1776-12-31")
406
+ expect(date[0][:inclusive_range]).to eq(true)
407
+ expect(date[0][:index_dates]).to eq([1776])
408
+ expect(date[0][:test_data]).to eq("310")
409
+
410
+ date = Timetwister.parse("Autumn 1776")
411
+ expect(date[0][:date_start]).to eq("1776-09-23")
412
+ expect(date[0][:date_start_full]).to eq("1776-09-23")
413
+ expect(date[0][:date_end]).to eq("1776-12-31")
414
+ expect(date[0][:date_end_full]).to eq("1776-12-31")
380
415
  expect(date[0][:inclusive_range]).to eq(true)
381
416
  expect(date[0][:index_dates]).to eq([1776])
382
417
  expect(date[0][:test_data]).to eq("310")
418
+
419
+ date = Timetwister.parse("Aut. 1776")
420
+ expect(date[0][:date_start]).to eq("1776-09-23")
421
+ expect(date[0][:date_start_full]).to eq("1776-09-23")
422
+ expect(date[0][:date_end]).to eq("1776-12-31")
423
+ expect(date[0][:date_end_full]).to eq("1776-12-31")
424
+ expect(date[0][:inclusive_range]).to eq(true)
425
+ expect(date[0][:index_dates]).to eq([1776])
426
+ expect(date[0][:test_data]).to eq("310")
427
+ end
428
+
429
+ it "parses ranges of seasons" do
430
+ date = Timetwister.parse("Winter 1776 - Spring 1777")
431
+ expect(date[0][:date_start]).to eq("1776-01-01")
432
+ expect(date[0][:date_start_full]).to eq("1776-01-01")
433
+ expect(date[0][:date_end]).to eq("1777-06-21")
434
+ expect(date[0][:date_end_full]).to eq("1777-06-21")
435
+ expect(date[0][:inclusive_range]).to eq(true)
436
+ expect(date[0][:index_dates]).to eq([1776, 1777])
437
+ expect(date[0][:test_data]).to eq("80")
438
+
439
+ date = Timetwister.parse("1777 Spring - 1778 Summer")
440
+ expect(date[0][:date_start]).to eq("1777-03-20")
441
+ expect(date[0][:date_start_full]).to eq("1777-03-20")
442
+ expect(date[0][:date_end]).to eq("1778-09-23")
443
+ expect(date[0][:date_end_full]).to eq("1778-09-23")
444
+ expect(date[0][:inclusive_range]).to eq(true)
445
+ expect(date[0][:index_dates]).to eq([1777, 1778])
446
+ expect(date[0][:test_data]).to eq("80")
383
447
  end
384
448
 
385
449
  it "parses dates with punctuation" do
@@ -507,6 +571,62 @@ describe Timetwister do
507
571
  end
508
572
  end
509
573
 
574
+ it "DOES NOT parse nonexistent dates" do
575
+ date = Timetwister.parse('1776 September 31')
576
+ expect(date[0][:date_start]).to eq(nil)
577
+ expect(date[0][:date_start_full]).to eq(nil)
578
+ expect(date[0][:date_end]).to eq(nil)
579
+ expect(date[0][:date_end_full]).to eq(nil)
580
+ expect(date[0][:inclusive_range]).to eq(nil)
581
+ expect(date[0][:index_dates]).to eq([])
582
+ expect(date[0][:test_data]).to eq(nil)
583
+
584
+ date = Timetwister.parse('1776-11-31')
585
+ expect(date[0][:date_start]).to eq(nil)
586
+ expect(date[0][:date_start_full]).to eq(nil)
587
+ expect(date[0][:date_end]).to eq(nil)
588
+ expect(date[0][:date_end_full]).to eq(nil)
589
+ expect(date[0][:inclusive_range]).to eq(nil)
590
+ expect(date[0][:index_dates]).to eq([])
591
+ expect(date[0][:test_data]).to eq(nil)
592
+
593
+ date = Timetwister.parse('1776-11-00')
594
+ expect(date[0][:date_start]).to eq(nil)
595
+ expect(date[0][:date_start_full]).to eq(nil)
596
+ expect(date[0][:date_end]).to eq(nil)
597
+ expect(date[0][:date_end_full]).to eq(nil)
598
+ expect(date[0][:inclusive_range]).to eq(nil)
599
+ expect(date[0][:index_dates]).to eq([])
600
+ expect(date[0][:test_data]).to eq(nil)
601
+
602
+ date = Timetwister.parse('1776 whatever 31')
603
+ expect(date[0][:date_start]).to eq(nil)
604
+ expect(date[0][:date_start_full]).to eq(nil)
605
+ expect(date[0][:date_end]).to eq(nil)
606
+ expect(date[0][:date_end_full]).to eq(nil)
607
+ expect(date[0][:inclusive_range]).to eq(nil)
608
+ expect(date[0][:index_dates]).to eq([])
609
+ expect(date[0][:test_data]).to eq(nil)
610
+
611
+ date = Timetwister.parse('1776 sometime 72 - december 42 1999')
612
+ expect(date[0][:date_start]).to eq(nil)
613
+ expect(date[0][:date_start_full]).to eq(nil)
614
+ expect(date[0][:date_end]).to eq(nil)
615
+ expect(date[0][:date_end_full]).to eq(nil)
616
+ expect(date[0][:inclusive_range]).to eq(nil)
617
+ expect(date[0][:index_dates]).to eq([])
618
+ expect(date[0][:test_data]).to eq(nil)
619
+
620
+ date = Timetwister.parse('just absolute garbage')
621
+ expect(date[0][:date_start]).to eq(nil)
622
+ expect(date[0][:date_start_full]).to eq(nil)
623
+ expect(date[0][:date_end]).to eq(nil)
624
+ expect(date[0][:date_end_full]).to eq(nil)
625
+ expect(date[0][:inclusive_range]).to eq(nil)
626
+ expect(date[0][:index_dates]).to eq([])
627
+ expect(date[0][:test_data]).to eq(nil)
628
+ end
629
+
510
630
  it "parses lists of years" do
511
631
  date = Timetwister.parse("1776, 1789, 1812")
512
632
  expect(date[0][:date_start]).to eq("1776")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timetwister
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Duryee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-08 00:00:00.000000000 Z
11
+ date: 2023-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -46,6 +46,7 @@ executables:
46
46
  extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
+ - ".github/workflows/ruby.yml"
49
50
  - ".gitignore"
50
51
  - ".travis.yml"
51
52
  - Gemfile