aunderwo-gcal4ruby 0.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,166 @@
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 = 'http://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 "http://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
+ # query_params is a hash of parameters which can be found here
82
+ # http://code.google.com/apis/gdata/docs/2.0/reference.html#Queries
83
+ # e.g {"max-results" => "50"} The default here is 10000
84
+ def calendars(query_params = {})
85
+ if not @auth_token
86
+ raise NotAuthenticated
87
+ end
88
+ ret = send_request(GData4Ruby::Request.new(:get, CALENDAR_LIST_FEED, nil, nil, {"max-results" => "10000"}.merge(query_params)))
89
+ cals = []
90
+ REXML::Document.new(ret.body).root.elements.each("entry"){}.map do |entry|
91
+ entry = GData4Ruby::Utils.add_namespaces(entry)
92
+ cal = Calendar.new(self)
93
+ cal.load(entry.to_s)
94
+ cals << cal
95
+ end
96
+ return cals
97
+ end
98
+
99
+ #Returns an array of Event objects for each event in this account
100
+ # query_params is a hash of parameters which can be found here
101
+ # http://code.google.com/apis/gdata/docs/2.0/reference.html#Queries
102
+ # e.g {"max-results" => "50"} The default here is 10000
103
+ def events(query_params = {})
104
+ if not @auth_token
105
+ raise NotAuthenticated
106
+ end
107
+ ret = send_request(GData4Ruby::Request.new(:get, default_event_feed, nil, nil, {"max-results" => "10000"}.merge(query_params)))
108
+ events = []
109
+ REXML::Document.new(ret.body).root.elements.each("entry"){}.map do |entry|
110
+ entry = GData4Ruby::Utils.add_namespaces(entry)
111
+ event = Event.new(self)
112
+ event.load(entry.to_s)
113
+ events << event
114
+ end
115
+ return events
116
+ end
117
+
118
+ #Helper function to return a formatted iframe embedded google calendar. Parameters are:
119
+ #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
120
+ #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:
121
+ # height:: the height of the embedded calendar in pixels
122
+ # width:: the width of the embedded calendar in pixels
123
+ # title:: the title to display
124
+ # bgcolor:: the background color. Limited choices, see google docs for allowable values.
125
+ # showTitle:: set to '0' to hide the title
126
+ # showDate:: set to '0' to hide the current date
127
+ # showNav:: set to '0 to hide the navigation tools
128
+ # showPrint:: set to '0' to hide the print icon
129
+ # showTabs:: set to '0' to hide the tabs
130
+ # showCalendars:: set to '0' to hide the calendars selection drop down
131
+ # showTz:: set to '0' to hide the timezone selection
132
+ # border:: the border width in pixels
133
+ # dates:: a range of dates to display in the format of 'yyyymmdd/yyyymmdd'. Example: 20090820/20091001
134
+ # privateKey:: use to display a private calendar. You can find this key under the calendar settings pane of the Google Calendar website.
135
+ # ctz:: The timezone to convert event times to
136
+ #3. *colors*: a hash of calendar ids as key and color values as associated hash values. Example: {'test@gmail.com' => '#7A367A'}
137
+ def to_iframe(cals, params = {}, colors = {})
138
+ params[:height] ||= "600"
139
+ params[:width] ||= "600"
140
+ params[:title] ||= (self.account ? self.account : '')
141
+ params[:bgcolor] ||= "#FFFFFF"
142
+ params[:border] ||= "0"
143
+ params.each{|key, value| params[key] = CGI::escape(value)}
144
+ output = "#{params.to_a.collect{|a| a.join("=")}.join("&")}&"
145
+
146
+ if cals.is_a?(Array)
147
+ for c in cals
148
+ output += "src=#{c}&"
149
+ if colors and colors[c]
150
+ output += "color=%23#{colors[c].gsub("#", "")}&"
151
+ end
152
+ end
153
+ elsif cals == :all
154
+ cal_list = calendars()
155
+ for c in cal_list
156
+ output += "src=#{c.id}&"
157
+ end
158
+ elsif cals == :first
159
+ cal_list = calendars()
160
+ output += "src=#{cal_list[0].id}&"
161
+ end
162
+
163
+ "<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>"
164
+ end
165
+ end
166
+ end
@@ -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,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aunderwo-gcal4ruby
3
+ version: !ruby/object:Gem::Version
4
+ hash: 7
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 5
9
+ - 6
10
+ version: 0.5.6
11
+ platform: ruby
12
+ authors:
13
+ - Mike Reich
14
+ - Anthony Underwood
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-10-01 00:00:00 +01:00
20
+ default_executable:
21
+ dependencies: []
22
+
23
+ description:
24
+ email:
25
+ - mike@seabourneconsulting.com
26
+ - email2ants@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ files:
34
+ - CHANGELOG
35
+ - README
36
+ - lib/gcal4ruby.rb
37
+ - lib/gcal4ruby/calendar.rb
38
+ - lib/gcal4ruby/event.rb
39
+ - lib/gcal4ruby/recurrence.rb
40
+ - lib/gcal4ruby/service.rb
41
+ - test/unit.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/aunderwo/GCal4Ruby
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.7
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: A full featured wrapper for interacting with the Google Calendar API
76
+ test_files:
77
+ - test/unit.rb