Class: Icalendar::GoogleCalendar

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#google_idObject

Returns the value of attribute google_id.



7
8
9
# File 'lib/icalendar/gcal.rb', line 7

def google_id
  @google_id
end

#ical_urlObject

Returns the value of attribute ical_url.



7
8
9
# File 'lib/icalendar/gcal.rb', line 7

def ical_url
  @ical_url
end

Class Method Details

.from_google_id(id) ⇒ Object



53
54
55
56
57
58
# File 'lib/icalendar/gcal.rb', line 53

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

.from_ical_url(url) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/icalendar/gcal.rb', line 43

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

Instance Method Details

#cidObject



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

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

#eid(event) ⇒ Object



13
14
15
16
# File 'lib/icalendar/gcal.rb', line 13

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

#event_url(event) ⇒ Object



18
19
20
# File 'lib/icalendar/gcal.rb', line 18

def event_url(event)
  "https://calendar.google.com/calendar/event?eid=#{eid event}"
end

#events_on(date) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/icalendar/gcal.rb', line 22

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



30
31
32
# File 'lib/icalendar/gcal.rb', line 30

def google_url
  "https://calendar.google.com/calendar/r?cid=#{cid}"
end

#tzObject



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

def tz
  TZInfo::Timezone.get self.x_wr_timezone.first
end

#webcal_urlObject



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

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