Class: Icalendar::Google::Calendar

Inherits:
Calendar
  • Object
show all
Defined in:
lib/icalendar/gcal/google_calendar.rb

Constant Summary collapse

ENDPOINT =
"https://calendar.google.com/calendar"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#google_idObject

Returns the value of attribute google_id.



10
11
12
# File 'lib/icalendar/gcal/google_calendar.rb', line 10

def google_id
  @google_id
end

#ical_urlObject

Returns the value of attribute ical_url.



10
11
12
# File 'lib/icalendar/gcal/google_calendar.rb', line 10

def ical_url
  @ical_url
end

Class Method Details

.from_google_id(id) ⇒ Object



56
57
58
59
60
61
# File 'lib/icalendar/gcal/google_calendar.rb', line 56

def from_google_id(id)
  url = "#{ENDPOINT}/ical/#{CGI::escape(id)}/public/basic.ics"
  cal = from_ical_url(url)
  cal&.google_id = id
  cal
end

.from_ical_url(url) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/icalendar/gcal/google_calendar.rb', line 46

def from_ical_url(url)
  cid = url[%r{#{ENDPOINT}/ical/(.*?)/public/basic.ics}, 1]
  uri = URI.parse(url)
  res = Net::HTTP.get(uri)
  cal = parse(res).first
  cal&.ical_url  = url
  cal&.google_id = CGI.unescape(cid)
  cal
end

Instance Method Details

#cidObject



12
13
14
# File 'lib/icalendar/gcal/google_calendar.rb', line 12

def cid
  Base64.encode64(google_id.to_s).gsub(%r{\n|=+\Z}, "")
end

#eid(event) ⇒ Object



16
17
18
19
# File 'lib/icalendar/gcal/google_calendar.rb', line 16

def eid(event)
  seed = "#{event.uid.split(%r{@}).first} #{google_id&.slice(0, 28)}"
  Base64.encode64(seed).gsub(%r{\n|=+\Z}, "")
end

#event_url(event) ⇒ Object



21
22
23
# File 'lib/icalendar/gcal/google_calendar.rb', line 21

def event_url(event)
  "#{ENDPOINT}/event?eid=#{eid(event)}"
end

#events_on(date) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/icalendar/gcal/google_calendar.rb', line 25

def events_on(date)
  events.select do |event|
    start = tz.utc_to_local event.dtstart.to_datetime
    stop  = tz.utc_to_local event.dtend.to_datetime
    start.to_date <= date && date <= stop.to_date
  end
end

#google_urlObject



33
34
35
# File 'lib/icalendar/gcal/google_calendar.rb', line 33

def google_url
  "#{ENDPOINT}/r?cid=#{cid}"
end

#tzObject



37
38
39
# File 'lib/icalendar/gcal/google_calendar.rb', line 37

def tz
  TZInfo::Timezone.get(x_wr_timezone.first.to_s)
end

#webcal_urlObject



41
42
43
# File 'lib/icalendar/gcal/google_calendar.rb', line 41

def webcal_url
  ical_url&.sub(%r{\Ahttps?://}, "webcal://")
end