Skip to content

Commit 9cb5d47

Browse files
committed
Add organizer for ical
1 parent 84a7153 commit 9cb5d47

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ event_attributes = {
7979
url: "https://www.ruby-lang.org/en/",
8080
description: "Join us to learn all about Ruby.",
8181
add_url_to_description: true, # defaults to true
82-
all_day: true # defaults to false
82+
all_day: true, # defaults to false
83+
organizer: {
84+
name: "First Last",
85+
email: "email@example.com"
86+
}
8387
}
8488

8589
cal = AddToCalendar::URLs.new(**event_attributes)
@@ -96,6 +100,7 @@ cal = AddToCalendar::URLs.new(**event_attributes)
96100
| description | No | String | Accepts newlines by passing `\n` Eg. `"Join us for fun & drinks\n\nPS. Smart casual"` |
97101
| add_url_to_description | No | true/false | Defaults to `true`. Set `add_url_to_description: false` to stop the URL from being added to the description |
98102
| all_day | No | true/false | <ul><li>Defaults to `false`.</li><li>When set to `true` the times will be ignored.</li><li>If no end_datetime provided it will be a single day event.</li><li>When providing end_datetime, use the final day of the event (eg. 1 day event start: 2023-05-01, end: 2023-05-01; 3 day event start: 2023-05-01, end: 2023-05-03).</li><li>Some calendars require you to specify the _day after_ as the end date which feels counterintuitive, this Gem takes care of that for you.</li></ul> |
103+
| organizer | No | Hash | <ul><li>Only supported by ical</li><li>If used you must provide both `name` and `email`</li><li>Must be in format `{ name: "First Last", email: "email@example.com" }`</li></ul> |
99104

100105
### Timezones and offsets
101106

lib/add_to_calendar.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
require 'date'
99
# require 'pry'
1010

11-
1211
module AddToCalendar
1312
class Error < StandardError; end
1413

1514
class URLs
16-
attr_accessor :start_datetime, :end_datetime, :title, :timezone, :location, :url, :description, :add_url_to_description, :all_day
17-
def initialize(start_datetime:, end_datetime: nil, title:, timezone:, location: nil, url: nil, description: nil, add_url_to_description: true, all_day: false)
15+
attr_accessor :start_datetime, :end_datetime, :title, :timezone, :location, :url, :description, :add_url_to_description, :all_day, :organizer
16+
def initialize(start_datetime:, end_datetime: nil, title:, timezone:, location: nil, url: nil, description: nil, add_url_to_description: true, all_day: false, organizer: nil)
1817
@start_datetime = start_datetime
1918
@end_datetime = end_datetime
2019
@title = title
@@ -24,6 +23,7 @@ def initialize(start_datetime:, end_datetime: nil, title:, timezone:, location:
2423
@description = description
2524
@add_url_to_description = add_url_to_description
2625
@all_day = all_day
26+
@organizer = organizer
2727

2828
validate_attributes
2929
end
@@ -126,6 +126,9 @@ def ical_url
126126
end
127127
end
128128
params[:SUMMARY] = url_encode_ical(title)
129+
if organizer
130+
params[:ORGANIZER] = url_encode_ical("CN=\"#{organizer[:name]}\":mailto:#{organizer[:email]}")
131+
end
129132
params[:URL] = url_encode(url) if url
130133
params[:DESCRIPTION] = url_encode_ical(description) if description
131134
if add_url_to_description && url
@@ -141,7 +144,11 @@ def ical_url
141144

142145
new_line = "%0A"
143146
params.each do |key, value|
144-
calendar_url << "#{new_line}#{key}:#{value}"
147+
if key == :ORGANIZER
148+
calendar_url << "#{new_line}#{key};#{value}"
149+
else
150+
calendar_url << "#{new_line}#{key}:#{value}"
151+
end
145152
end
146153

147154
calendar_url << "%0AEND:VEVENT%0AEND:VCALENDAR"
@@ -181,6 +188,12 @@ def validate_attributes
181188
if description
182189
raise(ArgumentError, ":description must be a string") unless self.description.kind_of? String
183190
end
191+
192+
if organizer
193+
unless self.organizer.is_a?(Hash) && self.organizer[:name].is_a?(String) && self.organizer[:email].is_a?(String)
194+
raise(ArgumentError, ":organizer must be a Hash of format { name: \"First Last\", email: \"email@example.com\" }")
195+
end
196+
end
184197
end
185198

186199
def microsoft(service)

test/add_to_calendar_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,15 @@ def test_calling_class_methods_should_not_mutate_initialized_attributes
270270
assert cal.location == "20 W 34th St, New York, NY 10001"
271271
end
272272

273+
def test_attribute_organizer_must_be_valid
274+
assert_raises(ArgumentError) do
275+
AddToCalendar::URLs.new(
276+
start_datetime: Time.new(@next_month_year,@next_month_month,@next_month_day,13,30,00,0),
277+
title: @title,
278+
timezone: @timezone,
279+
organizer: "not valid"
280+
)
281+
end
282+
end
283+
273284
end

test/urls/ical_url_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,5 +246,25 @@ def test_all_day_end_date_is_plus_one_from_end_date
246246

247247
assert cal.ical_url == ical
248248
end
249+
250+
def test_organizer
251+
cal = AddToCalendar::URLs.new(
252+
start_datetime: Time.new(@next_month_year,@next_month_month,@next_month_day,13,30,00,0),
253+
end_datetime: Time.new(@next_month_year,@next_month_month,@next_month_day,14,30,00,0),
254+
title: @title,
255+
timezone: @timezone,
256+
organizer: {
257+
name: "Jared Turner",
258+
email: "jared@example.com"
259+
}
260+
)
261+
uid = "%0AUID:-#{cal.send(:utc_datetime, cal.start_datetime)}-#{cal.send(:url_encode_ical, cal.title)}"
262+
ical = @url_with_defaults_required +
263+
"%0AORGANIZER;CN%3D%22Jared%20Turner%22%3Amailto%3Ajared%40example.com" +
264+
uid +
265+
@url_end
266+
267+
assert cal.ical_url == ical
268+
end
249269

250270
end

0 commit comments

Comments
 (0)