Class: GoogleClient::Event

Inherits:
Object
  • Object
show all
Includes:
Format
Defined in:
lib/google_client/event.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Format

#decode_response, #json?

Constructor Details

#initialize(params = {}) ⇒ Event

Returns a new instance of Event.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/google_client/event.rb', line 19

def initialize(params = {})
  @id = params[:event_id] || params[:id] || nil
  @title = params[:title]
  @description = params[:description]
  @location = params[:location]
  @start_time = params[:start_time]
  @end_time = params[:end_time]
  @calendar_id = params[:calendar_id]
  @calendar = params[:calendar]
  @comments = params[:comments]
  @attendees = params[:attendees].nil? ? [] : params[:attendees].map{|x| x.kind_of?(String) ? {:email => x} : x}
  @send_event_notifications = params[:send_event_notifications]

  @calendar.nil? or @connection = @calendar.connection
  @json_mode = true
  block_given? and yield self
end

Instance Attribute Details

#attendeesObject

Returns the value of attribute attendees.



11
12
13
# File 'lib/google_client/event.rb', line 11

def attendees
  @attendees
end

#calendarObject

Returns the value of attribute calendar.



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

def calendar
  @calendar
end

#calendar_idObject

Returns the value of attribute calendar_id.



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

def calendar_id
  @calendar_id
end

#commentsObject

Returns the value of attribute comments.



17
18
19
# File 'lib/google_client/event.rb', line 17

def comments
  @comments
end

#descriptionObject

Returns the value of attribute description.



10
11
12
# File 'lib/google_client/event.rb', line 10

def description
  @description
end

#end_timeObject

Returns the value of attribute end_time.



13
14
15
# File 'lib/google_client/event.rb', line 13

def end_time
  @end_time
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/google_client/event.rb', line 6

def id
  @id
end

#locationObject

Returns the value of attribute location.



14
15
16
# File 'lib/google_client/event.rb', line 14

def location
  @location
end

#send_event_notificationsObject

Returns the value of attribute send_event_notifications.



16
17
18
# File 'lib/google_client/event.rb', line 16

def send_event_notifications
  @send_event_notifications
end

#start_timeObject

Returns the value of attribute start_time.



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

def start_time
  @start_time
end

#titleObject

Returns the value of attribute title.



9
10
11
# File 'lib/google_client/event.rb', line 9

def title
  @title
end

Class Method Details

.build_event(data, calendar = nil) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/google_client/event.rb', line 118

def build_event(data, calendar = nil)
  data.nil? and return Event.new

  attendees = data["gd$who"]
  attendees.nil? or attendees = attendees.inject([]) do |values, attendee| 
    if attendee.has_key?("gd$attendeeStatus")
      values << { :name   => attendee["valueString"], 
        :email  => attendee["email"], 
        :status => attendee["gd$attendeeStatus"]["value"].split("\.").last}
    end
    values
  end

  start_time = data["gd$when"][0]["startTime"]
  end_time = data["gd$when"][0]["endTime"]

  Event.new({:id => data["id"]["$t"].split("full/").last, 
              :calendar => calendar, 
              :title => data["title"]["$t"],
              :description => data["content"]["$t"],
              :location => data["gd$where"][0]["valueString"],
              :comments => data["gd$comments"],
              :attendees => attendees,
              :start_time => start_time,
              :end_time =>  end_time})
end

.create(params) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/google_client/event.rb', line 110

def create(params)
  event = if block_given?
    new(params, &Proc.new)
  else
    new(params)
  end
end

Instance Method Details

#connectionObject



54
55
56
# File 'lib/google_client/event.rb', line 54

def connection
  @connection or raise RuntimeError.new "Http connection not established"
end

#deleteObject

Delete the Event from the server

Return

  • true if sucessful

  • raise Error if failure



89
90
91
92
93
# File 'lib/google_client/event.rb', line 89

def delete
  @id.nil? and raise Error.new "event cannot be deleted if has not an unique identifier"
  connection.delete "/calendar/feeds/#{calendar}/private/full/#{@id}", nil,  {"If-Match" => "*"}
  true
end

#fetchObject

Fetch the specific event from Google Calendar

Return

  • Event



99
100
101
102
103
104
105
106
# File 'lib/google_client/event.rb', line 99

def fetch
  if @calendar_id.nil?
    @calendar.nil? and raise Error.new "calendar or calendar_id must be valid in the event"
    @calendar_id = @calendar.id
  end
  data = decode_response connection.get "/calendar/feeds/#{@calendar_id}/private/full/#{@id}"
  self.class.build_event data["entry"], @calendar
end

#saveObject

Save the Event in the server

Return

  • Event instance



62
63
64
65
66
67
68
69
70
# File 'lib/google_client/event.rb', line 62

def save
  if @id.nil?
    data = decode_response connection.post("/calendar/feeds/#{calendar}/private/full", {:data => self.to_hash})
    @id = data["data"]["id"]
  else
    # put
  end
  self
end

#to_hashObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/google_client/event.rb', line 41

def to_hash
  {
    :title => @title,
    :details => @description,
    :timeZone => @timezone,
    :when => [{:start => @start_time,
      :end => @end_time}],
    :attendees => @attendees.map{|x| {:valueString => x[:name], :email => x[:email]}},
    :sendEventNotifications  => @send_event_notifications || false
  }.delete_if{|k,v| v.nil?}
end

#to_sObject



37
38
39
# File 'lib/google_client/event.rb', line 37

def to_s
  "#{self.class.name} => { id: #{@id}, title: #{@title}, description: #{@description}, :start_time => #{@start_time}, :end_time => #{@end_time}, :location => #{@location}, :attendees => #{@attendees}, :send_event_notifications => #{@send_event_notifications}, :calendar => #{@calendar} }"
end