Class: AddToCalendarLinks::URLs

Inherits:
Object
  • Object
show all
Defined in:
lib/add_to_calendar_links.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, organizer: nil, strip_html: false, sequence: nil, last_modified: Time.now.utc, uid:) ⇒ URLs

Returns a new instance of URLs.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/add_to_calendar_links.rb', line 16

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, sequence: nil, last_modified: Time.now.utc, uid:)
  @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
  @organizer = organizer if organizer
  @strip_html = strip_html
  @sequence = sequence
  @uid = uid
  @last_modified = last_modified
  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_links.rb', line 15

def add_url_to_description
  @add_url_to_description
end

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#end_datetimeObject

Returns the value of attribute end_datetime.



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

def end_datetime
  @end_datetime
end

#last_modifiedObject

Returns the value of attribute last_modified.



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

def last_modified
  @last_modified
end

#locationObject

Returns the value of attribute location.



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

def location
  @location
end

#organizerObject

Returns the value of attribute organizer.



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

def organizer
  @organizer
end

#sequenceObject

Returns the value of attribute sequence.



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

def sequence
  @sequence
end

#start_datetimeObject

Returns the value of attribute start_datetime.



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

def start_datetime
  @start_datetime
end

#strip_htmlObject

Returns the value of attribute strip_html.



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

def strip_html
  @strip_html
end

#timezoneObject

Returns the value of attribute timezone.



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

def timezone
  @timezone
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#uidObject

Returns the value of attribute uid.



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

def uid
  @uid
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#android_urlObject



204
205
206
# File 'lib/add_to_calendar_links.rb', line 204

def android_url
  ical_url
end

#apple_urlObject



196
197
198
# File 'lib/add_to_calendar_links.rb', line 196

def apple_url
  ical_url
end

#google_urlObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/add_to_calendar_links.rb', line 33

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)
  if end_datetime
    params[:dates] = "#{format_date_google(start_datetime)}/#{format_date_google(end_datetime)}"
  else
    params[:dates] = "#{format_date_google(start_datetime)}/#{format_date_google(start_datetime + 60*60)}" # end time is 1 hour later
  end
  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

#ical_fileObject



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
147
# File 'lib/add_to_calendar_links.rb', line 103

def ical_file
  calendar_url = "BEGIN:VCALENDAR\nVERSION:2.0\nMETHOD:REQUEST\nBEGIN:VEVENT"
  
  params = {}
  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
  params[:SUMMARY] = strip_html_tags(title) #ical doesnt support html so remove all markup. Optional for other formats
  params[:URL] = url if url
  description.gsub!(/\n/, "\\n")
  params[:DESCRIPTION] = strip_html_tags(description).strip if description
  if add_url_to_description && url
    if params[:DESCRIPTION]
      params[:DESCRIPTION] << "\\n\\n#{url}"
    else
      params[:DESCRIPTION] = url
    end
  end
  params[:LOCATION] = strip_html_tags(location) if location
  if uid
    params[:UID] = uid
  else
    params[:UID] = "-#{urlc}" if url
    params[:UID] = "-#{utc_datetime(start_datetime)}-#{title}" unless params[:UID] # set uid based on starttime and title only if url is unavailable
  end
  params[:ORGANIZER] = organizer if organizer
  params[:SEQUENCE] = sequence if sequence
  params["LAST-MODIFIED"] = format_date_google(last_modified) if last_modified
  params[:METHOD] = "REQUEST"

  params.each do |key, value|
    if key == :ORGANIZER
      calendar_url << "\n#{key}#{value}"
    else
      calendar_url << "\n#{key}:#{value}"
    end
  end

  calendar_url << "\nEND:VEVENT\nEND:VCALENDAR"

  return calendar_url
end

#ical_urlObject



149
150
151
152
153
154
155
156
157
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
# File 'lib/add_to_calendar_links.rb', line 149

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%0AMETHOD:REQUEST%0ABEGIN:VEVENT"

  params = {}
  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
  params[:SUMMARY] = url_encode_ical(title, strip_html: true) #ical doesnt support html so remove all markup. Optional for other formats
  params[:URL] = url_encode(url) if url
  params[:DESCRIPTION] = url_encode_ical(strip_html_tags(description, line_break_seperator: "\n")) 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
  if uid
    params[:UID] = uid
  else
    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
  end
  params[:ORGANIZER] = organizer if organizer
  params[:SEQUENCE] = sequence if sequence
  params["LAST-MODIFIED"] = format_date_google(last_modified) if last_modified

  new_line = "%0A"
  params.each do |key, value|
    if key == :ORGANIZER
      calendar_url << "\n#{key}#{value}"
    else
      calendar_url << "\n#{key}:#{value}"
    end
  end

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

  return calendar_url
end

#office365_urlObject



93
94
95
96
# File 'lib/add_to_calendar_links.rb', line 93

def office365_url
  # 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
  microsoft("office365")
end

#outlook_com_urlObject



98
99
100
101
# File 'lib/add_to_calendar_links.rb', line 98

def outlook_com_url
  # 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
  microsoft("outlook.com")
end

#outlook_urlObject



200
201
202
# File 'lib/add_to_calendar_links.rb', line 200

def outlook_url
  ical_url
end

#yahoo_urlObject



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
# File 'lib/add_to_calendar_links.rb', line 61

def yahoo_url
  # 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
  calendar_url = "https://calendar.yahoo.com/?v=60&VIEW=d&TYPE=20"
  params = {}
  params[:TITLE] = url_encode(title)
  params[:ST] = utc_datetime(start_datetime)
  if end_datetime
    # params[:ET] = utc_datetime(end_datetime)
    seconds = duration_seconds(start_datetime, end_datetime)
    params[:DUR] = seconds_to_hours_minutes(seconds)
  else
    # params[:ET] = utc_datetime(start_datetime + 60*60)
    params[:DUR] = "0100" 
  end
  params[:DESC] = url_encode(strip_html_tags(description)).truncate(3000) 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 << "&#{key}=#{value}"
  end
  
  return calendar_url
end