h13ronim-gcal4ruby 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +32 -0
- data/README +85 -0
- data/lib/gcal4ruby/base.rb +260 -0
- data/lib/gcal4ruby/calendar.rb +446 -0
- data/lib/gcal4ruby/event.rb +484 -0
- data/lib/gcal4ruby/recurrence.rb +164 -0
- data/lib/gcal4ruby/service.rb +151 -0
- data/lib/gcal4ruby.rb +1 -0
- data/test/unit.rb +141 -0
- metadata +62 -0
@@ -0,0 +1,151 @@
|
|
1
|
+
require 'gcal4ruby/base'
|
2
|
+
require 'gcal4ruby/calendar'
|
3
|
+
|
4
|
+
module GCal4Ruby
|
5
|
+
|
6
|
+
#The service class is the main handler for all direct interactions with the
|
7
|
+
#Google Calendar API. A service represents a single user account. Each user
|
8
|
+
#account can have multiple calendars, so you'll need to find the calendar you
|
9
|
+
#want from the service, using the Calendar#find class method.
|
10
|
+
#=Usage
|
11
|
+
#
|
12
|
+
#1. Authenticate
|
13
|
+
# service = Service.new
|
14
|
+
# service.authenticate("user@gmail.com", "password")
|
15
|
+
#
|
16
|
+
#2. Get Calendar List
|
17
|
+
# calendars = service.calendars
|
18
|
+
#
|
19
|
+
|
20
|
+
class Service < Base
|
21
|
+
#Convenience attribute contains the currently authenticated account name
|
22
|
+
attr_reader :account
|
23
|
+
|
24
|
+
# The token returned by the Google servers, used to authorize all subsequent messages
|
25
|
+
attr_reader :auth_token
|
26
|
+
|
27
|
+
# Determines whether GCal4Ruby ensures a calendar is public. Setting this to false can increase speeds by
|
28
|
+
# 50% but can cause errors if you try to do something to a calendar that is not public and you don't have
|
29
|
+
# adequate permissions
|
30
|
+
attr_accessor :check_public
|
31
|
+
|
32
|
+
def initialize
|
33
|
+
super
|
34
|
+
@check_public = true
|
35
|
+
end
|
36
|
+
|
37
|
+
# The authenticate method passes the username and password to google servers.
|
38
|
+
# If authentication succeeds, returns true, otherwise raises the AuthenticationFailed error.
|
39
|
+
def authenticate(username, password)
|
40
|
+
ret = nil
|
41
|
+
ret = send_post(AUTH_URL, "Email=#{username}&Passwd=#{password}&source=GCal4Ruby&service=cl&accountType=HOSTED_OR_GOOGLE")
|
42
|
+
if ret.class == Net::HTTPOK
|
43
|
+
@auth_token = ret.read_body.to_a[2].gsub("Auth=", "").strip
|
44
|
+
@account = username
|
45
|
+
return true
|
46
|
+
else
|
47
|
+
raise AuthenticationFailed
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
#Returns an array of Calendar objects for each calendar associated with
|
52
|
+
#the authenticated account.
|
53
|
+
def calendars
|
54
|
+
if not @auth_token
|
55
|
+
raise NotAuthenticated
|
56
|
+
end
|
57
|
+
ret = send_get(CALENDAR_LIST_FEED)
|
58
|
+
cals = []
|
59
|
+
REXML::Document.new(ret.body).root.elements.each("entry"){}.map do |entry|
|
60
|
+
entry.attributes["xmlns:gCal"] = "http://schemas.google.com/gCal/2005"
|
61
|
+
entry.attributes["xmlns:gd"] = "http://schemas.google.com/g/2005"
|
62
|
+
entry.attributes["xmlns:app"] = "http://www.w3.org/2007/app"
|
63
|
+
entry.attributes["xmlns"] = "http://www.w3.org/2005/Atom"
|
64
|
+
cal = Calendar.new(self)
|
65
|
+
cal.load("<?xml version='1.0' encoding='UTF-8'?>#{entry.to_s}")
|
66
|
+
cals << cal
|
67
|
+
end
|
68
|
+
return cals
|
69
|
+
end
|
70
|
+
|
71
|
+
#Helper function to return a formatted iframe embedded google calendar. Parameters are:
|
72
|
+
#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
|
73
|
+
#2. *params*: a hash of parameters that affect the display of the embedded calendar:
|
74
|
+
# height:: the height of the embedded calendar in pixels
|
75
|
+
# width:: the width of the embedded calendar in pixels
|
76
|
+
# title:: the title to display
|
77
|
+
# bgcolor:: the background color. Limited choices, see google docs for allowable values.
|
78
|
+
# color:: the color of the calendar elements. Limited choices, see google docs for allowable values.
|
79
|
+
# showTitle:: set to 'false' to hide the title
|
80
|
+
# showDate:: set to 'false' to hide the current date
|
81
|
+
# showNav:: set to 'false to hide the navigation tools
|
82
|
+
# showPrint:: set to 'false' to hide the print icon
|
83
|
+
# showTabs:: set to 'false' to hide the tabs
|
84
|
+
# showCalendars:: set to 'false' to hide the calendars selection drop down
|
85
|
+
# showTimezone:: set to 'false' to hide the timezone selection
|
86
|
+
# border:: the border width in pixels
|
87
|
+
# dates:: a range of dates to display in the format of 'yyyymmdd/yyyymmdd'. Example: 20090820/20091001
|
88
|
+
# privateKey:: use to display a private calendar. You can find this key under the calendar settings pane of the Google Calendar website.
|
89
|
+
# colors:: a hash of calendar ids as key and color values as associated hash values. Example: {'test@gmail.com' => '#7A367A'}
|
90
|
+
def to_iframe(cals, params = {})
|
91
|
+
params[:height] ||= "600"
|
92
|
+
params[:width] ||= "600"
|
93
|
+
params[:title] ||= (self.account ? self.account : '')
|
94
|
+
params[:bgcolor] ||= "#FFFFFF"
|
95
|
+
params[:color] ||= "#2952A3"
|
96
|
+
params[:showTitle] = params[:showTitle] == false ? "showTitle=0" : ''
|
97
|
+
params[:showNav] = params[:showNav] == false ? "showNav=0" : ''
|
98
|
+
params[:showDate] = params[:showDate] == false ? "showDate=0" : ''
|
99
|
+
params[:showPrint] = params[:showPrint] == false ? "showPrint=0" : ''
|
100
|
+
params[:showTabs] = params[:showTabs] == false ? "showTabs=0" : ''
|
101
|
+
params[:showCalendars] = params[:showCalendars] == false ? "showCalendars=0" : ''
|
102
|
+
params[:showTimezone] = params[:showTimezone] == false ? 'showTz=0' : ''
|
103
|
+
params[:border] ||= "0"
|
104
|
+
output = ''
|
105
|
+
puts "params = #{params.inspect}" if self.debug
|
106
|
+
params.each do |key, value|
|
107
|
+
puts "key = #{key} and value = #{value}" if self.debug
|
108
|
+
case key
|
109
|
+
when :height then output += "height=#{value}"
|
110
|
+
when :width then output += "width=#{value}"
|
111
|
+
when :title then output += "title=#{CGI.escape(value)}"
|
112
|
+
when :bgcolor then output += "bgcolor=#{CGI.escape(value)}"
|
113
|
+
when :color then output += "color=#{CGI.escape(value)}"
|
114
|
+
when :showTitle then output += value
|
115
|
+
when :showDate then output += value
|
116
|
+
when :showNav then output += value
|
117
|
+
when :showPrint then output += value
|
118
|
+
when :showTabs then output += value
|
119
|
+
when :showCalendars then output += value
|
120
|
+
when :showTimezone then output += value
|
121
|
+
when :viewMode then output += "mode=#{value}"
|
122
|
+
when :dates then output += "dates=#{CGI.escape(value)}"
|
123
|
+
when :privateKey then output += "pvttk=#{value}"
|
124
|
+
end
|
125
|
+
output += "&"
|
126
|
+
end
|
127
|
+
|
128
|
+
puts "output = #{output}" if self.debug
|
129
|
+
|
130
|
+
if cals.is_a?(Array)
|
131
|
+
for c in cals
|
132
|
+
output += "src=#{c}&"
|
133
|
+
if params[:colors] and params[:colors][c]
|
134
|
+
output += "color=#{CGI.escape(params[:colors][c])}&"
|
135
|
+
end
|
136
|
+
end
|
137
|
+
elsif cals == :all
|
138
|
+
cal_list = calendars()
|
139
|
+
for c in cal_list
|
140
|
+
output += "src=#{c.id}&"
|
141
|
+
end
|
142
|
+
elsif cals == :first
|
143
|
+
cal_list = calendars()
|
144
|
+
output += "src=#{cal_list[0].id}&"
|
145
|
+
end
|
146
|
+
|
147
|
+
"<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>"
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
data/lib/gcal4ruby.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "gcal4ruby/service"
|
data/test/unit.rb
ADDED
@@ -0,0 +1,141 @@
|
|
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
|
+
end
|
27
|
+
|
28
|
+
def service_test
|
29
|
+
puts "---Starting Service Test---"
|
30
|
+
puts "1. Authenticate"
|
31
|
+
if @service.authenticate(@username, @password)
|
32
|
+
successful
|
33
|
+
else
|
34
|
+
failed
|
35
|
+
end
|
36
|
+
|
37
|
+
puts "2. Calendar List"
|
38
|
+
cals = @service.calendars
|
39
|
+
if cals
|
40
|
+
successful "Calendars for this Account:"
|
41
|
+
cals.each do |cal|
|
42
|
+
puts cal.title
|
43
|
+
end
|
44
|
+
else
|
45
|
+
failed
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def calendar_test
|
50
|
+
puts "---Starting Calendar Test---"
|
51
|
+
|
52
|
+
puts "1. Create Calendar"
|
53
|
+
cal = Calendar.new(@service)
|
54
|
+
cal.title = "test calendar"+Time.now.to_s
|
55
|
+
puts "Calender exists = "+cal.exists?.to_s
|
56
|
+
if cal.save
|
57
|
+
successful cal.to_xml
|
58
|
+
else
|
59
|
+
failed
|
60
|
+
end
|
61
|
+
|
62
|
+
puts "2. Edit Calendar"
|
63
|
+
cal.title = "renamed title"
|
64
|
+
if cal.save
|
65
|
+
successful cal.to_xml
|
66
|
+
else
|
67
|
+
puts "Test 2 Failed"
|
68
|
+
end
|
69
|
+
|
70
|
+
puts "3. Find Calendar by ID"
|
71
|
+
c = Calendar.find(@service, cal.id)
|
72
|
+
if c.title == cal.title
|
73
|
+
successful
|
74
|
+
else
|
75
|
+
failed "#{c.title} not equal to #{cal.title}"
|
76
|
+
end
|
77
|
+
|
78
|
+
puts "4. Delete Calendar"
|
79
|
+
if cal.delete and not cal.title
|
80
|
+
successful
|
81
|
+
else
|
82
|
+
failed
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def event_test
|
87
|
+
puts "---Starting Event Test---"
|
88
|
+
|
89
|
+
puts "1. Create Event"
|
90
|
+
event = Event.new(@service.calendars[0])
|
91
|
+
event.title = "Test Event"
|
92
|
+
event.content = "Test event content"
|
93
|
+
event.start = Time.now+1800
|
94
|
+
event.end = Time.now+5400
|
95
|
+
if event.save
|
96
|
+
successful event.to_xml
|
97
|
+
else
|
98
|
+
failed
|
99
|
+
end
|
100
|
+
|
101
|
+
puts "2. Edit Event"
|
102
|
+
event.title = "Edited title"
|
103
|
+
if event.save
|
104
|
+
successful event.to_xml
|
105
|
+
else
|
106
|
+
failed
|
107
|
+
end
|
108
|
+
|
109
|
+
puts "3. Reload Event"
|
110
|
+
if event.reload
|
111
|
+
successful
|
112
|
+
end
|
113
|
+
|
114
|
+
puts "4. Find Event by id"
|
115
|
+
e = Event.find(@service.calendars[0], event.id)
|
116
|
+
if e.title == event.title
|
117
|
+
successful
|
118
|
+
else
|
119
|
+
failed "Found event doesn't match existing event"
|
120
|
+
end
|
121
|
+
|
122
|
+
puts "5. Delete Event"
|
123
|
+
if event.delete
|
124
|
+
successful
|
125
|
+
else
|
126
|
+
failed
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def failed(m = nil)
|
131
|
+
puts "Test Failed"
|
132
|
+
puts m if m
|
133
|
+
exit()
|
134
|
+
end
|
135
|
+
|
136
|
+
def successful(m = nil)
|
137
|
+
puts "Test Successful"
|
138
|
+
puts m if m
|
139
|
+
end
|
140
|
+
|
141
|
+
tester
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: h13ronim-gcal4ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Reich
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-01 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A full featured wrapper for interacting with the Google Calendar API
|
17
|
+
email: mike@seabourneconsulting.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README
|
26
|
+
- CHANGELOG
|
27
|
+
- lib/gcal4ruby.rb
|
28
|
+
- lib/gcal4ruby/base.rb
|
29
|
+
- lib/gcal4ruby/service.rb
|
30
|
+
- lib/gcal4ruby/calendar.rb
|
31
|
+
- lib/gcal4ruby/event.rb
|
32
|
+
- lib/gcal4ruby/recurrence.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://gcal4ruby.rubyforge.org/
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project: gcal4ruby
|
57
|
+
rubygems_version: 1.3.5
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: A full featured wrapper for interacting with the Google Calendar API
|
61
|
+
test_files:
|
62
|
+
- test/unit.rb
|