turba_chronos 5.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.ruby-version +1 -0
  4. data/Gemfile +4 -0
  5. data/Guardfile +6 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +68 -0
  8. data/Rakefile +11 -0
  9. data/lib/turba_chronos.rb +31 -0
  10. data/lib/turba_chronos/core_ext/range.rb +5 -0
  11. data/lib/turba_chronos/day.rb +71 -0
  12. data/lib/turba_chronos/period.rb +44 -0
  13. data/lib/turba_chronos/period_converter.rb +69 -0
  14. data/lib/turba_chronos/periods.rb +43 -0
  15. data/lib/turba_chronos/rule.rb +52 -0
  16. data/lib/turba_chronos/stored_periods.rb +68 -0
  17. data/lib/turba_chronos/timeline.rb +139 -0
  18. data/lib/turba_chronos/version.rb +3 -0
  19. data/spec/fixtures/base_fixture.rb +31 -0
  20. data/spec/fixtures/disney_fixture.rb +19 -0
  21. data/spec/fixtures/empty_periods_fixture.rb +13 -0
  22. data/spec/fixtures/future_reopen_fixture.rb +19 -0
  23. data/spec/fixtures/lcl_fixture.rb +18 -0
  24. data/spec/fixtures/november_bug_fixture.rb +13 -0
  25. data/spec/fixtures/opened_fixture.rb +12 -0
  26. data/spec/lib/alt_timeline_spec.rb +48 -0
  27. data/spec/lib/disney_2_spec.rb +21 -0
  28. data/spec/lib/disney_3_spec.rb +21 -0
  29. data/spec/lib/disney_spec.rb +21 -0
  30. data/spec/lib/empty_periods_spec.rb +21 -0
  31. data/spec/lib/future_reopen_spec.rb +21 -0
  32. data/spec/lib/lcl_spec.rb +28 -0
  33. data/spec/lib/november_bug_spec.rb +20 -0
  34. data/spec/lib/opened_spec.rb +21 -0
  35. data/spec/lib/timeline_spec.rb +70 -0
  36. data/spec/spec_helper.rb +8 -0
  37. data/test/fixtures/base_fixture.rb +31 -0
  38. data/test/test_helper.rb +6 -0
  39. data/test/test_periods.rb +16 -0
  40. data/test/test_periods_converter.rb +62 -0
  41. data/test/test_rule.rb +33 -0
  42. data/test/test_timeline.rb +51 -0
  43. data/turba_chronos.gemspec +30 -0
  44. metadata +236 -0
@@ -0,0 +1,21 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../fixtures/disney_fixture'
3
+
4
+ describe TurbaChronos::Timeline do
5
+ before(:all) do
6
+ Timecop.freeze(Time.zone.parse("November 02 2014, 14:30"))
7
+ from = Time.zone.parse("November 02 2014, 00:00").iso8601
8
+ to = Time.zone.parse("November 03 2014, 23:59").iso8601
9
+ @subject = TurbaChronos.load(disney.periods, from, to)
10
+ end
11
+
12
+ after(:all) do
13
+ Timecop.return
14
+ end
15
+
16
+ describe '.days' do
17
+ it "returns the days" do
18
+ expect(@subject.days[1].timeframes[1].level).to eql(1)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../fixtures/empty_periods_fixture'
3
+
4
+ describe TurbaChronos::Timeline do
5
+ before(:all) do
6
+ Timecop.freeze(Time.zone.parse("November 02 2014, 14:30").iso8601)
7
+ from = Time.zone.parse("November 01 2014, 00:00").iso8601
8
+ to = Time.zone.parse("November 02 2014, 23:59").iso8601
9
+ @subject = TurbaChronos.load(empty_periods.periods, from, to)
10
+ end
11
+
12
+ after(:all) do
13
+ Timecop.return
14
+ end
15
+
16
+ describe '.days' do
17
+ it "returns the days" do
18
+ expect(@subject.days.first.timeframes.first.timerange.to_s).to eql('0..2359')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../fixtures/future_reopen_fixture'
3
+
4
+ describe TurbaChronos::Timeline do
5
+ before(:all) do
6
+ Timecop.freeze(Time.zone.parse("July 30 2014, 16:15").iso8601)
7
+ @subject = TurbaChronos.load(future_reopen.periods, Time.now.utc.iso8601, (Time.now.utc + 3.days).iso8601)
8
+ end
9
+
10
+ after(:all) do
11
+ Timecop.return
12
+ end
13
+
14
+ describe '.reopen' do
15
+ it "returns when it's reopen" do
16
+ expect(@subject.reopen.date.to_s).to eql("2014-08-13")
17
+ expect(@subject.reopen.timeframe.timerange.to_s).to eql("800..1000")
18
+ expect(@subject.reopen.timeframe.level).to eql(2)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,28 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../fixtures/lcl_fixture'
3
+
4
+ describe TurbaChronos::Timeline do
5
+ before(:all) do
6
+ Timecop.freeze(Time.zone.parse("September 23 2014, 14:30").iso8601)
7
+ @subject = TurbaChronos.load(lcl.periods, Time.now.utc.iso8601, (Time.now.utc + 3.days).iso8601)
8
+ end
9
+
10
+ after(:all) do
11
+ Timecop.return
12
+ end
13
+
14
+ describe '.days' do
15
+ it "returns the days" do
16
+ expect(@subject.days.first.timeframes[0].level).to eql(3)
17
+ expect(@subject.days.first.timeframes[1].level).to eql(1)
18
+ end
19
+ end
20
+
21
+ describe '.reopen' do
22
+ it "returns when it's reopen" do
23
+ expect(@subject.reopen.date.to_s).to eql("2014-09-23")
24
+ expect(@subject.reopen.timeframe.timerange).to eql(1700..1730)
25
+ expect(@subject.reopen.timeframe.level).to eql(1)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,20 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../fixtures/november_bug_fixture'
3
+
4
+ describe TurbaChronos::Timeline do
5
+ before(:all) do
6
+ Timecop.freeze(Time.zone.parse("November 24 2014, 14:30").iso8601)
7
+ @subject = TurbaChronos.load(november_bug.periods, Time.now.utc.iso8601, (Time.now.utc + 3.days).iso8601)
8
+ end
9
+
10
+ after(:all) do
11
+ Timecop.return
12
+ end
13
+
14
+ describe '.current_timeframe' do
15
+ it "returns the current_timeframe" do
16
+ expect(@subject.current_timeframe.level).to eql(1)
17
+ # expect(@subject.current_timeframe.timerange).to eql((1200..1600))
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../fixtures/opened_fixture'
3
+
4
+ describe TurbaChronos::Timeline do
5
+ before(:all) do
6
+ Timecop.freeze(Time.zone.parse("November 02 2014, 14:30").iso8601)
7
+ from = Time.zone.parse("November 02 2014, 00:00").iso8601
8
+ to = Time.zone.parse("November 02 2014, 23:59").iso8601
9
+ @subject = TurbaChronos.load(opened.periods, from, to)
10
+ end
11
+
12
+ after(:all) do
13
+ Timecop.return
14
+ end
15
+
16
+ describe '.days' do
17
+ it "returns the days" do
18
+ expect(@subject.days.count).to eql(1)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,70 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../fixtures/base_fixture'
3
+
4
+ describe TurbaChronos::Timeline do
5
+ before(:all) do
6
+ Timecop.freeze(Time.zone.parse("July 30 2014, 16:15").iso8601)
7
+ @subject = TurbaChronos.load(base.periods, Time.now.utc.iso8601, (Time.now.utc + 3.days).iso8601)
8
+ end
9
+
10
+ after(:all) do
11
+ Timecop.return
12
+ end
13
+
14
+ describe '.business_timeranges' do
15
+ it "returns the business hours timeranges for days" do
16
+ expect(@subject.business_timeranges).to eql([["08:00", "10:00"], ["10:00", "17:00"]])
17
+ end
18
+ end
19
+
20
+ describe '.timeranges_for_days' do
21
+ it "returns the timeranges for days" do
22
+ expect(@subject.timeranges_for_days[:timeranges].to_s).to eql("[0..800, 800..1000, 1000..1700, 1700..2359]")
23
+ end
24
+ end
25
+
26
+ describe '.current_day' do
27
+ it "returns the current day" do
28
+ day = @subject.current_day
29
+ expect(day.beginning_of_day.to_s).to eql("2014-07-30")
30
+ end
31
+ end
32
+
33
+ describe '.current_timeframe' do
34
+ it "returns the current timeframe" do
35
+ # expect(@subject.current_timeframe.timerange).to eql((1000..1700))
36
+ expect(@subject.current_timeframe.level).to eql(1)
37
+ end
38
+ end
39
+
40
+ describe '.opened?' do
41
+ it "returns if it's opened" do
42
+ expect(@subject.opened?).to eql(true)
43
+ end
44
+ end
45
+
46
+ describe '.reopen' do
47
+ it "returns next opened timeframe" do
48
+ expect(@subject.reopen.date.to_s).to eql("2014-07-31")
49
+ expect(@subject.reopen.timeframe.timerange.to_s).to eql("800..1000")
50
+ expect(@subject.reopen.timeframe.level).to eql(1)
51
+ end
52
+ end
53
+
54
+ describe '.next' do
55
+ it "returns the three next timeframes" do
56
+ next_timeframes = @subject.next
57
+ expect(next_timeframes[0].date.to_s).to eql("2014-07-31")
58
+ expect(next_timeframes[0].timeframe.timerange.to_s).to eql("800..1000")
59
+ expect(next_timeframes[0].timeframe.level).to eql(1)
60
+
61
+ expect(next_timeframes[1].date.to_s).to eql("2014-07-31")
62
+ expect(next_timeframes[1].timeframe.timerange.to_s).to eql("1000..1700")
63
+ expect(next_timeframes[1].timeframe.level).to eql(1)
64
+
65
+ expect(next_timeframes[2].date.to_s).to eql("2014-08-01")
66
+ expect(next_timeframes[2].timeframe.timerange.to_s).to eql("800..1000")
67
+ expect(next_timeframes[2].timeframe.level).to eql(2)
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_support'
2
+ require_relative '../lib/turba_chronos'
3
+
4
+ require 'rspec'
5
+ require 'timecop'
6
+ require 'ostruct'
7
+
8
+ Time.zone = 'Europe/Paris'
@@ -0,0 +1,31 @@
1
+ def base_fixture
2
+ crowd = OpenStruct.new
3
+ crowd.periods = [
4
+ {
5
+ dates: '01/01:31/12',
6
+ rules: [
7
+ ['08:00', '10:00', 1, 2, 1, 1, 6, 6, 6],
8
+ ['10:00', '17:00', 1, 1, 1, 1, 6, 6, 6]
9
+ ]
10
+ },
11
+ {
12
+ dates: '18/05,19/05:20/05,21/05:21/05',
13
+ rules: [
14
+ ['06:00', '15:00', 3, 3, 3, 3, 3, 3, 3]
15
+ ]
16
+ },
17
+ {
18
+ dates: '01/05,25/08:28/08',
19
+ rules: [
20
+ ['08:00', '10:00', 6, 6, 6, 6, 6, 6, 6]
21
+ ]
22
+ },
23
+ {
24
+ dates: 'august,vacances_toussaint,vacances_noel',
25
+ rules: [
26
+ ['08:00', '10:00', 2, 2, 2, 2, 2, 2, 2]
27
+ ]
28
+ }
29
+ ]
30
+ crowd
31
+ end
@@ -0,0 +1,6 @@
1
+ require 'timecop'
2
+ require 'minitest/autorun'
3
+ require 'minitest/reporters'
4
+ require 'turba_chronos'
5
+
6
+ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+ require 'fixtures/base_fixture'
3
+
4
+ class TestPeriods < Minitest::Test
5
+
6
+ def setup
7
+ Time.zone = "Europe/Paris"
8
+ new_time = Time.zone.local(2015, 6, 8, 12, 25, 0)
9
+ Timecop.freeze(new_time)
10
+ @periods = TurbaChronos::Periods.new(base_fixture.periods)
11
+ end
12
+
13
+ def test_raw_periods
14
+ assert_equal @periods.raw_periods.count, 10
15
+ end
16
+ end
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+ require 'fixtures/base_fixture'
3
+
4
+ if ENV['BENCH']
5
+ require 'minitest/benchmark'
6
+
7
+ class TestPeriodConverterBench < Minitest::Benchmark
8
+ def bench_parser
9
+ Time.zone = "Europe/Paris"
10
+ assert_performance_constant 0.9999 do |n|
11
+ period = TurbaChronos::PeriodConverter.new('25/08:28/08')
12
+ period.parse
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ class TestPeriodConverter < Minitest::Test
19
+ def setup
20
+ Time.zone = "Europe/Paris"
21
+ end
22
+
23
+ def test_two_dates
24
+ period = TurbaChronos::PeriodConverter.new('25/08:28/08')
25
+ assert_equal period.parse.first.from.day, 25
26
+ assert_equal period.parse.first.from.month, 8
27
+ assert_equal period.parse.first.to.day, 28
28
+ assert_equal period.parse.first.to.month, 8
29
+ end
30
+
31
+ def test_one_date
32
+ period = TurbaChronos::PeriodConverter.new('25/08')
33
+ assert_equal period.parse.first.from.day, 25
34
+ assert_equal period.parse.first.from.month, 8
35
+ assert_equal period.parse.first.to.day, 25
36
+ assert_equal period.parse.first.to.month, 8
37
+ end
38
+
39
+ def test_one_month
40
+ period = TurbaChronos::PeriodConverter.new('august')
41
+ assert_equal period.parse.first.from.day, 1
42
+ assert_equal period.parse.first.from.month, 8
43
+ assert_equal period.parse.first.to.day, 31
44
+ assert_equal period.parse.first.to.month, 8
45
+ end
46
+
47
+ def test_two_months
48
+ period = TurbaChronos::PeriodConverter.new('august-october')
49
+ assert_equal period.parse.first.from.day, 1
50
+ assert_equal period.parse.first.from.month, 8
51
+ assert_equal period.parse.first.to.day, 31
52
+ assert_equal period.parse.first.to.month, 10
53
+ end
54
+
55
+ def test_stored_dates
56
+ period = TurbaChronos::PeriodConverter.new('ponts_mai')
57
+ assert_equal period.parse.first.from.day, 1
58
+ assert_equal period.parse.first.from.month, 5
59
+ assert_equal period.parse.first.to.day, 3
60
+ assert_equal period.parse.first.to.month, 5
61
+ end
62
+ end
data/test/test_rule.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'test_helper'
2
+
3
+ class TestRule < Minitest::Test
4
+ def setup
5
+ @rule = TurbaChronos::Rule.new(['06:00', '15:00', 1, 3, 3, 3, 3, 3, 6])
6
+ end
7
+
8
+ def test_min
9
+ assert_equal @rule.min, 600
10
+ end
11
+
12
+ def test_max
13
+ assert_equal @rule.max, 1500
14
+ end
15
+
16
+ def test_start_time
17
+ assert_equal @rule.start_time.text, "06:00"
18
+ assert_equal @rule.start_time.integer, 600
19
+ end
20
+
21
+ def test_end_time
22
+ assert_equal @rule.end_time.text, "15:00"
23
+ assert_equal @rule.end_time.integer, 1500
24
+ end
25
+
26
+ def test_range
27
+ assert_equal @rule.range, 600..1500
28
+ end
29
+
30
+ def test_wdays
31
+ assert_equal @rule.wdays, [1, 3, 3, 3, 3, 3, 6]
32
+ end
33
+ end
@@ -0,0 +1,51 @@
1
+ require 'test_helper'
2
+ require 'fixtures/base_fixture'
3
+
4
+ class TestTimeline < Minitest::Test
5
+ def setup
6
+ Time.zone = "Europe/Paris"
7
+ new_time = Time.zone.local(2015, 6, 8, 12, 25, 0)
8
+ Timecop.freeze(new_time)
9
+
10
+ from = Time.zone.parse("June 08 2015, 00:00").iso8601
11
+ to = Time.zone.parse("June 14 2015, 23:59").iso8601
12
+ TurbaChronos.current_numeric_time = Time.now.utc.strftime("%k%M").to_i
13
+ @timeline = TurbaChronos::Timeline.new(base_fixture.periods, Time.zone.parse(from), Time.zone.parse(to))
14
+ end
15
+
16
+ def test_current_timeframe
17
+ assert_equal @timeline.current_timeframe.timerange, (1000..1700)
18
+ assert_equal @timeline.current_timeframe.level, 1
19
+ end
20
+
21
+ def test_timeranges_for_days
22
+ assert_equal @timeline.timeranges_for_days[:timeranges], [0..800, 800..1000, 1000..1700, 1700..2359]
23
+ assert_equal @timeline.timeranges_for_days[:business_timeranges], [800..1000, 1000..1700]
24
+ end
25
+
26
+
27
+ def test_opened?
28
+ assert_equal @timeline.opened?, true
29
+ end
30
+
31
+
32
+ def test_days
33
+ assert_equal @timeline.days.count, 7
34
+ end
35
+
36
+ def test_next
37
+ assert_equal @timeline.next.first.timeframe.level, 2
38
+ end
39
+
40
+ def test_levels
41
+ assert_equal @timeline.levels, [1, 2, 6]
42
+ end
43
+
44
+ # def test_current_level
45
+ # assert_equal @table.current_level, 5
46
+ # end
47
+
48
+ def teardown
49
+ Timecop.return
50
+ end
51
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'turba_chronos/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "turba_chronos"
8
+ spec.version = TurbaChronos::VERSION.dup
9
+ spec.authors = ["Joffrey JAFFEUX"]
10
+ spec.email = ["j.jaffeux@gmail.com"]
11
+ spec.description = %q{Turba Chronos : converts raw periods to full year crowd table}
12
+ spec.summary = %q{Used in leonard}
13
+ spec.homepage = "https://github.com/turba-webservices/turba_chronos"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "timecop"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "rspec-mocks"
26
+ spec.add_development_dependency "guard-rspec"
27
+ spec.add_development_dependency "minitest"
28
+ spec.add_development_dependency "minitest-reporters"
29
+ spec.add_dependency "activesupport"
30
+ end