periodoxical 0.2.2 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 86a786f06e166368721d1f0f17d9c702bfc23950f66482f4c2da80fde583d9a5
4
- data.tar.gz: 65bae069f30ee999ccc99353d03149db0e3414adbb1257b1c163e2d7cfaa5e01
3
+ metadata.gz: d5a035b48977603a07dc33634bd290fab82ccee9844056cea809ea7b1b6dcdd9
4
+ data.tar.gz: 397ac66e80143a549f8ebb51c0ada7cb1e275491d5c91d6444707f06f52a2d3d
5
5
  SHA512:
6
- metadata.gz: e9f76a36b7a77e326ab999adbda7d59f24f84383f198c260e99b76cfb7c9fe069c10fa3ff46c61c53491e4d6b96f8c23b8378471cd7494cce21e78dbfface58e
7
- data.tar.gz: 3ad7d053bb777bf3f3dbf403a787447dc77eeab162d185a6e3de79c21a68d468ac30365aa2ad3d5db65525a2678f78e3f0c5e50e728b22cc521c205445696056
6
+ metadata.gz: ed99e7495d51df3865bc8e555f4b8ccdd259b94a661dd6fe3f257d4fa1196d86c6775faf0fbe1adfb013dba31c2e1cbfd7f2d563ffce56febebc2b01d7077c7e
7
+ data.tar.gz: 1fb4a1626239919706784d2ba638c51d2e0316f69ac8baa4833d9a6a029d15fc6ad6b0f3c52c362655b88bc08f719199b98d03e6d03306f189947df0d941b907
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- periodoxical (0.2.2)
4
+ periodoxical (0.3.2)
5
5
  tzinfo (~> 2.0, >= 2.0.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -23,7 +23,16 @@ Or install it yourself as:
23
23
 
24
24
  ## Usage
25
25
 
26
- Generates datetimes blocks of 9:00AM - 10:30AM and 2:00PM - 2:30PM, on Mondays, Wednesdays, and Thursdays, between the dates of May 23, 2024 and June 24, 2024
26
+ ##### Example 1
27
+ As a Ruby dev, I want to generate all the datetimes blocks of 9:00AM - 10:30AM and 2:00PM - 2:30PM, on Mondays, Wednesdays, and Thursdays, between the dates of May 23, 2024 and June 12, 2024, inclusive. This can be represented visually as:
28
+
29
+ <div align="center">
30
+ <img width="558" alt="Screenshot 2024-05-28 at 10 55 45 PM" src="https://github.com/StevenJL/periodoxical/assets/2191808/e92fc6ff-03fd-44ed-a955-d3a0dd0f5d0a">
31
+ <p><i>(image courtesy of Cal.com)</i></p>
32
+ </div>
33
+
34
+
35
+ <br>
27
36
 
28
37
  ```rb
29
38
  Periodoxical.generate(
@@ -40,37 +49,44 @@ Periodoxical.generate(
40
49
  }
41
50
  ],
42
51
  start_date: Date.parse('2024-05-23'),
43
- end_date: Date.parse('2024-06-24')
52
+ end_date: Date.parse('2024-06-12')
44
53
  )
45
- # returns an array of hashes, each with a :start, :end
54
+ # returns an array of hashes, each with :start and :end keys
46
55
  #=>
47
56
  [
48
57
  {
49
- start: #<DateTime: 2024-05-23T16:00:00+00:00>,
50
- end: #<DateTime: 2024-05-24T05:30:00+00:00>,
58
+ start: #<DateTime: 2024-05-23T09:00:00-0700">,
59
+ end: #<DateTime: 2024-05-23T22:30:00-0700">,
51
60
  },
52
61
  {
53
- start: #<DateTime: 2024-05-23T21:00:00+00:00>,
54
- end: #<DateTime: 2024-05-23T21:30:00+00:00>,
62
+ start: #<DateTime: 2024-05-23T14:00:00-0700>,
63
+ end: #<DateTime: 2024-05-23T14:30:00-0700>,
55
64
  },
56
65
  {
57
- start: #<DateTime: 2024-05-27T16:00:00+00:00>,
58
- end: #<DateTime: 2024-05-28T05:30:00+00:00>
66
+ start: #<DateTime: 2024-05-27T09:00:00-0700>,
67
+ end: #<DateTime: 2024-05-27T22:30:00-0700>
59
68
  },
60
69
  ...
70
+ {
71
+ start: #<DateTime: 2024-06-12T14:00:00-0700>,
72
+ end: #<DateTime: 2024-06-12T14:30:00-0700>
73
+ }
61
74
  ]
62
75
  ```
63
76
 
64
- Generate the next 10 datetime blocks of 3:00PM - 4:30PM, on Sundays, after May 30, 2024
77
+ ##### Example 2 - using the `limit` key.
78
+
79
+ As a ruby dev, I want to generate the next 3 datetime blocks of 9:00AM - 10:30AM and 2:00PM - 2:30PM on Sundays, after May 23, 2024
80
+ using the `limit` key.
65
81
 
66
82
  ```rb
67
83
  Periodoxical.generate(
68
84
  time_zone: 'America/Los_Angeles',
69
- days_of_week: %w[mon wed thu],
85
+ days_of_week: %w[sun],
70
86
  time_blocks: [
71
87
  {
72
88
  start_time: '9:00AM',
73
- end_time: '10:30AM'
89
+ end_time: '10:30PM'
74
90
  },
75
91
  {
76
92
  start_time: '2:00PM',
@@ -78,7 +94,46 @@ Periodoxical.generate(
78
94
  }
79
95
  ],
80
96
  start_date: Date.parse('2024-05-23'),
81
- limit: 10
97
+ limit: 3
98
+ )
99
+ # =>
100
+ [
101
+ {
102
+ start: #<DateTime: 2024-05-26T09:00:00-0700>,
103
+ end: #<DateTime: 2024-05-26T22:30:00-0700>,
104
+ },
105
+ {
106
+ start: #<DateTime: 2024-05-26T14:00:00-0700>,
107
+ end: #<DateTime: 2024-05-26T14:30:00-0700>,
108
+ },
109
+ {
110
+ start: #<DateTime: 2024-06-02T09:00:00-0700>,
111
+ end: #<DateTime: 2024-06-02T22:30:00-0700>,
112
+ },
113
+ ]
114
+ ```
115
+
116
+ ##### Example 3 - when time blocks vary between days
117
+
118
+ As a ruby dev, I want to generate all the timeblocks between May 23, 2024 and June 12, 2024 where the time should be 8AM-9AM on Mondays, but 10:45AM-12:00PM and 2:00PM and 4:00PM on Wednesdays, and 2:30PM to 4:15PM on Thursdays.
119
+
120
+ ```rb
121
+ Periodoxical.generate(
122
+ time_zone: 'America/Los_Angeles',
123
+ start_date: Date.parse('2024-05-23'),
124
+ end_date: Date.parse('2024-06-12'),
125
+ day_of_week_time_blocks: {
126
+ mon: [
127
+ { start_time: '8:00AM', end_time: '9:00AM' },
128
+ ],
129
+ wed: [
130
+ { start_time: '10:45AM', end_time: '12:00PM' },
131
+ { start_time: '2:00PM', end_time: '4:00PM' },
132
+ ],
133
+ thu: [
134
+ { start_time: '2:30PM', end_time: '4:15PM' }
135
+ ],
136
+ }
82
137
  )
83
138
  ```
84
139
 
@@ -1,3 +1,3 @@
1
1
  module Periodoxical
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.2"
3
3
  end
data/lib/periodoxical.rb CHANGED
@@ -34,7 +34,7 @@ module Periodoxical
34
34
  # Ex: %w(mon tue wed sat)
35
35
  # @param [Integer] limit
36
36
  # How many date times to generate. To be used when `end_date` is nil.
37
- # @param [Hash<Hash>] day_of_week_hours
37
+ # @param [Hash<Hash>] day_of_week_time_blocks
38
38
  # To be used when hours are different between days of the week
39
39
  # Ex: {
40
40
  # mon: [{ start_time: '10:15AM', end_time: '11:35AM' }, { start_time: '9:00AM' }, {end_time: '4:30PM'} ],
@@ -42,11 +42,11 @@ module Periodoxical
42
42
  # fri: { start_time: '7:00PM', end_time: '9:00PM' },
43
43
  # }
44
44
  def initialize(time_zone: 'Etc/UTC', days_of_week: nil,
45
- start_date:, end_date: nil, time_blocks: nil, day_of_week_hours: nil, limit: nil)
45
+ start_date:, end_date: nil, time_blocks: nil, day_of_week_time_blocks: nil, limit: nil)
46
46
  @time_zone = TZInfo::Timezone.get(time_zone)
47
47
  @days_of_week = days_of_week
48
48
  @time_blocks = time_blocks
49
- @day_of_week_hours = day_of_week_hours
49
+ @day_of_week_time_blocks = day_of_week_time_blocks
50
50
  @start_date = start_date
51
51
  @end_date = end_date
52
52
  @limit = limit
@@ -62,13 +62,44 @@ module Periodoxical
62
62
  # ]
63
63
  def generate
64
64
  if @days_of_week && @time_blocks
65
- generate_from_days_of_week_time_blocks
65
+ generate_when_same_time_blocks_for_all_days
66
+ elsif @day_of_week_time_blocks
67
+ generate_when_different_time_blocks_between_days
66
68
  end
67
69
  end
68
70
 
69
71
  private
70
72
 
71
- def generate_from_days_of_week_time_blocks
73
+ def generate_when_different_time_blocks_between_days
74
+ times_output = []
75
+ current_date = @start_date
76
+ current_count = 0
77
+ keep_generating = true
78
+ while keep_generating
79
+ day_of_week = day_of_week_long_to_short(current_date.strftime("%A"))
80
+ if @day_of_week_time_blocks[day_of_week.to_sym]
81
+ time_blocks = @day_of_week_time_blocks[day_of_week.to_sym]
82
+ time_blocks.each do |tb|
83
+ times_output << {
84
+ start: time_str_to_object(current_date, tb[:start_time]),
85
+ end: time_str_to_object(current_date, tb[:end_time])
86
+ }
87
+ current_count = current_count + 1
88
+ if @limit && current_count == @limit
89
+ keep_generating = false
90
+ break
91
+ end
92
+ end
93
+ end
94
+ current_date = current_date + 1
95
+ if @end_date && (current_date > @end_date)
96
+ keep_generating = false
97
+ end
98
+ end
99
+ times_output
100
+ end
101
+
102
+ def generate_when_same_time_blocks_for_all_days
72
103
  times_output = []
73
104
  current_date = @start_date
74
105
  current_count = 0
@@ -107,16 +138,16 @@ module Periodoxical
107
138
  end
108
139
  end
109
140
 
110
- if @day_of_week_hours
111
- @day_of_week_hours.keys.each do |d|
112
- unless VALID_DAYS_OF_WEEK.include?(d)
141
+ if @day_of_week_time_blocks
142
+ @day_of_week_time_blocks.keys.each do |d|
143
+ unless VALID_DAYS_OF_WEEK.include?(d.to_s)
113
144
  raise "#{d} is not a valid day of week format. Must be #{VALID_DAYS_OF_WEEK}"
114
145
  end
115
146
  end
116
147
  end
117
148
 
118
- unless (@days_of_week && @time_blocks) || (@day_of_week_hours)
119
- raise "Need to provide either `days_of_week` and `time_blocks` or `day_of_week_hours`"
149
+ unless (@days_of_week && @time_blocks) || (@day_of_week_time_blocks)
150
+ raise "Need to provide either `days_of_week` and `time_blocks` or `day_of_week_time_blocks`"
120
151
  end
121
152
 
122
153
  unless( @limit || @end_date)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: periodoxical
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Li
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-24 00:00:00.000000000 Z
11
+ date: 2024-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tzinfo