Class: GoogleClient::Calendar

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

Constant Summary collapse

BASE_URL =
"https://www.google.com/calendar/feeds"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Format

#decode_response, #json?

Constructor Details

#initialize(params) ⇒ Calendar

Returns a new instance of Calendar.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/google_client/calendar.rb', line 14

def initialize(params)
  @user = params[:user]
  @user.nil? or @connection = @user.connection
  @id = params[:calendar_id] || params[:id]
  @title = params[:title]
  @details = params[:details]
  @timezone = params[:timezone]
  @location = params[:location]
  @json_mode = true
  block_given? and yield self
end

Instance Attribute Details

#detailsObject

Returns the value of attribute details.



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

def details
  @details
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#locationObject

Returns the value of attribute location.



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

def location
  @location
end

#timezoneObject

Helper to get the Timezone in rfc3339 format



133
134
135
# File 'lib/google_client/calendar.rb', line 133

def timezone
  @timezone
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

Class Method Details

.build_calendar(data, user = nil) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/google_client/calendar.rb', line 149

def build_calendar(data, user = nil)

  id = begin
    data["id"]["$t"].split("full/").last
  rescue
    nil
  end

  details = begin
    data["summary"]["$t"]
  rescue
    ""
  end
  title = begin
    data["title"]["$t"]
  rescue
    ""
  end

  timezone = begin
    data["gCal$timezone"]["value"]
  rescue
    nil
  end

  location = begin
    data["gd$where"][0]["valueString"]
  rescue
    nil
  end
  
  Calendar.new({:id => id, 
                :user => user, 
                :title => title,
                :details => details,
                :location => location,
                :timezone => timezone})
end

.create(params) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/google_client/calendar.rb', line 141

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

Instance Method Details

#connectionObject



30
31
32
# File 'lib/google_client/calendar.rb', line 30

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

#create_event(params = {}) ⇒ Object

Create a new event in the calendar

Parameters

  • params Hash options

    • c.send_event_notifications : true | false

    • c.title : event title (string)

    • c.description : event detail description (string)

    • c.start_time : start time (time format)

    • c.end_time : end time (time format)

    • c.attendees : array of emails or hashes with :name and :email



115
116
117
118
119
120
121
122
# File 'lib/google_client/calendar.rb', line 115

def create_event(params = {})
  event = if block_given?
                Event.create(params.merge({:calendar => self}), &Proc.new)
              else
                Event.create(params.merge({:calendar => self}))
              end
  event.save
end

#deleteObject



48
49
50
51
52
# File 'lib/google_client/calendar.rb', line 48

def delete
  @id.nil? and raise Error.new "calendar cannot be deleted if has not an unique identifier"
  connection.delete("/calendar/feeds/default/owncalendars/full/#{@id}")
  true
end

#delete_event(event_id) ⇒ Object



124
125
# File 'lib/google_client/calendar.rb', line 124

def delete_event(event_id)
end

#events(args = nil) ⇒ Object

Fetch a set of the events from the calendar

Parameters

  • params: Hash

** id: optional, specific event identifier. If present, no other parameter will be considered ** from: optional, if to is present and from is not, all events starting till to will be fetched ** to: optional, if from is present and to is not, all events starting after from will be fetched

Return

  • Nil if no event matches the query parameters.

  • Event instance if only one Event matches the query parameter.

  • An array of Event instances if more than one Event matches the query parameter.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/google_client/calendar.rb', line 76

def events(args = nil)
  events = if args.nil? || args.eql?(:all)
    events = decode_response connection.get "/calendar/feeds/#{id}/private/full"
    events = events["feed"]["entry"]
    events.map{ |event| Event.build_event(event, self)}
  elsif args.is_a?(String)
    Event.new({:id => args, :calendar => self}).fetch
  elsif args.is_a?(Hash)
    if args.is_a?(Hash) && args.has_key?(:id)
      Event.new({:id => args[:id], :calendar => self}).fetch
    else
      params = { "start-min" => args[:from],
                 "start-max" => args[:to]}
      events = decode_response connection.get "/calendar/feeds/#{id}/private/full", params
      events = events["feed"]["entry"]
      events = events.nil? ? [] : events.map{ |event| Event.build_event(event, self)}
    end
  else
    raise ArgumentError.new "Invalid argument type #{args.class}"
  end

end

#fetchObject



127
128
129
130
# File 'lib/google_client/calendar.rb', line 127

def fetch
  data = decode_response connection.get "/calendar/feeds/default/owncalendars/full/#{id}"
  self.class.build_calendar data["entry"], @user
end

#forthcoming_events(time = 3600) ⇒ Object

Fetch forthcoming events in the following time minutes



101
102
103
104
# File 'lib/google_client/calendar.rb', line 101

def forthcoming_events(time = 3600)
  events({:from => Time.now.strftime("%Y-%m-%dT%k:%M:%S").concat(timezone).gsub(/ /,"0"),
          :to => (Time.now + time).strftime("%Y-%m-%dT%k:%M:%S").concat(timezone).gsub(/ /,"0")})
end

#saveObject

Save the Calendar in the server

Returns:

  • Event instance



38
39
40
41
42
43
44
45
46
# File 'lib/google_client/calendar.rb', line 38

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

#to_hashObject



54
55
56
57
58
59
60
61
# File 'lib/google_client/calendar.rb', line 54

def to_hash
  {
    :title => @title,
    :details => @details,
    :timeZone => @timezone,
    :location => @location
  }.delete_if{|k,v| v.nil?}
end

#to_sObject



26
27
28
# File 'lib/google_client/calendar.rb', line 26

def to_s
  "#{self.class.name} => { id: #{@id}, title: #{@title}, timezone => #{@timezone}, location => #{@location} }"
end