Module: Icalendar::Google::Parsers

Included in:
Calendar
Defined in:
lib/icalendar/google/parsers.rb

Instance Method Summary collapse

Instance Method Details

#from_google_id(id) ⇒ Object



6
7
8
9
10
# File 'lib/icalendar/google/parsers.rb', line 6

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

#from_url(url) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/icalendar/google/parsers.rb', line 12

def from_url(url)
  cid = url[%r{https://calendar.google.com/calendar/ical/(.*?)/public/basic.ics}, 1]
  uri = URI.parse(url)
  ssl = uri.scheme == "https"
  # require "pry"; binding.pry
  body = Net::HTTP.start(uri.host, uri.port, use_ssl: ssl) do |http|
    req = Net::HTTP::Get.new(uri.request_uri)
    res = http.request(req)
    enc = res['content-type'][%r{charset=(.*)}i, 1]
    enc.nil? ? res.body : res.body.force_encoding(enc)
  end
  Calendar.parse(body).each do |calendar|
    calendar.ical_url  = url
    calendar.google_id = CGI.unescape(cid)
  end
end