geoloqi 0.9.5 → 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  Gemfile.lock
2
+ *.gem
data/README.markdown CHANGED
@@ -65,13 +65,20 @@ Here is a simple Sinatra example implementing the OAuth2 flow with Geoloqi:
65
65
  GEOLOQI_REDIRECT_URI = 'http://yourwebsite.net'
66
66
 
67
67
  enable :sessions
68
+ set :session_secret, 'PUT A SECRET WORD HERE' # Encrypts the cookie session.. recommended.
68
69
 
69
70
  def geoloqi
70
71
  @geoloqi ||= Geoloqi::Session.new :auth => session[:geoloqi_auth],
71
72
  :config => {:client_id => 'YOUR OAUTH CLIENT ID',
72
73
  :client_secret => 'YOUR CLIENT SECRET'}
73
74
  end
74
-
75
+
76
+ # If the access token expires, Geoloqi::Session will refresh inline!
77
+ # This after block makes sure the session gets the updated config.
78
+ after do
79
+ session[:geoloqi_auth] = @geoloqi.auth
80
+ end
81
+
75
82
  get '/?' do
76
83
  session[:geoloqi_auth] = geoloqi.get_auth(params[:code], GEOLOQI_REDIRECT_URI) if params[:code] && !geoloqi.access_token?
77
84
  redirect geoloqi.authorize_url(GEOLOQI_REDIRECT_URI) unless geoloqi.access_token?
@@ -91,6 +98,7 @@ Now, here's a power example: Uses [sinatra-synchrony](http://github.com/kyledrak
91
98
  GEOLOQI_REDIRECT_URI = 'http://example.com'
92
99
 
93
100
  enable :sessions
101
+ set :session_secret, 'PUT A SECRET WORD HERE'
94
102
 
95
103
  configure do
96
104
  Geoloqi.config :client_id => 'YOUR OAUTH CLIENT ID', :client_secret => 'YOUR CLIENT SECRET', :adapter => :em_synchrony
@@ -99,6 +107,10 @@ Now, here's a power example: Uses [sinatra-synchrony](http://github.com/kyledrak
99
107
  def geoloqi
100
108
  @geoloqi ||= Geoloqi::Session.new :auth => session[:geoloqi_auth]
101
109
  end
110
+
111
+ after do
112
+ session[:geoloqi_auth] = @geoloqi.auth
113
+ end
102
114
 
103
115
  get '/?' do
104
116
  session[:geoloqi_auth] = geoloqi.get_auth(params[:code], GEOLOQI_REDIRECT_URI) if params[:code] && !geoloqi.access_token?
@@ -114,4 +126,11 @@ Let us know! Send a pull request or a patch. Questions? Ask! We're here to help.
114
126
  Authors
115
127
  ---
116
128
  * Kyle Drake
117
- * Aaron Parecki
129
+ * Aaron Parecki
130
+
131
+ TODO / Possible projects
132
+ ---
133
+ * Plugin for Sinatra
134
+ * Rails plugin (works fine as-is, but maybe we can make it easier?)
135
+ * More Concrete API in addition to the simple one?
136
+ * Hashie::Mash and/or SymbolTable support out of the box
@@ -41,8 +41,9 @@ module Geoloqi
41
41
 
42
42
  def run(meth, path, query=nil)
43
43
  query = Rack::Utils.parse_query query if query.is_a?(String)
44
- renew_access_token! if auth[:expires_at] && auth[:expires_at] <= Time.now
44
+ renew_access_token! if auth[:expires_at] && Time.rfc2822(auth[:expires_at]) <= Time.now && !(path =~ /^\/?oauth\/token$/)
45
45
  retry_attempt = 0
46
+
46
47
  begin
47
48
  response = @connection.send(meth) do |req|
48
49
  req.url "/#{API_VERSION.to_s}/#{path.gsub(/^\//, '')}"
@@ -68,57 +69,40 @@ module Geoloqi
68
69
  json
69
70
  end
70
71
 
71
- # TODO: DRY and refactor
72
72
  def renew_access_token!
73
73
  require 'client_id and client_secret are required to get access token' unless @config.client_id? && @config.client_secret?
74
- args = {:client_id => @config.client_id,
75
- :client_secret => @config.client_secret,
76
- :grant_type => 'refresh_token',
77
- :refresh_token => auth[:refresh_token]}
78
-
79
- response = @connection.post do |req|
80
- req.url "/#{API_VERSION.to_s}/oauth/token"
81
- req.headers = headers false
82
- req.body = args.to_json
83
- end
84
-
85
- auth = JSON.parse response.body
74
+ auth = post 'oauth/token', :client_id => @config.client_id,
75
+ :client_secret => @config.client_secret,
76
+ :grant_type => 'refresh_token',
77
+ :refresh_token => self.auth[:refresh_token]
86
78
 
87
79
  # expires_at is likely incorrect. I'm chopping 5 seconds
88
80
  # off to allow for a more graceful failover.
89
- auth['expires_at'] = (Time.now + @expires_in.to_i)-5
90
-
91
- self.auth = JSON.parse response.body
81
+ auth['expires_at'] = ((Time.now + auth['expires_in'].to_i)-5).rfc2822
82
+ self.auth = auth
92
83
  self.auth
93
84
  end
94
85
 
95
86
  def get_auth(code, redirect_uri=@config.redirect_uri)
96
87
  require 'client_id and client_secret are required to get access token' unless @config.client_id? && @config.client_secret?
97
- args = {:client_id => @config.client_id,
98
- :client_secret => @config.client_secret,
99
- :code => code,
100
- :grant_type => 'authorization_code',
101
- :redirect_uri => redirect_uri}
102
-
103
- response = @connection.post do |req|
104
- req.url "/#{API_VERSION.to_s}/oauth/token"
105
- req.headers = headers false
106
- req.body = args.to_json
107
- end
108
-
109
- auth = JSON.parse response.body
88
+ auth = post 'oauth/token', :client_id => @config.client_id,
89
+ :client_secret => @config.client_secret,
90
+ :code => code,
91
+ :grant_type => 'authorization_code',
92
+ :redirect_uri => redirect_uri
110
93
 
111
94
  # expires_at is likely incorrect. I'm chopping 5 seconds
112
95
  # off to allow for a more graceful failover.
113
- auth['expires_at'] = (Time.now + @expires_in.to_i)-5
114
-
115
- self.auth = JSON.parse response.body
96
+ auth['expires_at'] = ((Time.now + auth['expires_in'].to_i)-5).rfc2822
97
+ self.auth = auth
116
98
  self.auth
117
99
  end
118
100
 
119
- def headers(with_oauth=true)
101
+ private
102
+
103
+ def headers
120
104
  headers = {'Content-Type' => 'application/json', 'User-Agent' => "geoloqi-ruby #{Geoloqi.version}", 'Accept' => 'application/json'}
121
- headers['Authorization'] = "OAuth #{access_token}" if with_oauth
105
+ headers['Authorization'] = "OAuth #{access_token}" if access_token
122
106
  headers
123
107
  end
124
108
  end
@@ -1,5 +1,5 @@
1
1
  module Geoloqi
2
2
  def self.version
3
- '0.9.5'
3
+ '0.9.6'
4
4
  end
5
5
  end
data/spec/geoloqi_spec.rb CHANGED
@@ -97,11 +97,11 @@ describe Geoloqi::Session do
97
97
  it 'makes a location/history call with get and hash params' do
98
98
  expect { @session.get('location/history', :count => 2)['points'].count == 2 }
99
99
  end
100
-
100
+
101
101
  it 'makes a location/history call with get and query string directly in path' do
102
102
  expect { @session.get('location/history?count=2')['points'].count == 2 }
103
103
  end
104
-
104
+
105
105
  it 'makes a location/history call with get and query string params' do
106
106
  expect { @session.get('location/history', 'count=2')['points'].count == 2 }
107
107
  end
@@ -176,10 +176,15 @@ describe Geoloqi::Session do
176
176
  ensure
177
177
  WebMock.allow_net_connect!
178
178
  end
179
- expect { response == {:access_token => 'access_token1234',
179
+
180
+ {:access_token => 'access_token1234',
180
181
  :scope => nil,
181
182
  :expires_in => '86400',
182
- :refresh_token => 'refresh_token1234'} }
183
+ :refresh_token => 'refresh_token1234'}.each do |k,v|
184
+ expect { response[k] == v }
185
+ end
186
+
187
+ expect { (5..10).include? (Time.rfc2822(response[:expires_at]) - (Time.now+86400)).abs }
183
188
  end
184
189
  end
185
190
 
@@ -189,7 +194,7 @@ describe Geoloqi::Session do
189
194
  :auth => { :access_token => 'access_token1234',
190
195
  :scope => nil,
191
196
  :expires_in => '86400',
192
- :expires_at => Time.at(0),
197
+ :expires_at => Time.at(0).rfc2822,
193
198
  :refresh_token => 'refresh_token1234' }
194
199
  end
195
200
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: geoloqi
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.9.5
5
+ version: 0.9.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kyle Drake
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2011-06-17 00:00:00 -07:00
14
+ date: 2011-06-23 00:00:00 -07:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
118
  requirements: []
119
119
 
120
120
  rubyforge_project: geoloqi
121
- rubygems_version: 1.5.2
121
+ rubygems_version: 1.6.2
122
122
  signing_key:
123
123
  specification_version: 3
124
124
  summary: Powerful, flexible, lightweight interface to the awesome Geoloqi platform API