google_oauth 0.0.1 → 0.0.2
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.
- data/README.md +45 -0
- data/lib/google_oauth/array_response.rb +37 -0
- data/lib/google_oauth/calendar.rb +10 -2
- data/lib/google_oauth/client.rb +17 -5
- data/lib/google_oauth/contacts.rb +2 -1
- data/lib/google_oauth/hash_response.rb +34 -0
- metadata +5 -3
- data/README.textile +0 -5
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# OAuth2 Google Data client library for Ruby
|
2
|
+
|
3
|
+
## Install the gem
|
4
|
+
|
5
|
+
gem install google_oauth
|
6
|
+
|
7
|
+
## Authorization
|
8
|
+
|
9
|
+
Before you begin you will need a <code>client_id</code> and <code>client_secret</code> for your application from Google. You can create one
|
10
|
+
from the [Google API Console](https://code.google.com/apis/console/) - ensure you click on API Access and create an OAuth2 client ID.
|
11
|
+
|
12
|
+
With your Client ID credentials you can now initialize the client:
|
13
|
+
|
14
|
+
client = GoogleOAuth::Client.new(
|
15
|
+
:client_id => 'YOUR_CLIENT_ID',
|
16
|
+
:client_secret => 'YOUR_CLIENT_SECRET',
|
17
|
+
:redirect => 'YOUR_REDIRECT_URI',
|
18
|
+
)
|
19
|
+
|
20
|
+
redirect_to client.authorization_url
|
21
|
+
|
22
|
+
At this point the client is redirected to the Google login page. If they authorize your application then Google will return to your
|
23
|
+
redirect URL with a <code>code</code> parameter.
|
24
|
+
|
25
|
+
token = client.authorize(:code => params[:code])
|
26
|
+
|
27
|
+
You now have a valid token that you can store for future requests. You can initialize the client with this token if you already have it:
|
28
|
+
|
29
|
+
client = GoogleOAuth::Client.new(
|
30
|
+
:client_id => 'YOUR_CLIENT_ID',
|
31
|
+
:client_secret => 'YOUR_CLIENT_SECRET',
|
32
|
+
:token => 'USER_TOKEN'
|
33
|
+
)
|
34
|
+
|
35
|
+
## Working with Calendars
|
36
|
+
|
37
|
+
The Google Calendar API supports the jsonc format. Check out the [Google Documentation](http://code.google.com/apis/calendar/data/2.0/developers_guide_protocol.html) to see what properties are available.
|
38
|
+
|
39
|
+
Here is an example that gets the users first calendar and loops over all the events:
|
40
|
+
|
41
|
+
calendar = client.own_calendars.items.first
|
42
|
+
|
43
|
+
client.events(calendar.eventFeedLink).items.each do |event|
|
44
|
+
puts event.title
|
45
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module GoogleOAuth
|
2
|
+
class ArrayResponse
|
3
|
+
|
4
|
+
def initialize(response_array)
|
5
|
+
@array = response_array
|
6
|
+
end
|
7
|
+
|
8
|
+
def each
|
9
|
+
@array.each do |val|
|
10
|
+
yield(wrap(val))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def first
|
15
|
+
wrap(@array.first)
|
16
|
+
end
|
17
|
+
|
18
|
+
def size
|
19
|
+
@array.size
|
20
|
+
end
|
21
|
+
|
22
|
+
def length
|
23
|
+
@array.length
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def wrap(val)
|
28
|
+
if val.is_a? Hash
|
29
|
+
GoogleOAuth::HashResponse.new(val)
|
30
|
+
elsif val.is_a? Array
|
31
|
+
GoogleOAuth::ArrayResponse.new(val)
|
32
|
+
else
|
33
|
+
val
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -1,7 +1,15 @@
|
|
1
1
|
module GoogleOAuth
|
2
2
|
class Client
|
3
|
-
def
|
4
|
-
|
3
|
+
def all_calendars
|
4
|
+
_get_jsonc 'https://www.google.com/calendar/feeds/default/allcalendars/full'
|
5
|
+
end
|
6
|
+
|
7
|
+
def own_calendars
|
8
|
+
_get_jsonc 'https://www.google.com/calendar/feeds/default/owncalendars/full'
|
9
|
+
end
|
10
|
+
|
11
|
+
def events(event_feed)
|
12
|
+
_get_jsonc event_feed
|
5
13
|
end
|
6
14
|
end
|
7
15
|
end
|
data/lib/google_oauth/client.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'google_oauth/hash_response'
|
2
|
+
require 'google_oauth/array_response'
|
1
3
|
require 'google_oauth/contacts'
|
2
4
|
require 'google_oauth/calendar'
|
3
5
|
|
@@ -5,9 +7,9 @@ module GoogleOAuth
|
|
5
7
|
class Client
|
6
8
|
|
7
9
|
def initialize(options = {})
|
8
|
-
@application_id = options[:
|
9
|
-
@application_secret = options[:
|
10
|
-
@callback = options[:
|
10
|
+
@application_id = options[:client_id]
|
11
|
+
@application_secret = options[:client_secret]
|
12
|
+
@callback = options[:redirect]
|
11
13
|
@token = options[:token]
|
12
14
|
end
|
13
15
|
|
@@ -45,14 +47,24 @@ module GoogleOAuth
|
|
45
47
|
OAuth2::AccessToken.new(consumer, @token)
|
46
48
|
end
|
47
49
|
|
50
|
+
def _get_jsonc(url, params={})
|
51
|
+
params.merge! 'alt' => 'jsonc'
|
52
|
+
res = _get(url, params)
|
53
|
+
GoogleOAuth::HashResponse.new(JSON.parse(res)) rescue res
|
54
|
+
end
|
55
|
+
|
56
|
+
def _get_json(url, params={})
|
57
|
+
params.merge! 'alt' => 'json'
|
58
|
+
res = _get(url, params)
|
59
|
+
JSON.parse(res) rescue res
|
60
|
+
end
|
61
|
+
|
48
62
|
def _get(url, params={})
|
49
63
|
oauth_response = access_token.get(url, params)
|
50
|
-
JSON.parse(oauth_response) rescue oauth_response
|
51
64
|
end
|
52
65
|
|
53
66
|
def _post(url, params={}, headers={})
|
54
67
|
oauth_response = access_token.post(url, params, headers)
|
55
|
-
puts oauth_response.inspect
|
56
68
|
JSON.parse(oauth_response) rescue oauth_response
|
57
69
|
end
|
58
70
|
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module GoogleOAuth
|
2
|
+
class HashResponse
|
3
|
+
|
4
|
+
def initialize(response_hash)
|
5
|
+
@hash = response_hash
|
6
|
+
@hash = @hash['data'] if @hash['data']
|
7
|
+
end
|
8
|
+
|
9
|
+
def first
|
10
|
+
wrap(@hash.first)
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing(name)
|
14
|
+
val = @hash[name] if @hash.key? name
|
15
|
+
@hash.each { |k,v| val = v if k.to_s.to_sym == name } unless val
|
16
|
+
if val
|
17
|
+
return wrap(val)
|
18
|
+
end
|
19
|
+
super.method_missing name
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def wrap(val)
|
24
|
+
if val.is_a? Hash
|
25
|
+
GoogleOAuth::HashResponse.new(val)
|
26
|
+
elsif val.is_a? Array
|
27
|
+
GoogleOAuth::ArrayResponse.new(val)
|
28
|
+
else
|
29
|
+
val
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: google_oauth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Richard Taylor
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-04 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -67,11 +67,13 @@ extra_rdoc_files: []
|
|
67
67
|
|
68
68
|
files:
|
69
69
|
- LICENSE
|
70
|
-
- README.
|
70
|
+
- README.md
|
71
71
|
- lib/google_oauth.rb
|
72
|
+
- lib/google_oauth/array_response.rb
|
72
73
|
- lib/google_oauth/calendar.rb
|
73
74
|
- lib/google_oauth/client.rb
|
74
75
|
- lib/google_oauth/contacts.rb
|
76
|
+
- lib/google_oauth/hash_response.rb
|
75
77
|
has_rdoc: true
|
76
78
|
homepage: http://github.com/moomerman/google_oauth
|
77
79
|
licenses: []
|