Class: GoogleR::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/google_r/event.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(calendar, opts = {}) ⇒ Event

Returns a new instance of Event.



7
8
9
10
# File 'lib/google_r/event.rb', line 7

def initialize(calendar, opts = {})
  self.calendar = calendar
  self.visibility = opts[:visibility] || "private"
end

Instance Attribute Details

#calendarObject

Returns the value of attribute calendar.



4
5
6
# File 'lib/google_r/event.rb', line 4

def calendar
  @calendar
end

#createdObject

Returns the value of attribute created.



4
5
6
# File 'lib/google_r/event.rb', line 4

def created
  @created
end

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/google_r/event.rb', line 4

def description
  @description
end

#end_timeObject

Returns the value of attribute end_time.



4
5
6
# File 'lib/google_r/event.rb', line 4

def end_time
  @end_time
end

#end_time_zoneObject

Returns the value of attribute end_time_zone.



4
5
6
# File 'lib/google_r/event.rb', line 4

def end_time_zone
  @end_time_zone
end

#etagObject

Returns the value of attribute etag.



4
5
6
# File 'lib/google_r/event.rb', line 4

def etag
  @etag
end

#google_idObject

Returns the value of attribute google_id.



4
5
6
# File 'lib/google_r/event.rb', line 4

def google_id
  @google_id
end

#start_timeObject

Returns the value of attribute start_time.



4
5
6
# File 'lib/google_r/event.rb', line 4

def start_time
  @start_time
end

#start_time_zoneObject

Returns the value of attribute start_time_zone.



4
5
6
# File 'lib/google_r/event.rb', line 4

def start_time_zone
  @start_time_zone
end

#statusObject

Returns the value of attribute status.



4
5
6
# File 'lib/google_r/event.rb', line 4

def status
  @status
end

#summaryObject

Returns the value of attribute summary.



4
5
6
# File 'lib/google_r/event.rb', line 4

def summary
  @summary
end

#updatedObject

Returns the value of attribute updated.



4
5
6
# File 'lib/google_r/event.rb', line 4

def updated
  @updated
end

#visibilityObject

Returns the value of attribute visibility.



4
5
6
# File 'lib/google_r/event.rb', line 4

def visibility
  @visibility
end

Class Method Details

.api_headersObject



16
17
18
19
20
21
# 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) ⇒ Object



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
60
61
62
63
64
65
66
67
# 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

.path(calendar_google_id) ⇒ Object



31
32
33
# File 'lib/google_r/event.rb', line 31

def self.path(calendar_google_id)
  "/calendar/v3/calendars/#{calendar_google_id}/events"
end

.urlObject



12
13
14
# File 'lib/google_r/event.rb', line 12

def self.url
  "https://www.googleapis.com"
end

Instance Method Details

#format_time(time) ⇒ Object



96
97
98
# File 'lib/google_r/event.rb', line 96

def format_time(time)
  time.strftime("%Y-%m-%dT%H:%M:%S%:z")
end

#new?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/google_r/event.rb', line 92

def new?
  self.google_id.nil?
end

#pathObject



23
24
25
26
27
28
29
# 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 = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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