class GoogleR::Event

Attributes

calendar[RW]
created[RW]
description[RW]
end_time[RW]
end_time_zone[RW]
etag[RW]
google_id[RW]
start_time[RW]
start_time_zone[RW]
status[RW]
summary[RW]
updated[RW]
visibility[RW]

Public Class Methods

api_headers() click to toggle source
# File lib/google_r/event.rb, line 16
def self.api_headers
  {
    'GData-Version' => '3.0',
    'Content-Type' => 'application/json',
  }
end
from_json(json, *attrs) click to toggle source
# File lib/google_r/event.rb, line 35
def self.from_json(json, *attrs)
  calendar = attrs[0].calendar

  if json["kind"] == "calendar#events"
    (json["items"] || []).map { |e| from_json(e, *attrs) }
  elsif json["kind"] == "calendar#event"
    event = self.new(calendar)
    event.google_id = json["id"]
    event.etag = json["etag"]
    event.description = json["description"]
    event.summary = json["summary"]
    event.visibility = json["visibility"]
    event.status = json["status"]

    start_time = json["start"]["dateTime"] || json["start"]["date"]
    if start_time
      event.start_time = Time.parse(start_time)
      event.start_time_zone = json["start"]["timeZone"]
    end

    end_time = json["end"]["dateTime"] || json["end"]["date"]
    if end_time
      event.end_time = Time.parse(end_time)
      event.end_time_zone = json["end"]["timeZone"]
    end

    event.updated = Time.parse(json["updated"]) if json['updated']
    event.created = Time.parse(json["created"]) if json['created']
    event
  else
    raise "Not implemented:\n#{json.inspect}"
  end
end
new(calendar, opts = {}) click to toggle source
# File lib/google_r/event.rb, line 7
def initialize(calendar, opts = {})
  self.calendar = calendar
  self.visibility = opts[:visibility] || "private"
end
path(calendar_google_id) click to toggle source
# File lib/google_r/event.rb, line 31
def self.path(calendar_google_id)
  "/calendar/v3/calendars/#{calendar_google_id}/events"
end
url() click to toggle source
# File lib/google_r/event.rb, line 12
def self.url
  "https://www.googleapis.com"
end

Public Instance Methods

format_time(time) click to toggle source
# File lib/google_r/event.rb, line 96
def format_time(time)
  time.strftime("%Y-%m-%dT%H:%M:%S%:z")
end
new?() click to toggle source
# File lib/google_r/event.rb, line 92
def new?
  self.google_id.nil?
end
path() click to toggle source
# File lib/google_r/event.rb, line 23
def path
  if new?
    "/calendar/v3/calendars/#{calendar.google_id}/events"
  else
    "/calendar/v3/calendars/#{calendar.google_id}/events/#{google_id}"
  end
end
to_google(yajl_opts = {}) click to toggle source
# File lib/google_r/event.rb, line 69
def to_google(yajl_opts = {})
  hash = {
    "kind" => "calendar#event",
  }
  hash["etag"] = etag if etag
  hash["id"] = google_id if google_id
  hash["description"] = description if description
  hash["summary"] = summary if summary
  start = {}
  start["dateTime"] = format_time(start_time) if start_time
  start["date"] = nil if start_time
  start["timeZone"] = start_time_zone if start_time_zone
  finish = {}
  finish["dateTime"] = format_time(end_time) if end_time
  finish["date"] = nil if end_time
  finish["timeZone"] = end_time_zone if end_time_zone
  hash["start"] = start
  hash["end"] = finish
  hash["visibility"] = visibility if visibility
  hash["status"] = status if status
  Yajl::Encoder.encode(hash, yajl_opts)
end