gplus 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,19 +1,19 @@
1
- # Gplus: A Google+ API client library for Ruby
1
+ # gplus: A Google+ API client library for Ruby
2
2
 
3
3
  ## Intro
4
4
 
5
- GPlus is a complete implementation of the Google+ API, with help from OAuth2 and MultiJson.
5
+ gplus is a complete implementation of the Google+ API, with help from OAuth2 and MultiJson.
6
6
 
7
7
  I'm aiming to produce something light-weight, well documented, and thoroughly tested.
8
8
 
9
9
  It currently has full support for the People and Activities APIs, using either OAuth requests for private data or API key requests for public data.
10
10
 
11
- * [Documentation](http://rubydoc.info/gems/gplus/frames)
11
+ * [Documentation](http://rubydoc.info/github/nfm/gplus/master/frames)
12
12
  * [Issues](https://github.com/nfm/gplus/issues)
13
13
 
14
14
  ## Installation
15
15
 
16
- Add GPlus to your Gemfile, then run `bundle install`.
16
+ Add gplus to your Gemfile, then run `bundle install`.
17
17
 
18
18
  gem "gplus", "~> 0.3.1"
19
19
 
@@ -54,24 +54,48 @@ Generate an authorization URL, and use it in a view:
54
54
 
55
55
  = link_to 'Authorize This App', @auth_url
56
56
 
57
- After the user authorizes your app, they will be redirected to your `redirect_uri`. Store `params[:code]`:
57
+ After the user authorizes your app, they will be redirected to your `redirect_uri`. Use `params[:code]` to retrieve an OAuth token for the user, and store the `token`, `refresh_token` and `expires_at` for persistence:
58
58
 
59
59
  class OauthController < ApplicationController
60
60
  def callback
61
- current_user.update_attributes(:oauth_code => params[:code])
61
+ access_token = client.authorize(params[:code])
62
+ current_user.update_attributes(
63
+ :token => access_token.token,
64
+ :refresh_token => access_token.refresh_token,
65
+ :token_expires_at => access_token.expires_at
66
+ )
62
67
  end
63
68
  end
64
69
 
65
- Finally, create an authorized client instance:
66
-
67
- client.authorize(current_user.oauth_code)
68
-
69
- If you have an OAuth code stored, you can use it to initialize your API client:
70
+ Now you can create an authorized client instance using the stored OAuth token:
70
71
 
71
72
  @client = Gplus::Client.new(
72
- :token => current_user.oauth_code,
73
+ :token => current_user.token,
74
+ :refresh_token => current_user.refresh_token,
75
+ :token_expires_at => current_user.token_expires_at,
76
+ :client_id => 'YOUR_CLIENT_ID',
77
+ :client_secret => 'YOUR_CLIENT_SECRET',
78
+ :redirect_uri => 'http://example.com/oauth/callback'
73
79
  )
74
80
 
81
+ ## Refreshing OAuth tokens
82
+
83
+ Google+ OAuth tokens are currently only valid for 3600 seconds (one hour). You can use the `:refresh_token` to get a new OAuth token after your existing token expires, without requiring the user to re-authorize your application.
84
+
85
+ Gplus will automatically request a new token if the provided token has expired. You should check to see if this has occured so that you can store the new token. Otherwise, after the initial token expires, you'll be requesting a new token from Google each time you initialize an API client. This is slow!
86
+
87
+ You can determine whether a token has been refreshed by calling `access_token_refreshed?`:
88
+
89
+ if @client.access_token_refreshed?
90
+ access_token = @client.access_token
91
+ current_user.update_attributes(
92
+ :token => access_token.token,
93
+ :token_expires_at => access_token.expires_at
94
+ )
95
+ end
96
+
97
+ The refreshed OAuth token will have `:refresh_token` set to `nil`. Keep using the initial `:refresh_token` you were given for all future refreshes.
98
+
75
99
  ## [People](http://developers.google.com/+/api/latest/people)
76
100
 
77
101
  Get a person's profile with `client.get_person`:
@@ -86,7 +110,7 @@ The person's profile will be returned as a nested hash:
86
110
  person[:urls].count
87
111
  person[:name][:middleName]
88
112
 
89
- See the API documentation for [People](http://developers.google.com/+/api/latest/people) and [People: get](http://developers.google.com/+/api/latest/people/get) for more info.
113
+ See the Google+ API documentation for [People](http://developers.google.com/+/api/latest/people) and [People: get](http://developers.google.com/+/api/latest/people/get) for more info.
90
114
 
91
115
  ## [Activities](http://developers.google.com/+/api/latest/activities)
92
116
 
@@ -120,9 +144,9 @@ If you want more than 100 results, take the `:nextPageToken` returned from your
120
144
  activities = client.list_activities(id, :results => 100)
121
145
  more_activities = client.list_activities(id, :results => 100, :page => activities[:nextPageToken])
122
146
 
123
- See the API documentation for [Activities](http://developers.google.com/+/api/latest/activities), [Activities: get](http://developers.google.com/+/api/latest/activities/get) and [Activities: list](http://developers.google.com/+/api/latest/activities/list).
147
+ See the Google+ API documentation for [Activities](http://developers.google.com/+/api/latest/activities), [Activities: get](http://developers.google.com/+/api/latest/activities/get) and [Activities: list](http://developers.google.com/+/api/latest/activities/list).
124
148
 
125
- ## Contributing to Gplus
149
+ ## Contributing to gplus
126
150
 
127
151
  Please submit bug reports as [Github Issues](https://github.com/nfm/Gplus/issues).
128
152
 
data/lib/gplus/client.rb CHANGED
@@ -8,16 +8,20 @@ module Gplus
8
8
  # @param [Hash] options
9
9
  # @option options [String] :api_key Your application's API key, used for non-authenticated requests (for public data).
10
10
  # @option options [String] :token The OAuth token to authorize the API client for authenticated requests (for non-public data). This can be supplied after initialization by calling {#authorize}.
11
+ # @option options [String] :refresh_token The OAuth refresh_token, to request a new token if the provided token has expired.
12
+ # @option options [Integer] :token_expires_at The time that the OAuth token expires at in seconds since the epoch.
11
13
  # @option options [String] :client_id Your application's Client ID. Required to generate an authorization URL with {#authorize_url}.
12
14
  # @option options [String] :client_secret Your application's Client Secret. Required to generate an authorization URL with {#authorize_url}.
13
15
  # @option options [String] :redirect_uri The default URI to redirect to after authorization. You can override this in many other methods. It must be specified as an authorized URI in your application's console. Required to generate an authorization URL with #authorize_url.
14
16
  # @return [Gplus::Client] A Google+ API client.
15
17
  def initialize(options = {})
16
18
  @api_key = options[:api_key]
19
+ @token = options[:token]
20
+ @refresh_token = options[:refresh_token]
21
+ @token_expires_at = options[:token_expires_at]
17
22
  @client_id = options[:client_id]
18
23
  @client_secret = options[:client_secret]
19
24
  @redirect_uri = options[:redirect_uri]
20
- @token = options[:token]
21
25
 
22
26
  @oauth_client = OAuth2::Client.new(
23
27
  @client_id,
@@ -37,6 +41,7 @@ module Gplus
37
41
  end
38
42
 
39
43
  # Authorize an API client instance to access the user's private data.
44
+ #
40
45
  # @param [String] auth_code The code returned to your redirect_uri after the user authorized your application to access their Google+ data.
41
46
  # @param [String] redirect_uri An optional over-ride for the redirect_uri you initialized the API client with.
42
47
  # @return [OAuth2::AccessToken] An OAuth access token. Store access_token[:token] and access_token[:refresh_token] to get persistent access to the user's data until access_token[:expires_at].
@@ -44,13 +49,25 @@ module Gplus
44
49
  @access_token = @oauth_client.auth_code.get_token(auth_code, :redirect_uri => redirect_uri)
45
50
  end
46
51
 
47
- private
52
+ # Retrieve or create an OAuth2::AccessToken, using the :token and :refresh_token specified when the API client instance was initialized
53
+ #
54
+ # @return An OAuth2::AccessToken
48
55
  def access_token
49
56
  if @token
50
- @access_token ||= OAuth2::AccessToken.new(@oauth_client, @token)
57
+ @access_token ||= OAuth2::AccessToken.new(@oauth_client, @token, :refresh_token => @refresh_token, :expires_at => @token_expires_at)
58
+ if @access_token.expired?
59
+ @access_token.refresh!
60
+ @access_token_refreshed = true
61
+ end
51
62
  end
52
63
  end
53
64
 
65
+ # Return true if the user's access token has been refreshed. If so, you should store the new token's :token and :expires_at.
66
+ def access_token_refreshed?
67
+ @access_token_refreshed
68
+ end
69
+
70
+ private
54
71
  def get(path, params = {})
55
72
  if access_token
56
73
  response = access_token.get("v1/#{path}", params)
data/lib/gplus/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Gplus
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gplus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-17 00:00:00.000000000Z
12
+ date: 2011-09-18 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
16
- requirement: &11450460 !ruby/object:Gem::Requirement
16
+ requirement: &18783920 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '1.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *11450460
24
+ version_requirements: *18783920
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: oauth2
27
- requirement: &11449420 !ruby/object:Gem::Requirement
27
+ requirement: &18783100 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0.5'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *11449420
35
+ version_requirements: *18783100
36
36
  description: A complete implementation of the Google plus API for Ruby
37
37
  email:
38
38
  - nicholas@2suggestions.com.au