goauth2 0.0.1
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/.gitignore +4 -0
- data/.rvmrc +55 -0
- data/Gemfile +4 -0
- data/README.md +5 -0
- data/Rakefile +1 -0
- data/goauth2.gemspec +26 -0
- data/lib/goauth2/array_response.rb +37 -0
- data/lib/goauth2/calendar.rb +15 -0
- data/lib/goauth2/client.rb +101 -0
- data/lib/goauth2/contacts.rb +8 -0
- data/lib/goauth2/hash_response.rb +34 -0
- data/lib/goauth2/version.rb +3 -0
- data/lib/goauth2.rb +10 -0
- metadata +102 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
|
7
|
+
environment_id="ruby-1.9.2-p290@goauth2"
|
8
|
+
|
9
|
+
#
|
10
|
+
# Uncomment following line if you want options to be set only for given project.
|
11
|
+
#
|
12
|
+
# PROJECT_JRUBY_OPTS=( --1.9 )
|
13
|
+
|
14
|
+
#
|
15
|
+
# First we attempt to load the desired environment directly from the environment
|
16
|
+
# file. This is very fast and efficient compared to running through the entire
|
17
|
+
# CLI and selector. If you want feedback on which environment was used then
|
18
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
19
|
+
#
|
20
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
|
21
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
22
|
+
then
|
23
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
24
|
+
|
25
|
+
if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
|
26
|
+
then
|
27
|
+
. "${rvm_path:-$HOME/.rvm}/hooks/after_use"
|
28
|
+
fi
|
29
|
+
else
|
30
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
31
|
+
if ! rvm --create use "$environment_id"
|
32
|
+
then
|
33
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
34
|
+
exit 1
|
35
|
+
fi
|
36
|
+
fi
|
37
|
+
|
38
|
+
#
|
39
|
+
# If you use an RVM gemset file to install a list of gems (*.gems), you can have
|
40
|
+
# it be automatically loaded. Uncomment the following and adjust the filename if
|
41
|
+
# necessary.
|
42
|
+
#
|
43
|
+
# filename=".gems"
|
44
|
+
# if [[ -s "$filename" ]]
|
45
|
+
# then
|
46
|
+
# rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
|
47
|
+
# fi
|
48
|
+
|
49
|
+
# If you use bundler, this might be useful to you:
|
50
|
+
# if command -v bundle && [[ -s Gemfile ]]
|
51
|
+
# then
|
52
|
+
# bundle
|
53
|
+
# fi
|
54
|
+
|
55
|
+
|
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/goauth2.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "goauth2/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "goauth2"
|
7
|
+
s.version = Goauth2::VERSION
|
8
|
+
s.authors = ["Mark Dillon"]
|
9
|
+
s.email = ["mdillon@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/markdillon/goauth2"
|
11
|
+
s.summary = %q{Oauth2 for Google Data APIs}
|
12
|
+
s.description = %q{Oauth2 for Google Data APIs}
|
13
|
+
|
14
|
+
s.rubyforge_project = "goauth2"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
s.add_dependency 'json'
|
24
|
+
s.add_dependency 'oauth2'
|
25
|
+
s.add_development_dependency 'bundler'
|
26
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Goauth2
|
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
|
+
Goauth2::HashResponse.new(val)
|
30
|
+
elsif val.is_a? Array
|
31
|
+
Goauth2::ArrayResponse.new(val)
|
32
|
+
else
|
33
|
+
val
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Goauth2
|
2
|
+
class Client
|
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
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'goauth2/hash_response'
|
2
|
+
require 'goauth2/array_response'
|
3
|
+
require 'goauth2/contacts'
|
4
|
+
require 'goauth2/calendar'
|
5
|
+
|
6
|
+
module Goauth2
|
7
|
+
class Client
|
8
|
+
SCOPES = {
|
9
|
+
:calendar => 'https://www.google.com/calendar/feeds/',
|
10
|
+
:contacts => 'https://www.google.com/m8/feeds/',
|
11
|
+
:gmail => 'https://mail.google.com/mail/feed/atom/'
|
12
|
+
}
|
13
|
+
|
14
|
+
def initialize(options = {})
|
15
|
+
@client_id = options[:client_id]
|
16
|
+
@client_secret = options[:client_secret]
|
17
|
+
@redirect_uri = options[:redirect_uri]
|
18
|
+
@scope = options[:scope] || SCOPES.values.join(' ')
|
19
|
+
|
20
|
+
@token = options[:token]
|
21
|
+
@refresh_token = options[:refresh_token]
|
22
|
+
@expires_at = options[:expires_at]
|
23
|
+
@expires_in = options[:expires_in]
|
24
|
+
end
|
25
|
+
|
26
|
+
def authorize_url(options = {})
|
27
|
+
client.auth_code.authorize_url(
|
28
|
+
:redirect_uri => options[:redirect_uri] || @redirect_uri,
|
29
|
+
:scope => options[:scope] || @scope
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
def authorize(options = {})
|
34
|
+
@access_token ||= client.auth_code.get_token(
|
35
|
+
options[:code],
|
36
|
+
:redirect_uri => options[:redirect_uri] || @redirect_uri
|
37
|
+
)
|
38
|
+
@token = @access_token.token
|
39
|
+
@refresh_token = @access_token.refresh_token
|
40
|
+
@expires_at = @access_token.expires_at
|
41
|
+
@expires_in = @access_token.expires_in
|
42
|
+
@access_token
|
43
|
+
end
|
44
|
+
|
45
|
+
def client
|
46
|
+
@client ||= OAuth2::Client.new(
|
47
|
+
@client_id,
|
48
|
+
@client_secret,
|
49
|
+
{
|
50
|
+
:site => "https://accounts.google.com",
|
51
|
+
:authorize_url => '/o/oauth2/auth',
|
52
|
+
:token_url => '/o/oauth2/token'
|
53
|
+
}
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
def access_token
|
58
|
+
refresh!
|
59
|
+
@access_token ||= OAuth2::AccessToken.new(client, @token, :refresh_token => @refresh_token, :expires_at => @expires_at, :expires_in => @expires_in)
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def refresh!
|
65
|
+
if @access_token && @access_token.expired?
|
66
|
+
@refresh_token ||= @access_token.refresh_token
|
67
|
+
access_token = @access_token.refresh!
|
68
|
+
@token = access_token.token
|
69
|
+
@expires_at = access_token.expires_at
|
70
|
+
@expires_in = access_token.expires_in
|
71
|
+
@access_token = OAuth2::AccessToken.new(client, @token, :refresh_token => @refresh_token, :expires_at => @expires_at, :expires_in => @expires_in)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def _get_jsonc(url, params={})
|
76
|
+
params.merge! :params => {'alt' => 'jsonc'}
|
77
|
+
res = _get(url, params)
|
78
|
+
end
|
79
|
+
|
80
|
+
def _get_json(url, params={})
|
81
|
+
params.merge! :params => {'alt' => 'json'}
|
82
|
+
res = _get(url, params)
|
83
|
+
end
|
84
|
+
|
85
|
+
def _get(url, params={})
|
86
|
+
res = access_token.get(url, params)
|
87
|
+
Goauth2::HashResponse.new(JSON.parse(res.body)) rescue res
|
88
|
+
end
|
89
|
+
|
90
|
+
def _post(url, params={}, headers={})
|
91
|
+
oauth_response = access_token.post(url, params, headers)
|
92
|
+
JSON.parse(oauth_response) rescue oauth_response
|
93
|
+
end
|
94
|
+
|
95
|
+
def _delete(url)
|
96
|
+
oauth_response = access_token.delete(url)
|
97
|
+
JSON.parse(oauth_response) rescue oauth_response
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Goauth2
|
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
|
+
Goauth2::HashResponse.new(val)
|
26
|
+
elsif val.is_a? Array
|
27
|
+
Goauth2::ArrayResponse.new(val)
|
28
|
+
else
|
29
|
+
val
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
data/lib/goauth2.rb
ADDED
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: goauth2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mark Dillon
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-26 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70296606918260 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70296606918260
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: json
|
27
|
+
requirement: &70296606911180 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70296606911180
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: oauth2
|
38
|
+
requirement: &70296606910300 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70296606910300
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: &70296606909400 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70296606909400
|
58
|
+
description: Oauth2 for Google Data APIs
|
59
|
+
email:
|
60
|
+
- mdillon@gmail.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- .rvmrc
|
67
|
+
- Gemfile
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- goauth2.gemspec
|
71
|
+
- lib/goauth2.rb
|
72
|
+
- lib/goauth2/array_response.rb
|
73
|
+
- lib/goauth2/calendar.rb
|
74
|
+
- lib/goauth2/client.rb
|
75
|
+
- lib/goauth2/contacts.rb
|
76
|
+
- lib/goauth2/hash_response.rb
|
77
|
+
- lib/goauth2/version.rb
|
78
|
+
homepage: https://github.com/markdillon/goauth2
|
79
|
+
licenses: []
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project: goauth2
|
98
|
+
rubygems_version: 1.8.10
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: Oauth2 for Google Data APIs
|
102
|
+
test_files: []
|