Class: AddToCalendar::URLs

Inherits:
Object
  • Object
show all
Defined in:
lib/add_to_calendar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_datetime:, end_datetime: nil, title:, timezone:, location: nil, url: nil, description: nil, add_url_to_description: true, all_day: false, organizer: nil) ⇒ URLs

Returns a new instance of URLs.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/add_to_calendar.rb', line 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)
  @start_datetime = start_datetime
  @end_datetime = end_datetime
  @title = title
  @timezone = TZInfo::Timezone.get(timezone)
  @location = location
  @url = url
  @description = description
  @add_url_to_description = add_url_to_description
  @all_day = all_day
  @organizer = organizer
  
  validate_attributes
end

Instance Attribute Details

#add_url_to_descriptionObject

Returns the value of attribute add_url_to_description.



15
16
17
# File 'lib/add_to_calendar.rb', line 15

def add_url_to_description
  @add_url_to_description
end

#all_dayObject

Returns the value of attribute all_day.



15
16
17
# File 'lib/add_to_calendar.rb', line 15

def all_day
  @all_day
end

#descriptionObject

Returns the value of attribute description.



15
16
17
# File 'lib/add_to_calendar.rb', line 15

def description
  @description
end

#end_datetimeObject

Returns the value of attribute end_datetime.



15
16
17
# File 'lib/add_to_calendar.rb', line 15

def end_datetime
  @end_datetime
end

#locationObject

Returns the value of attribute location.



15
16
17
# File 'lib/add_to_calendar.rb', line 15

def location
  @location
end

#organizerObject

Returns the value of attribute organizer.



15
16
17
# File 'lib/add_to_calendar.rb', line 15

def organizer
  @organizer
end

#start_datetimeObject

Returns the value of attribute start_datetime.



15
16
17
# File 'lib/add_to_calendar.rb', line 15

def start_datetime
  @start_datetime
end

#timezoneObject

Returns the value of attribute timezone.



15
16
17
# File 'lib/add_to_calendar.rb', line 15

def timezone
  @timezone
end

#titleObject

Returns the value of attribute title.



15
16
17
# File 'lib/add_to_calendar.rb', line 15

def title
  @title
end

#urlObject

Returns the value of attribute url.



15
16
17
# File 'lib/add_to_calendar.rb', line 15

def url
  @url
end

Instance Method Details

#android_urlObject



220
221
222
# File 'lib/add_to_calendar.rb', line 220

def android_url
  ical_url
end

#apple_urlObject



212
213
214
# File 'lib/add_to_calendar.rb', line 212

def apple_url
  ical_url
end

#google_urlObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/add_to_calendar.rb', line 31

def google_url
  # 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:
  calendar_url = "https://www.google.com/calendar/render?action=TEMPLATE"
  params = {}
  params[:text] = url_encode(title)
  params[:dates] = google_dates(start_datetime, end_datetime, all_day)
  params[:ctz] = timezone.identifier
  params[:location] = url_encode(location) if location
  params[:details] = url_encode(description) if description
  if add_url_to_description && url
    if params[:details]
      params[:details] << url_encode("\n\n#{url}")
    else
      params[:details] = url_encode(url)
    end
  end
  
  params.each do |key, value|
    calendar_url << "&#{key}=#{value}"
  end
  
  return calendar_url
end

#hey_urlObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/add_to_calendar.rb', line 95

def hey_url
  calendar_url = "https://app.hey.com/calendar/ical_events/new?ical_source=BEGIN%3AVCALENDAR%0AVERSION%3A2.0%0APRODID%3A-//AddToCalendar//RubyGem//EN%0ABEGIN%3AVEVENT"
  
  params = {}
  params[:SUMMARY] = url_encode(title)
  params[:DTSTAMP] = Time.now.strftime("%Y%m%dT%H%M%SZ")
  
  if all_day
    one_day = 1 * 24 * 60 * 60
    params["DTSTART%3BVALUE=DATE"] = format_date(start_datetime)
    if end_datetime
      params["DTEND%3BVALUE=DATE"] = format_date(end_datetime + one_day)
    else
      params["DTEND%3BVALUE=DATE"] = format_date(start_datetime + one_day)
    end
  else
    params[:DTSTART] = utc_datetime(start_datetime)
    if end_datetime
      params[:DTEND] = utc_datetime(end_datetime)
    else
      params[:DTEND] = utc_datetime(start_datetime + 60*60) # 1 hour later
    end
  end
  
  params[:URL] = url_encode(url) if url
  params[:UID] = "-#{utc_datetime(start_datetime)}-#{url_encode(title)}"
  params[:DESCRIPTION] = url_encode_hey(description) if description
  if add_url_to_description && url
    if params[:DESCRIPTION]
      params[:DESCRIPTION] << "\n\n#{url_encode(url)}"
    else
      params[:DESCRIPTION] = url_encode(url)
    end
  end
  if organizer
    params[:ORGANIZER] = url_encode("CN=\"#{organizer[:name]}\"%3Amailto%3A#{organizer[:email]}")
  end
  params[:LOCATION] = url_encode(location) if location
  
  new_line = "%0A"
  params.each do |key, value|
    if key == :ORGANIZER
      calendar_url << "#{new_line}#{key}%3B#{value}"
    else
      calendar_url << "#{new_line}#{key}%3A#{value}"
    end
  end
  
  calendar_url << "%0AEND%3AVEVENT%0AEND%3AVCALENDAR"

  return calendar_url
end

#ical_urlObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/add_to_calendar.rb', line 158

def ical_url
  # Downloads a *.ics file provided as a data-uri
  # 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"
  calendar_url = "data:text/calendar;charset=utf8,BEGIN:VCALENDAR%0AVERSION:2.0%0APRODID:-//AddToCalendar//RubyGem//EN%0ABEGIN:VEVENT"

  params = {}
  params[:DTSTAMP] = Time.now.strftime("%Y%m%dT%H%M%SZ")
  if all_day
    one_day = 1 * 24 * 60 * 60
    params["DTSTART;VALUE=DATE"] = format_date(start_datetime)
    if end_datetime
      params["DTEND;VALUE=DATE"] = format_date(end_datetime + one_day)
    else
      params["DTEND;VALUE=DATE"] = format_date(start_datetime + one_day)
    end
  else
    params[:DTSTART] = utc_datetime(start_datetime)
    if end_datetime
      params[:DTEND] = utc_datetime(end_datetime)
    else
      params[:DTEND] = utc_datetime(start_datetime + 60*60) # 1 hour later
    end
  end
  params[:SUMMARY] = url_encode_ical(title)
  if organizer
    params[:ORGANIZER] = url_encode_ical("CN=\"#{organizer[:name]}\":mailto:#{organizer[:email]}")
  end
  params[:URL] = url_encode(url) if url
  params[:DESCRIPTION] = url_encode_ical(description) if description
  if add_url_to_description && url
    if params[:DESCRIPTION]
      params[:DESCRIPTION] << "\\n\\n#{url_encode(url)}"
    else
      params[:DESCRIPTION] = url_encode(url)
    end
  end
  params[:LOCATION] = url_encode_ical(location) if location
  params[:UID] = "-#{url_encode(url)}" if url
  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
  
  new_line = "%0A"
  params.each do |key, value|
    if key == :ORGANIZER
      calendar_url << "#{new_line}#{key};#{value}"
    else
      calendar_url << "#{new_line}#{key}:#{value}"
    end
  end

  calendar_url << "%0AEND:VEVENT%0AEND:VCALENDAR"

  return calendar_url
end

#office365_urlObject



148
149
150
151
# File 'lib/add_to_calendar.rb', line 148

def office365_url
  # Eg. https://outlook.office.com/calendar/0/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
  microsoft("office365")
end

#outlook_com_urlObject



153
154
155
156
# File 'lib/add_to_calendar.rb', line 153

def outlook_com_url
  # Eg. https://outlook.live.com/calendar/0/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
  microsoft("outlook.com")
end

#outlook_urlObject



216
217
218
# File 'lib/add_to_calendar.rb', line 216

def outlook_url
  ical_url
end

#yahoo_urlObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/add_to_calendar.rb', line 55

def yahoo_url
  # Eg. https://calendar.yahoo.com/?v=60&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
  calendar_url = "https://calendar.yahoo.com/?v=60"
  params = {}
  params[:title] = url_encode(title)
  if all_day
    params[:st] = format_date(start_datetime)
    if end_datetime
      params[:et] = format_date(end_datetime)
    else
      params[:et] = format_date(start_datetime)
    end
    params[:dur] = "allday"
  else
    params[:st] = utc_datetime(start_datetime)
    if end_datetime
      seconds = duration_seconds(start_datetime, end_datetime)
      params[:dur] = seconds_to_hours_minutes(seconds)
    else
      params[:dur] = "0100" 
    end
  end

  params[:desc] = url_encode(description) if description
  if add_url_to_description && url
    if params[:desc]
      params[:desc] << url_encode("\n\n#{url}")
    else
      params[:desc] = url_encode(url)
    end
  end
  params[:in_loc] = url_encode(location) if location

  params.each do |key, value|
    calendar_url << "&#{yahoo_param(key)}=#{value}"
  end
  
  return calendar_url
end