Skip to content

Commit a866a00

Browse files
committed
Initial commit
0 parents  commit a866a00

11 files changed

+923
-0
lines changed

LICENSE

+661
Large diffs are not rendered by default.

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Find & Create + Update & Delete
2+
3+
All you need to survive a syncronization project with Google Calendar using GData 2.0 + OAuth 1.0
4+
5+
# Synopsis
6+
7+
require 'google_calendar'
8+
client = GoogleCalendarApiV2::Client.new "teambox.com", "secret", "oauth_token", "oauth_secret"
9+
10+
calendar = client.calendars.find 'calendar_token' || client.calendars.create { :title => "Teambox" }
11+
12+
calendar[:title] = 'Teambox calendar'
13+
14+
calendar.save
15+
16+
event = calendar.events.find 'calendar_token'
17+
18+
event[:title] = 'Updated event name'
19+
event.save
20+
21+
event.destroy
22+
calendar.destroy

google_calendar_api_v2.gemspec

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Gem::Specification.new do |s|
2+
s.name = 'google_calendar_api_v2'
3+
s.version = '0.1.0'
4+
s.summary = "Work with Google Calendar using GData 2.0 + OAuth 1.0"
5+
s.author = ['Charles Barbier']
6+
s.email = '[email protected]'
7+
s.homepage = 'http://www.github.com/unixcharles/google_calendar_api_v2'
8+
9+
s.files = Dir['README.md', 'LICENSE',
10+
'lib/google_calendar_api_v2.rb',
11+
'lib/google_calendar_api_v2/base.rb',
12+
'lib/google_calendar_api_v2/client.rb',
13+
'lib/google_calendar_api_v2/calendar.rb',
14+
'lib/google_calendar_api_v2/event.rb',
15+
'lib/google_calendar_api_v2/response/base.rb',
16+
'lib/google_calendar_api_v2/response/calendar.rb',
17+
'lib/google_calendar_api_v2/response/event.rb']
18+
19+
s.require_path = 'lib'
20+
21+
s.add_runtime_dependency 'oauth'
22+
end

lib/google_calendar_api_v2.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module GoogleCalendarApiV2
2+
autoload :Client, 'google_calendar_api_v2/client'
3+
autoload :Base, 'google_calendar_api_v2/base'
4+
autoload :Calendar, 'google_calendar_api_v2/calendar'
5+
autoload :Event, 'google_calendar_api_v2/event'
6+
7+
module Response
8+
autoload :Base, 'google_calendar_api_v2/response/base'
9+
autoload :Calendar, 'google_calendar_api_v2/response/calendar'
10+
autoload :Event, 'google_calendar_api_v2/response/event'
11+
end
12+
end

lib/google_calendar_api_v2/base.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module GoogleCalendarApiV2
2+
module Base
3+
attr_reader :connection
4+
5+
def success?(response)
6+
case response.code.to_i
7+
when 200, 201
8+
true
9+
else
10+
false
11+
end
12+
end
13+
end
14+
end
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module GoogleCalendarApiV2
2+
class Calendar
3+
include Base
4+
5+
attr_reader :events
6+
7+
def initialize(connection)
8+
@connection = connection
9+
end
10+
11+
def find(calendar_token)
12+
url = "/calendar/feeds/default/allcalendars/full/#{calendar_token}?alt=jsonc"
13+
response = @connection.get url, Client::HEADERS
14+
if success? response
15+
Response::Calendar.new(response, @connection)
16+
end
17+
end
18+
19+
def create(params = {})
20+
response = @connection.post '/calendar/feeds/default/owncalendars/full?alt=jsonc',
21+
{
22+
:data => {
23+
:title => "Unnamed calendar",
24+
:hidden => false
25+
}.merge(params)
26+
}.to_json, Client::HEADERS
27+
28+
if success? response
29+
Response::Calendar.new(response, @connection)
30+
end
31+
end
32+
33+
34+
end
35+
end

lib/google_calendar_api_v2/client.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'oauth'
2+
3+
module GoogleCalendarApiV2
4+
class Client
5+
attr_reader :connection, :headers, :calendars, :events
6+
7+
HEADERS = {'Content-Type' => 'application/json', 'GData-Version' => '2.6'}
8+
9+
# Initialize this class with :consumer_key, :consumer_secret, :token, :token_secret
10+
def initialize(consumer_key, consumer_secret, token, token_secret)
11+
consumer = OAuth::Consumer.new(consumer_key, consumer_secret, {
12+
:site => "https://www.google.com",
13+
:scheme => :header
14+
})
15+
16+
@connection = OAuth::AccessToken.new(consumer,token, token_secret)
17+
18+
@calendars = Calendar.new(@connection)
19+
end
20+
end
21+
end

lib/google_calendar_api_v2/event.rb

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module GoogleCalendarApiV2
2+
class Event
3+
include Base
4+
5+
attr_reader :calendar
6+
7+
def initialize(connection, calendar)
8+
@connection = connection
9+
@calendar = calendar
10+
end
11+
12+
def find(event_token)
13+
url = "https://www.google.com/calendar/feeds/#{@calendar.token}/private/full/#{event_token}?alt=jsonc"
14+
response = @connection.get url, Client::HEADERS
15+
16+
if success? response
17+
Response::Calendar.new(response, @connection)
18+
end
19+
end
20+
21+
def create(params = {})
22+
response = @connection.post "/calendar/feeds/#{@calendar.token}/private/full?alt=jsonc",
23+
{
24+
:data => {
25+
:title => "Undefined event",
26+
:hidden => false
27+
}.merge(params)
28+
}.to_json, Client::HEADERS
29+
30+
if success? response
31+
Response::Calendar.new(response, @connection)
32+
else
33+
response
34+
end
35+
end
36+
37+
end
38+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
require 'json'
2+
3+
module GoogleCalendarApiV2
4+
module Response
5+
module Base
6+
attr_reader :response, :connection, :attributes
7+
8+
def initialize(response, connection, parent = nil)
9+
@response, @connection = response, connection
10+
@attributes = JSON.parse(response.body)['data'] rescue {}
11+
end
12+
13+
def update_attributes(attributes)
14+
@attributes.merge!(attributes)
15+
end
16+
17+
def []=(key, value)
18+
@attributes[key.to_s]=value
19+
end
20+
21+
def [](key)
22+
@attributes[key.to_s]
23+
end
24+
25+
def to_json
26+
{
27+
"apiVersion" => "2.3",
28+
:data => @attributes
29+
}.to_json
30+
end
31+
32+
def save
33+
res = @connection.put @attributes['selfLink'], self.to_json, GoogleCalendarApiV2::Client::HEADERS
34+
35+
@attributes = JSON.parse(res.body)['data'] rescue @attributes
36+
if success? res
37+
true
38+
else
39+
false
40+
end
41+
end
42+
43+
def destroy
44+
res = @connection.delete @attributes['selfLink'], GoogleCalendarApiV2::Client::HEADERS.merge({ 'If-Match' => '*' })
45+
if success? res
46+
true
47+
else
48+
false
49+
end
50+
end
51+
52+
def token
53+
@attributes['id'].split('/').last
54+
end
55+
56+
private
57+
58+
def success?(response)
59+
case response.code.to_i
60+
when 200, 201
61+
true
62+
else
63+
false
64+
end
65+
end
66+
67+
end
68+
end
69+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module GoogleCalendarApiV2
2+
module Response
3+
class Calendar
4+
include Base
5+
6+
attr_reader :events
7+
8+
def initialize(response, connection)
9+
super
10+
@events = GoogleCalendarApiV2::Event.new(@connection, self)
11+
end
12+
end
13+
end
14+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module GoogleCalendarApiV2
2+
module Response
3+
class Event
4+
include Base
5+
6+
attr_reader :calendar
7+
8+
def initialize(response, connection, calendar)
9+
super
10+
@calendar = calendar
11+
end
12+
13+
end
14+
end
15+
end

0 commit comments

Comments
 (0)