add_to_calendar_links 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/FUNDING.yml +12 -0
- data/.gitignore +9 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +134 -0
- data/Rakefile +10 -0
- data/add_to_calendar_links.gemspec +38 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/add_to_calendar_links/version.rb +5 -0
- data/lib/add_to_calendar_links.rb +276 -0
- metadata +152 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: db13d2739854e9084dc5f20de1ba937af2760ca5722e99cd92b83ae59adb79da
|
4
|
+
data.tar.gz: bdd83bb55237b2de0346e78a7c75752f02f4ea89ef522d5b4dc95e3b5f8f177c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1df0b7a7307a37cb42614d6cc2ce9fbc8b8a42fae7c11abe995df5625a9b0622f1aa14ada6a983c7e0cafa5f870b368893cc1dbf15b6ce142692a0bda105c04d
|
7
|
+
data.tar.gz: bfe123ceec2622a0b18c0020d65ac1b060614795f35bf9e8d51244e5a2730cda39a127f4f3ec6ec991e3a3670bc54d67ee73e3fa035c00207e7073ac97619ce9
|
data/.github/FUNDING.yml
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# These are supported funding model platforms
|
2
|
+
|
3
|
+
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
|
4
|
+
patreon: # Replace with a single Patreon username
|
5
|
+
open_collective: # Replace with a single Open Collective username
|
6
|
+
ko_fi: jaredlt
|
7
|
+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
8
|
+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
9
|
+
liberapay: # Replace with a single Liberapay username
|
10
|
+
issuehunt: # Replace with a single IssueHunt username
|
11
|
+
otechie: # Replace with a single Otechie username
|
12
|
+
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
add_to_calendar_links (0.3.1)
|
5
|
+
tzinfo (>= 1.1, < 3)
|
6
|
+
tzinfo-data (~> 1.2021)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
coderay (1.1.3)
|
12
|
+
concurrent-ruby (1.1.8)
|
13
|
+
method_source (1.0.0)
|
14
|
+
minitest (5.14.4)
|
15
|
+
pry (0.14.0)
|
16
|
+
coderay (~> 1.1)
|
17
|
+
method_source (~> 1.0)
|
18
|
+
rake (13.0.3)
|
19
|
+
tzinfo (2.0.4)
|
20
|
+
concurrent-ruby (~> 1.0)
|
21
|
+
tzinfo-data (1.2021.1)
|
22
|
+
tzinfo (>= 1.0.0)
|
23
|
+
|
24
|
+
PLATFORMS
|
25
|
+
ruby
|
26
|
+
|
27
|
+
DEPENDENCIES
|
28
|
+
add_to_calendar_links!
|
29
|
+
bundler (~> 2.0)
|
30
|
+
minitest (~> 5.0)
|
31
|
+
pry (~> 0.14.0)
|
32
|
+
rake (~> 13.0.3)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
2.2.3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Jared Turner
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
# AddToCalendar
|
2
|
+
|
3
|
+
A ruby gem to generate 'Add To Calendar' URLs for Android, Apple, Google, Office 365, Outlook, Outlook.com and Yahoo calendars.
|
4
|
+
|
5
|
+
If this gem brings you some value feel free to buy me a coffee :) [![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/P5P71PK9T)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'add_to_calendar'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install add_to_calendar
|
22
|
+
|
23
|
+
## Requirements
|
24
|
+
|
25
|
+
- Ruby 2.0 or higher
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
# create new instance, adding your event attributes
|
31
|
+
@cal = AddToCalendar::URLs.new(
|
32
|
+
start_datetime: Time.new(2020,12,12,13,30,00,0),
|
33
|
+
title: "Christmas party!",
|
34
|
+
timezone: 'Europe/London'
|
35
|
+
)
|
36
|
+
|
37
|
+
# access 'add to calendar' URLs
|
38
|
+
@cal.google_url
|
39
|
+
#=> "https://www.google.com/calendar/render?action=TEMPLATE&text=Christmas%20party%21&dates=20201212T133000/20201212T143000&ctz=Europe/London"
|
40
|
+
|
41
|
+
@cal.yahoo_url
|
42
|
+
#=> "https://calendar.yahoo.com/?v=60&view=d&type=20&title=Christmas%20party%21&st=20201212T133000Z&dur=0100"
|
43
|
+
|
44
|
+
@cal.office365_url
|
45
|
+
#=> "https://outlook.office.com/calendar/0/deeplink/compose?path=/calendar/action/compose&rru=addevent&subject=Christmas%20party%21&startdt=2020-12-12T13:30:00Z&enddt=2020-12-12T14:30:00Z"
|
46
|
+
|
47
|
+
# For outlook.com, different to Outlook the desktop application below
|
48
|
+
@cal.outlook_com_url
|
49
|
+
#=> "https://outlook.live.com/calendar/0/deeplink/compose?path=/calendar/action/compose&rru=addevent&subject=Christmas%20party%21&startdt=2020-12-12T13:30:00Z&enddt=2020-12-12T14:30:00Z"
|
50
|
+
|
51
|
+
# ical provides a data-uri which will download a properly formatted *.ics file (see 'Creating HTML links' section)
|
52
|
+
@cal.ical_url
|
53
|
+
#=> "data:text/calendar;charset=utf8,BEGIN:VCALENDAR%0AVERSION:2.0%0ABEGIN:VEVENT%0ADTSTART:20201212T133000Z%0ADTEND:20201212T143000Z%0ASUMMARY:Christmas%20party%21%0AUID:-20201212T133000Z-Christmas%20party%21%0AEND:VEVENT%0AEND:VCALENDAR"
|
54
|
+
|
55
|
+
# android_url, apple_url and outlook_url are simply helper methods that call ical_url and return the same string.
|
56
|
+
```
|
57
|
+
|
58
|
+
### Creating HTML links
|
59
|
+
|
60
|
+
```erb
|
61
|
+
<!-- Simply pass the url into the href Eg. in ERB -->
|
62
|
+
<a href="<%= @cal.google_url %>">Add to Google Calendar</a>
|
63
|
+
|
64
|
+
<a href="<%= @cal.yahoo_url %>">Add to Yahoo Calendar</a>
|
65
|
+
|
66
|
+
<!-- for ical_url, android_url, apple_url and outlook_url you can set the filename like so -->
|
67
|
+
<a download="calendar-event.ics" href="<%= @cal.ical_url %>">Download iCal</a>
|
68
|
+
```
|
69
|
+
|
70
|
+
### Event attributes
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
event_attributes = {
|
74
|
+
start_datetime: Time.new(2020,12,12,9,00,00,0), # required
|
75
|
+
end_datetime: Time.new(2020,12,12,17,00,00,0),
|
76
|
+
title: "Ruby Conference", # required
|
77
|
+
timezone: 'America/New_York', # required
|
78
|
+
location: "20 W 34th St, New York, NY 10001",
|
79
|
+
url: "https://www.ruby-lang.org/en/",
|
80
|
+
description: "Join us to learn all about Ruby.",
|
81
|
+
add_url_to_description: true # defaults to true
|
82
|
+
}
|
83
|
+
|
84
|
+
cal = AddToCalendar::URLs.new(event_attributes)
|
85
|
+
```
|
86
|
+
|
87
|
+
| Attribute | Required? | Class | Notes |
|
88
|
+
| -----------------------|-----------|------------|-------|
|
89
|
+
| start_datetime | Yes | Time | |
|
90
|
+
| end_datetime | No | Time | <ul><li>If not provided, defaults to start_datetime + 1 hour</li><li>Must be > start_datetime</li></ul> |
|
91
|
+
| title | Yes | String | |
|
92
|
+
| timezone | Yes | String | Must be in [tz database format](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) Eg. 'Europe/London', 'America/New_York' |
|
93
|
+
| location | No | String | |
|
94
|
+
| url | No | String | Most providers do not have a native URL field. If you set `url` it will be added to the end of the description field (see `add_url_to_description`) |
|
95
|
+
| description | No | String | Accepts newlines by passing `\n` Eg. `"Join us for fun & drinks\n\nPS. Smart casual"` |
|
96
|
+
| 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 |
|
97
|
+
|
98
|
+
### Timezones and offsets
|
99
|
+
|
100
|
+
- Offset values eg. "2020-05-13 15:31:00 **+05:00**" are ignored. It is only important that you have the correct date and time numbers set. The timezone is set directly using its own attribute `timezone`.
|
101
|
+
- You must set a timezone so that when users add the event to their calendar it shows at their correct local time.
|
102
|
+
- Eg. London event @ `2020-05-13 13:30:00` will save in a New Yorker's calendar as local time `2020-05-13 17:30:00`
|
103
|
+
|
104
|
+
### Browser support
|
105
|
+
|
106
|
+
- IE11 and lower will not work for `ical_url`, `apple_url` and `outlook_url` (IE does not properly support [data-uri links](https://caniuse.com/#feat=datauri). See [#16](https://github.com/jaredlt/add_to_calendar/issues/16)).
|
107
|
+
- IE11 will also not work with `Yahoo`, but this is because Yahoo is deprecating IE 11 support and only offers a simplified interface which does not work with the add event URL.
|
108
|
+
- `Office 365` and `Outlook.com` do not work on mobile. This seems to be an issue on Microsoft's side. Their mobile web interface does not support the 'create event' URLs and the links do not open the apps if you have them installed.
|
109
|
+
|
110
|
+
### More details
|
111
|
+
|
112
|
+
- Read the [Wiki](https://github.com/jaredlt/add_to_calendar/wiki) for more specific details
|
113
|
+
|
114
|
+
## Why build this?
|
115
|
+
|
116
|
+
I couldn't find an approriate gem or javascript library that did exactly what I wanted. So I decided to scratch my own itch to solve a problem for a startup I'm working on: https://www.littlefutures.org
|
117
|
+
|
118
|
+
## Releases
|
119
|
+
|
120
|
+
- https://rubygems.org/gems/add_to_calendar
|
121
|
+
|
122
|
+
## Development
|
123
|
+
|
124
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
125
|
+
|
126
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
127
|
+
|
128
|
+
## Contributing
|
129
|
+
|
130
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jaredlt/add_to_calendar.
|
131
|
+
|
132
|
+
## License
|
133
|
+
|
134
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "add_to_calendar_links/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "add_to_calendar_links"
|
7
|
+
spec.version = AddToCalendarLinks::VERSION
|
8
|
+
spec.authors = ["Jared Turner", "James Watling"]
|
9
|
+
spec.email = ["jaredlt01@gmail.com", "watling.james@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Generate 'Add To Calendar' URLs for Android, Apple, Google, Office 365, Outlook, Outlook.com and Yahoo calendars"
|
12
|
+
# spec.description = %q{TODO: Write a longer description or delete this line.}
|
13
|
+
spec.homepage = "https://github.com/JamesWatling/add_to_calendar"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
18
|
+
spec.metadata["changelog_uri"] = spec.homepage
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.required_ruby_version = '>= 2.0'
|
30
|
+
|
31
|
+
spec.add_dependency "tzinfo", ">= 1.1", "< 3"
|
32
|
+
spec.add_dependency "tzinfo-data", "~> 1.2021"
|
33
|
+
|
34
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
35
|
+
spec.add_development_dependency "rake", "~> 13.0.3"
|
36
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
37
|
+
spec.add_development_dependency "pry", "~> 0.14.0"
|
38
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "add_to_calendar"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,276 @@
|
|
1
|
+
require "add_to_calendar_links/version"
|
2
|
+
|
3
|
+
# erb util needed for url_encode method
|
4
|
+
# CGI::escape uses + instead of %20 which doesn't work for ical files
|
5
|
+
require "erb"
|
6
|
+
include ERB::Util
|
7
|
+
require 'tzinfo'
|
8
|
+
require 'date'
|
9
|
+
require 'uri'
|
10
|
+
# require 'pry'
|
11
|
+
|
12
|
+
module AddToCalendarLinks
|
13
|
+
class Error < StandardError; end
|
14
|
+
|
15
|
+
class URLs
|
16
|
+
attr_accessor :start_datetime, :end_datetime, :title, :timezone, :location, :url, :description, :add_url_to_description, :organizer, :strip_html
|
17
|
+
def initialize(start_datetime:, end_datetime: nil, title:, timezone:, location: nil, url: nil, description: nil, add_url_to_description: true, organizer: nil, strip_html: false)
|
18
|
+
@start_datetime = start_datetime
|
19
|
+
@end_datetime = end_datetime
|
20
|
+
@title = title
|
21
|
+
@timezone = TZInfo::Timezone.get(timezone)
|
22
|
+
@location = location
|
23
|
+
@url = url
|
24
|
+
@description = description
|
25
|
+
@add_url_to_description = add_url_to_description
|
26
|
+
@organizer = URI.parse(organizer) if organizer
|
27
|
+
@strip_html = strip_html
|
28
|
+
validate_attributes
|
29
|
+
end
|
30
|
+
|
31
|
+
def google_url
|
32
|
+
# Eg. https://www.google.com/calendar/render?action=TEMPLATE&text=Holly%27s%208th%20Birthday!&dates=20200615T180000/20200615T190000&ctz=Europe/London&details=Join%20us%20to%20celebrate%20with%20lots%20of%20games%20and%20cake!&location=Apartments,%20London&sprop=&sprop=name:
|
33
|
+
calendar_url = "https://www.google.com/calendar/render?action=TEMPLATE"
|
34
|
+
params = {}
|
35
|
+
params[:text] = url_encode(title)
|
36
|
+
if end_datetime
|
37
|
+
params[:dates] = "#{format_date_google(start_datetime)}/#{format_date_google(end_datetime)}"
|
38
|
+
else
|
39
|
+
params[:dates] = "#{format_date_google(start_datetime)}/#{format_date_google(start_datetime + 60*60)}" # end time is 1 hour later
|
40
|
+
end
|
41
|
+
params[:ctz] = timezone.identifier
|
42
|
+
params[:location] = url_encode(location) if location
|
43
|
+
params[:details] = url_encode(description) if description
|
44
|
+
if add_url_to_description && url
|
45
|
+
if params[:details]
|
46
|
+
params[:details] << url_encode("\n\n#{url}")
|
47
|
+
else
|
48
|
+
params[:details] = url_encode(url)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
params.each do |key, value|
|
53
|
+
calendar_url << "&#{key}=#{value}"
|
54
|
+
end
|
55
|
+
|
56
|
+
return calendar_url
|
57
|
+
end
|
58
|
+
|
59
|
+
def yahoo_url
|
60
|
+
# Eg. https://calendar.yahoo.com/?v=60&view=d&type=20&title=Holly%27s%208th%20Birthday!&st=20200615T170000Z&dur=0100&desc=Join%20us%20to%20celebrate%20with%20lots%20of%20games%20and%20cake!&in_loc=7%20Apartments,%20London
|
61
|
+
calendar_url = "https://calendar.yahoo.com/?v=60&view=d&type=20"
|
62
|
+
params = {}
|
63
|
+
params[:title] = url_encode(title)
|
64
|
+
params[:st] = utc_datetime(start_datetime)
|
65
|
+
if end_datetime
|
66
|
+
seconds = duration_seconds(start_datetime, end_datetime)
|
67
|
+
params[:dur] = seconds_to_hours_minutes(seconds)
|
68
|
+
else
|
69
|
+
params[:dur] = "0100"
|
70
|
+
end
|
71
|
+
params[:desc] = url_encode(description) if description
|
72
|
+
if add_url_to_description && url
|
73
|
+
if params[:desc]
|
74
|
+
params[:desc] << url_encode("\n\n#{url}")
|
75
|
+
else
|
76
|
+
params[:desc] = url_encode(url)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
params[:in_loc] = url_encode(location) if location
|
80
|
+
|
81
|
+
params.each do |key, value|
|
82
|
+
calendar_url << "&#{key}=#{value}"
|
83
|
+
end
|
84
|
+
|
85
|
+
return calendar_url
|
86
|
+
end
|
87
|
+
|
88
|
+
def office365_url
|
89
|
+
# Eg. https://outlook.live.com/calendar/0/deeplink/compose?path=/calendar/action/compose&rru=addevent&subject=Holly%27s%208th%20Birthday%21&startdt=2020-05-12T12:30:00Z&enddt=2020-05-12T16:00:00Z&body=Come%20join%20us%20for%20lots%20of%20fun%20%26%20cake%21%0A%0Ahttps%3A%2F%2Fwww.example.com%2Fevent-details&location=Flat%204%2C%20The%20Edge%2C%2038%20Smith-Dorrien%20St%2C%20London%2C%20N1%207GU
|
90
|
+
microsoft("office365")
|
91
|
+
end
|
92
|
+
|
93
|
+
def outlook_com_url
|
94
|
+
# Eg. https://outlook.live.com/calendar/0/deeplink/compose?path=/calendar/action/compose&rru=addevent&subject=Holly%27s%208th%20Birthday%21&startdt=2020-05-12T12:30:00Z&enddt=2020-05-12T16:00:00Z&body=Come%20join%20us%20for%20lots%20of%20fun%20%26%20cake%21%0A%0Ahttps%3A%2F%2Fwww.example.com%2Fevent-details&location=Flat%204%2C%20The%20Edge%2C%2038%20Smith-Dorrien%20St%2C%20London%2C%20N1%207GU
|
95
|
+
microsoft("outlook.com")
|
96
|
+
end
|
97
|
+
|
98
|
+
def ical_url
|
99
|
+
# Downloads a *.ics file provided as a data-uri
|
100
|
+
# Eg. "data:text/calendar;charset=utf8,BEGIN:VCALENDAR%0AVERSION:2.0%0ABEGIN:VEVENT%0ADTSTART:20200512T123000Z%0ADTEND:20200512T160000Z%0ASUMMARY:Holly%27s%208th%20Birthday%21%0AURL:https%3A%2F%2Fwww.example.com%2Fevent-details%0ADESCRIPTION:Come%20join%20us%20for%20lots%20of%20fun%20%26%20cake%21\\n\\nhttps%3A%2F%2Fwww.example.com%2Fevent-details%0ALOCATION:Flat%204%5C%2C%20The%20Edge%5C%2C%2038%20Smith-Dorrien%20St%5C%2C%20London%5C%2C%20N1%207GU%0AUID:-https%3A%2F%2Fwww.example.com%2Fevent-details%0AEND:VEVENT%0AEND:VCALENDAR"
|
101
|
+
calendar_url = "data:text/calendar;charset=utf8,BEGIN:VCALENDAR%0AVERSION:2.0%0ABEGIN:VEVENT"
|
102
|
+
params = {}
|
103
|
+
params[:DTSTART] = utc_datetime(start_datetime)
|
104
|
+
if end_datetime
|
105
|
+
params[:DTEND] = utc_datetime(end_datetime)
|
106
|
+
else
|
107
|
+
params[:DTEND] = utc_datetime(start_datetime + 60*60) # 1 hour later
|
108
|
+
end
|
109
|
+
params[:SUMMARY] = url_encode_ical(title, strip_html: true) #ical doesnt support html so remove all markup. Optional for other formats
|
110
|
+
params[:URL] = url_encode(url) if url
|
111
|
+
params[:DESCRIPTION] = url_encode_ical(description, strip_html: true) if description
|
112
|
+
if add_url_to_description && url
|
113
|
+
if params[:DESCRIPTION]
|
114
|
+
params[:DESCRIPTION] << "\\n\\n#{url_encode(url)}"
|
115
|
+
else
|
116
|
+
params[:DESCRIPTION] = url_encode(url)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
params[:LOCATION] = url_encode_ical(location) if location
|
120
|
+
params[:UID] = "-#{url_encode(url)}" if url
|
121
|
+
params[:UID] = "-#{utc_datetime(start_datetime)}-#{url_encode_ical(title)}" unless params[:UID] # set uid based on starttime and title only if url is unavailable
|
122
|
+
params[:organizer] = organizer if organizer
|
123
|
+
|
124
|
+
new_line = "%0A"
|
125
|
+
params.each do |key, value|
|
126
|
+
calendar_url << "#{new_line}#{key}:#{value}"
|
127
|
+
end
|
128
|
+
|
129
|
+
calendar_url << "%0AEND:VEVENT%0AEND:VCALENDAR"
|
130
|
+
|
131
|
+
return calendar_url
|
132
|
+
end
|
133
|
+
|
134
|
+
def apple_url
|
135
|
+
ical_url
|
136
|
+
end
|
137
|
+
|
138
|
+
def outlook_url
|
139
|
+
ical_url
|
140
|
+
end
|
141
|
+
|
142
|
+
def android_url
|
143
|
+
ical_url
|
144
|
+
end
|
145
|
+
|
146
|
+
private
|
147
|
+
def validate_attributes
|
148
|
+
# msg = "- Object must be a DateTime or Time object."
|
149
|
+
msg = "- Object must be a Time object."
|
150
|
+
raise(ArgumentError, ":start_datetime #{msg} #{start_datetime.class} given") unless start_datetime.kind_of? Time
|
151
|
+
if end_datetime
|
152
|
+
raise(ArgumentError, ":end_datetime #{msg} #{end_datetime.class} given") unless end_datetime.kind_of? Time
|
153
|
+
raise(ArgumentError, ":end_datetime must be greater than :start_datetime") unless end_datetime > start_datetime
|
154
|
+
end
|
155
|
+
|
156
|
+
raise(ArgumentError, ":title must be a string") unless self.title.kind_of? String
|
157
|
+
raise(ArgumentError, ":title must not be blank") if self.title.strip.empty? # strip first, otherwise " ".empty? #=> false
|
158
|
+
|
159
|
+
if location
|
160
|
+
raise(ArgumentError, ":location must be a string") unless self.location.kind_of? String
|
161
|
+
end
|
162
|
+
|
163
|
+
if description
|
164
|
+
raise(ArgumentError, ":description must be a string") unless self.description.kind_of? String
|
165
|
+
end
|
166
|
+
|
167
|
+
if organizer
|
168
|
+
raise(ArgumentError, ":organizer must be a string") unless self.organizer.kind_of? String
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def microsoft(service)
|
173
|
+
# Eg.
|
174
|
+
calendar_url = case service
|
175
|
+
when "outlook.com"
|
176
|
+
"https://outlook.live.com/calendar/0/deeplink/compose?path=/calendar/action/compose&rru=addevent"
|
177
|
+
when "office365"
|
178
|
+
"https://outlook.office.com/calendar/0/deeplink/compose?path=/calendar/action/compose&rru=addevent"
|
179
|
+
else
|
180
|
+
raise MicrosoftServiceError, ":service must be 'outlook.com' or 'office365'. '#{service}' given"
|
181
|
+
end
|
182
|
+
|
183
|
+
params = {}
|
184
|
+
params[:subject] = url_encode(title.gsub(' & ', ' and '))
|
185
|
+
params[:startdt] = utc_datetime_microsoft(start_datetime)
|
186
|
+
if end_datetime
|
187
|
+
params[:enddt] = utc_datetime_microsoft(end_datetime)
|
188
|
+
else
|
189
|
+
params[:enddt] = utc_datetime_microsoft(start_datetime + 60*60) # 1 hour later
|
190
|
+
end
|
191
|
+
params[:body] = url_encode(newlines_to_html_br(description)) if description
|
192
|
+
if add_url_to_description && url
|
193
|
+
if params[:body]
|
194
|
+
params[:body] << url_encode(newlines_to_html_br("\n\n#{url}"))
|
195
|
+
else
|
196
|
+
params[:body] = url_encode(url)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
params[:location] = url_encode(location) if location
|
200
|
+
|
201
|
+
params.each do |key, value|
|
202
|
+
calendar_url << "&#{key}=#{value}"
|
203
|
+
end
|
204
|
+
|
205
|
+
return calendar_url
|
206
|
+
end
|
207
|
+
|
208
|
+
def utc_datetime(datetime)
|
209
|
+
t = timezone.local_to_utc(
|
210
|
+
Time.new(
|
211
|
+
datetime.strftime("%Y").to_i,
|
212
|
+
datetime.strftime("%m").to_i,
|
213
|
+
datetime.strftime("%d").to_i,
|
214
|
+
datetime.strftime("%H").to_i,
|
215
|
+
datetime.strftime("%M").to_i,
|
216
|
+
datetime.strftime("%S").to_i
|
217
|
+
)
|
218
|
+
)
|
219
|
+
|
220
|
+
return t.strftime('%Y%m%dT%H%M%SZ')
|
221
|
+
end
|
222
|
+
|
223
|
+
def utc_datetime_microsoft(datetime)
|
224
|
+
t = timezone.local_to_utc(
|
225
|
+
Time.new(
|
226
|
+
datetime.strftime("%Y").to_i,
|
227
|
+
datetime.strftime("%m").to_i,
|
228
|
+
datetime.strftime("%d").to_i,
|
229
|
+
datetime.strftime("%H").to_i,
|
230
|
+
datetime.strftime("%M").to_i,
|
231
|
+
datetime.strftime("%S").to_i
|
232
|
+
)
|
233
|
+
)
|
234
|
+
|
235
|
+
return t.strftime('%Y-%m-%dT%H:%M:%SZ')
|
236
|
+
end
|
237
|
+
|
238
|
+
def format_date_google(start_datetime)
|
239
|
+
start_datetime.strftime('%Y%m%dT%H%M%S')
|
240
|
+
end
|
241
|
+
|
242
|
+
def duration_seconds(start_time, end_time)
|
243
|
+
(start_time.to_i - end_time.to_i).abs
|
244
|
+
end
|
245
|
+
|
246
|
+
def seconds_to_hours_minutes(sec)
|
247
|
+
"%02d%02d" % [sec / 3600, sec / 60 % 60]
|
248
|
+
end
|
249
|
+
|
250
|
+
def newlines_to_html_br(string)
|
251
|
+
string.gsub(/(?:\n\r?|\r\n?)/, '<br>')
|
252
|
+
end
|
253
|
+
|
254
|
+
def url_encode_ical(s, strip_html: @strip_html)
|
255
|
+
# per https://tools.ietf.org/html/rfc5545#section-3.3.11
|
256
|
+
string = s.dup # don't modify original input
|
257
|
+
|
258
|
+
string = strip_html_tags(string) if strip_html
|
259
|
+
string.gsub!("\\", "\\\\\\") # \ >> \\ --yes, really: https://stackoverflow.com/questions/6209480/how-to-replace-backslash-with-double-backslash
|
260
|
+
string.gsub!(",", "\\,")
|
261
|
+
string.gsub!(";", "\\;")
|
262
|
+
string.gsub!("\r\n", "\n") # so can handle all newlines the same
|
263
|
+
string.split("\n").map { |e|
|
264
|
+
if e.empty?
|
265
|
+
e
|
266
|
+
else
|
267
|
+
url_encode(e)
|
268
|
+
end
|
269
|
+
}.join("\\n")
|
270
|
+
end
|
271
|
+
|
272
|
+
def strip_html_tags(description)
|
273
|
+
description.dup.gsub(/<\/?[^>]*>/, "")
|
274
|
+
end
|
275
|
+
end
|
276
|
+
end
|
metadata
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: add_to_calendar_links
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jared Turner
|
8
|
+
- James Watling
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2021-03-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: tzinfo
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.1'
|
21
|
+
- - "<"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '3'
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '1.1'
|
31
|
+
- - "<"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3'
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: tzinfo-data
|
36
|
+
requirement: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.2021'
|
41
|
+
type: :runtime
|
42
|
+
prerelease: false
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.2021'
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: bundler
|
50
|
+
requirement: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 13.0.3
|
69
|
+
type: :development
|
70
|
+
prerelease: false
|
71
|
+
version_requirements: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 13.0.3
|
76
|
+
- !ruby/object:Gem::Dependency
|
77
|
+
name: minitest
|
78
|
+
requirement: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '5.0'
|
83
|
+
type: :development
|
84
|
+
prerelease: false
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '5.0'
|
90
|
+
- !ruby/object:Gem::Dependency
|
91
|
+
name: pry
|
92
|
+
requirement: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.14.0
|
97
|
+
type: :development
|
98
|
+
prerelease: false
|
99
|
+
version_requirements: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.14.0
|
104
|
+
description:
|
105
|
+
email:
|
106
|
+
- jaredlt01@gmail.com
|
107
|
+
- watling.james@gmail.com
|
108
|
+
executables: []
|
109
|
+
extensions: []
|
110
|
+
extra_rdoc_files: []
|
111
|
+
files:
|
112
|
+
- ".github/FUNDING.yml"
|
113
|
+
- ".gitignore"
|
114
|
+
- ".travis.yml"
|
115
|
+
- Gemfile
|
116
|
+
- Gemfile.lock
|
117
|
+
- LICENSE.txt
|
118
|
+
- README.md
|
119
|
+
- Rakefile
|
120
|
+
- add_to_calendar_links.gemspec
|
121
|
+
- bin/console
|
122
|
+
- bin/setup
|
123
|
+
- lib/add_to_calendar_links.rb
|
124
|
+
- lib/add_to_calendar_links/version.rb
|
125
|
+
homepage: https://github.com/JamesWatling/add_to_calendar
|
126
|
+
licenses:
|
127
|
+
- MIT
|
128
|
+
metadata:
|
129
|
+
homepage_uri: https://github.com/JamesWatling/add_to_calendar
|
130
|
+
source_code_uri: https://github.com/JamesWatling/add_to_calendar
|
131
|
+
changelog_uri: https://github.com/JamesWatling/add_to_calendar
|
132
|
+
post_install_message:
|
133
|
+
rdoc_options: []
|
134
|
+
require_paths:
|
135
|
+
- lib
|
136
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '2.0'
|
141
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
requirements: []
|
147
|
+
rubygems_version: 3.2.3
|
148
|
+
signing_key:
|
149
|
+
specification_version: 4
|
150
|
+
summary: Generate 'Add To Calendar' URLs for Android, Apple, Google, Office 365, Outlook,
|
151
|
+
Outlook.com and Yahoo calendars
|
152
|
+
test_files: []
|