groesser3-gcal4ruby 0.5.51
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +83 -0
- data/README.md +99 -0
- data/lib/gcal4ruby.rb +1 -0
- data/lib/gcal4ruby/calendar.rb +371 -0
- data/lib/gcal4ruby/event.rb +446 -0
- data/lib/gcal4ruby/recurrence.rb +263 -0
- data/lib/gcal4ruby/service.rb +160 -0
- data/test/unit.rb +182 -0
- metadata +72 -0
@@ -0,0 +1,160 @@
|
|
1
|
+
# Author:: Mike Reich (mike@seabourneconsulting.com)
|
2
|
+
# Copyright:: Copyright (C) 2010 Mike Reich
|
3
|
+
# License:: GPL v2
|
4
|
+
#--
|
5
|
+
# Licensed under the General Public License (GPL), Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
#
|
15
|
+
# Feel free to use and update, but be sure to contribute your
|
16
|
+
# code back to the project and attribute as required by the license.
|
17
|
+
#++
|
18
|
+
require 'gdata4ruby'
|
19
|
+
require 'gdata4ruby/gdata_object'
|
20
|
+
require 'gdata4ruby/utils/utils'
|
21
|
+
require 'gdata4ruby/acl/access_rule'
|
22
|
+
require 'gcal4ruby/calendar'
|
23
|
+
require 'gcal4ruby/event'
|
24
|
+
require 'gcal4ruby/recurrence'
|
25
|
+
require 'rexml/document'
|
26
|
+
|
27
|
+
module GCal4Ruby
|
28
|
+
#The service class is the main handler for all direct interactions with the
|
29
|
+
#Google Calendar API. A service represents a single user account. Each user
|
30
|
+
#account can have multiple calendars, so you'll need to find the calendar you
|
31
|
+
#want from the service, using the Calendar#find class method.
|
32
|
+
#=Usage
|
33
|
+
#
|
34
|
+
#1. Authenticate
|
35
|
+
# service = Service.new
|
36
|
+
# service.authenticate("user@gmail.com", "password")
|
37
|
+
#
|
38
|
+
#2. Get Calendar List
|
39
|
+
# calendars = service.calendars
|
40
|
+
#
|
41
|
+
class Service < GData4Ruby::Service
|
42
|
+
CALENDAR_LIST_FEED = 'https://www.google.com/calendar/feeds/default/allcalendars/full'
|
43
|
+
|
44
|
+
#Convenience attribute contains the currently authenticated account name
|
45
|
+
attr_reader :account
|
46
|
+
|
47
|
+
# The token returned by the Google servers, used to authorize all subsequent messages
|
48
|
+
attr_reader :auth_token
|
49
|
+
|
50
|
+
# Determines whether GCal4Ruby ensures a calendar is public. Setting this to false can increase speeds by
|
51
|
+
# 50% but can cause errors if you try to do something to a calendar that is not public and you don't have
|
52
|
+
# adequate permissions
|
53
|
+
attr_accessor :check_public
|
54
|
+
|
55
|
+
#Accepts an optional attributes hash for initialization values
|
56
|
+
def initialize(attributes = {})
|
57
|
+
super(attributes)
|
58
|
+
attributes.each do |key, value|
|
59
|
+
self.send("#{key}=", value)
|
60
|
+
end
|
61
|
+
@check_public ||= true
|
62
|
+
end
|
63
|
+
|
64
|
+
def default_event_feed
|
65
|
+
return "https://www.google.com/calendar/feeds/#{@account}/private/full"
|
66
|
+
end
|
67
|
+
|
68
|
+
# The authenticate method passes the username and password to google servers.
|
69
|
+
# If authentication succeeds, returns true, otherwise raises the AuthenticationFailed error.
|
70
|
+
def authenticate(username, password, service='cl')
|
71
|
+
super(username, password, service)
|
72
|
+
end
|
73
|
+
|
74
|
+
#Helper function to reauthenticate to a new Google service without having to re-set credentials.
|
75
|
+
def reauthenticate(service='cl')
|
76
|
+
authenticate(@account, @password, service)
|
77
|
+
end
|
78
|
+
|
79
|
+
#Returns an array of Calendar objects for each calendar associated with
|
80
|
+
#the authenticated account.
|
81
|
+
def calendars
|
82
|
+
if not @auth_token
|
83
|
+
raise NotAuthenticated
|
84
|
+
end
|
85
|
+
ret = send_request(GData4Ruby::Request.new(:get, CALENDAR_LIST_FEED, nil, {"max-results" => "10000"}))
|
86
|
+
cals = []
|
87
|
+
REXML::Document.new(ret.body).root.elements.each("entry"){}.map do |entry|
|
88
|
+
entry = GData4Ruby::Utils.add_namespaces(entry)
|
89
|
+
cal = Calendar.new(self)
|
90
|
+
cal.load(entry.to_s)
|
91
|
+
cals << cal
|
92
|
+
end
|
93
|
+
return cals
|
94
|
+
end
|
95
|
+
|
96
|
+
#Returns an array of Event objects for each event in this account
|
97
|
+
def events
|
98
|
+
if not @auth_token
|
99
|
+
raise NotAuthenticated
|
100
|
+
end
|
101
|
+
ret = send_request(GData4Ruby::Request.new(:get, default_event_feed, nil, {"max-results" => "10000"}))
|
102
|
+
events = []
|
103
|
+
REXML::Document.new(ret.body).root.elements.each("entry"){}.map do |entry|
|
104
|
+
entry = GData4Ruby::Utils.add_namespaces(entry)
|
105
|
+
event = Event.new(self)
|
106
|
+
event.load(entry.to_s)
|
107
|
+
events << event
|
108
|
+
end
|
109
|
+
return events
|
110
|
+
end
|
111
|
+
|
112
|
+
#Helper function to return a formatted iframe embedded google calendar. Parameters are:
|
113
|
+
#1. *cals*: either an array of calendar ids, or <em>:all</em> for all calendars, or <em>:first</em> for the first (usally default) calendar
|
114
|
+
#2. *params*: a hash of parameters that affect the display of the embedded calendar. Accepts any parameter that the google iframe recognizes. Here are the most common:
|
115
|
+
# height:: the height of the embedded calendar in pixels
|
116
|
+
# width:: the width of the embedded calendar in pixels
|
117
|
+
# title:: the title to display
|
118
|
+
# bgcolor:: the background color. Limited choices, see google docs for allowable values.
|
119
|
+
# showTitle:: set to '0' to hide the title
|
120
|
+
# showDate:: set to '0' to hide the current date
|
121
|
+
# showNav:: set to '0 to hide the navigation tools
|
122
|
+
# showPrint:: set to '0' to hide the print icon
|
123
|
+
# showTabs:: set to '0' to hide the tabs
|
124
|
+
# showCalendars:: set to '0' to hide the calendars selection drop down
|
125
|
+
# showTz:: set to '0' to hide the timezone selection
|
126
|
+
# border:: the border width in pixels
|
127
|
+
# dates:: a range of dates to display in the format of 'yyyymmdd/yyyymmdd'. Example: 20090820/20091001
|
128
|
+
# privateKey:: use to display a private calendar. You can find this key under the calendar settings pane of the Google Calendar website.
|
129
|
+
# ctz:: The timezone to convert event times to
|
130
|
+
#3. *colors*: a hash of calendar ids as key and color values as associated hash values. Example: {'test@gmail.com' => '#7A367A'}
|
131
|
+
def to_iframe(cals, params = {}, colors = {})
|
132
|
+
params[:height] ||= "600"
|
133
|
+
params[:width] ||= "600"
|
134
|
+
params[:title] ||= (self.account ? self.account : '')
|
135
|
+
params[:bgcolor] ||= "#FFFFFF"
|
136
|
+
params[:border] ||= "0"
|
137
|
+
params.each{|key, value| params[key] = CGI::escape(value)}
|
138
|
+
output = "#{params.to_a.collect{|a| a.join("=")}.join("&")}&"
|
139
|
+
|
140
|
+
if cals.is_a?(Array)
|
141
|
+
for c in cals
|
142
|
+
output += "src=#{c}&"
|
143
|
+
if colors and colors[c]
|
144
|
+
output += "color=%23#{colors[c].gsub("#", "")}&"
|
145
|
+
end
|
146
|
+
end
|
147
|
+
elsif cals == :all
|
148
|
+
cal_list = calendars()
|
149
|
+
for c in cal_list
|
150
|
+
output += "src=#{c.id}&"
|
151
|
+
end
|
152
|
+
elsif cals == :first
|
153
|
+
cal_list = calendars()
|
154
|
+
output += "src=#{cal_list[0].id}&"
|
155
|
+
end
|
156
|
+
|
157
|
+
"<iframe src='http://www.google.com/calendar/embed?#{output}' style='#{params[:border]} px solid;' width='#{params[:width]}' height='#{params[:height]}' frameborder='#{params[:border]}' scrolling='no'></iframe>"
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
data/test/unit.rb
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'gcal4ruby'
|
5
|
+
include GCal4Ruby
|
6
|
+
|
7
|
+
@service = Service.new
|
8
|
+
@username = nil
|
9
|
+
@password = nil
|
10
|
+
|
11
|
+
def tester
|
12
|
+
if ARGV.include?("-d")
|
13
|
+
@service.debug = true
|
14
|
+
end
|
15
|
+
ARGV.each do |ar|
|
16
|
+
if ar.match("username=")
|
17
|
+
@username = ar.gsub("username=", "")
|
18
|
+
end
|
19
|
+
if ar.match("password=")
|
20
|
+
@password = ar.gsub("password=", "")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
service_test
|
24
|
+
calendar_test
|
25
|
+
event_test
|
26
|
+
event_recurrence_test
|
27
|
+
end
|
28
|
+
|
29
|
+
def service_test
|
30
|
+
puts "---Starting Service Test---"
|
31
|
+
puts "1. Authenticate"
|
32
|
+
if @service.authenticate(@username, @password)
|
33
|
+
successful
|
34
|
+
else
|
35
|
+
failed
|
36
|
+
end
|
37
|
+
|
38
|
+
puts "2. Calendar List"
|
39
|
+
cals = @service.calendars
|
40
|
+
if cals
|
41
|
+
successful "Calendars for this Account:"
|
42
|
+
cals.each do |cal|
|
43
|
+
puts cal.title
|
44
|
+
end
|
45
|
+
else
|
46
|
+
failed
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def calendar_test
|
51
|
+
puts "---Starting Calendar Test---"
|
52
|
+
|
53
|
+
puts "1. Create Calendar"
|
54
|
+
cal = Calendar.new(@service)
|
55
|
+
cal.title = "test calendar"+Time.now.to_s
|
56
|
+
puts "Calender exists = "+cal.exists?.to_s
|
57
|
+
if cal.save
|
58
|
+
successful cal.to_xml
|
59
|
+
else
|
60
|
+
failed
|
61
|
+
end
|
62
|
+
|
63
|
+
puts "2. Edit Calendar"
|
64
|
+
cal.title = "renamed title"
|
65
|
+
if cal.save
|
66
|
+
successful cal.to_xml
|
67
|
+
else
|
68
|
+
puts "Test 2 Failed"
|
69
|
+
end
|
70
|
+
|
71
|
+
puts "3. Find Calendar by ID"
|
72
|
+
c = Calendar.find(@service, {:id => cal.id})
|
73
|
+
if c.title == cal.title
|
74
|
+
successful
|
75
|
+
else
|
76
|
+
failed "#{c.title} not equal to #{cal.title}"
|
77
|
+
end
|
78
|
+
|
79
|
+
puts "4. Delete Calendar"
|
80
|
+
if cal.delete and not cal.exists?
|
81
|
+
successful
|
82
|
+
else
|
83
|
+
failed
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def event_test
|
88
|
+
puts "---Starting Event Test---"
|
89
|
+
|
90
|
+
puts "1. Create Event"
|
91
|
+
event = Event.new(@service)
|
92
|
+
event.calendar = @service.calendars[0]
|
93
|
+
event.title = "Test Event"
|
94
|
+
event.content = "Test event content"
|
95
|
+
event.start_time = Time.now+1800
|
96
|
+
event.end_time = Time.now+5400
|
97
|
+
if event.save
|
98
|
+
successful event.to_xml
|
99
|
+
else
|
100
|
+
failed
|
101
|
+
end
|
102
|
+
|
103
|
+
puts "2. Edit Event"
|
104
|
+
event.title = "Edited title"
|
105
|
+
if event.save
|
106
|
+
successful event.to_xml
|
107
|
+
else
|
108
|
+
failed
|
109
|
+
end
|
110
|
+
|
111
|
+
puts "3. Reload Event"
|
112
|
+
if event.reload
|
113
|
+
successful
|
114
|
+
end
|
115
|
+
|
116
|
+
puts "4. Find Event by id"
|
117
|
+
e = Event.find(@service, {:id => event.id})
|
118
|
+
if e.title == event.title
|
119
|
+
successful
|
120
|
+
else
|
121
|
+
failed "Found event doesn't match existing event"
|
122
|
+
end
|
123
|
+
|
124
|
+
puts "5. Delete Event"
|
125
|
+
if event.delete
|
126
|
+
successful
|
127
|
+
else
|
128
|
+
failed
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def event_recurrence_test
|
133
|
+
puts "---Starting Event Recurrence Test---"
|
134
|
+
|
135
|
+
@first_start = Time.now
|
136
|
+
@first_end = Time.now+3600
|
137
|
+
@first_freq = {'weekly' => ['TU']}
|
138
|
+
@second_start = Time.now+86000
|
139
|
+
@second_end = Time.now+89600
|
140
|
+
@second_freq = {'weekly' => ['SA']}
|
141
|
+
|
142
|
+
puts "1. Create Recurring Event"
|
143
|
+
event = Event.new(@service)
|
144
|
+
event.calendar = @service.calendars[0]
|
145
|
+
event.title = "Test Recurring Event"
|
146
|
+
event.content = "Test event content"
|
147
|
+
event.recurrence = Recurrence.new({:start_time => @first_start, :end_time => @first_end, :frequency => @first_freq})
|
148
|
+
if event.save
|
149
|
+
successful event.to_xml
|
150
|
+
else
|
151
|
+
failed("recurrence = "+event.recurrence.to_s)
|
152
|
+
end
|
153
|
+
|
154
|
+
puts "2. Edit Recurrence"
|
155
|
+
event.title = "Edited recurring title"
|
156
|
+
event.recurrence = Recurrence.new({:start_time => @second_start, :end_time => @second_end, :frequency => @second_freq})
|
157
|
+
if event.save
|
158
|
+
successful event.to_xml
|
159
|
+
else
|
160
|
+
failed
|
161
|
+
end
|
162
|
+
|
163
|
+
puts "3. Delete Event"
|
164
|
+
if event.delete
|
165
|
+
successful
|
166
|
+
else
|
167
|
+
failed
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def failed(m = nil)
|
172
|
+
puts "Test Failed"
|
173
|
+
puts m if m
|
174
|
+
exit()
|
175
|
+
end
|
176
|
+
|
177
|
+
def successful(m = nil)
|
178
|
+
puts "Test Successful"
|
179
|
+
puts m if m
|
180
|
+
end
|
181
|
+
|
182
|
+
tester
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: groesser3-gcal4ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.51
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mike Reich
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-06-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: gdata4ruby
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.1.5
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.1.5
|
30
|
+
description: ! 'GCal4Ruby is a Ruby Gem that can be used to interact with the current
|
31
|
+
version of the Google Calendar API. GCal4Ruby provides the following features: Create
|
32
|
+
and edit calendar events, Add and invite users to events, Set reminders, Make recurring
|
33
|
+
events.'
|
34
|
+
email: mike@seabourneconsulting.com
|
35
|
+
executables: []
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- README.md
|
40
|
+
- CHANGELOG
|
41
|
+
- lib/gcal4ruby.rb
|
42
|
+
- lib/gcal4ruby/service.rb
|
43
|
+
- lib/gcal4ruby/calendar.rb
|
44
|
+
- lib/gcal4ruby/event.rb
|
45
|
+
- lib/gcal4ruby/recurrence.rb
|
46
|
+
- test/unit.rb
|
47
|
+
homepage: http://cookingandcoding.com/gcal4ruby/
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project: gcal4ruby
|
67
|
+
rubygems_version: 1.8.24
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: A full featured wrapper for interacting with the Google Calendar API
|
71
|
+
test_files:
|
72
|
+
- test/unit.rb
|