geoloqi 0.9.33 → 0.9.35
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.yardopts +3 -0
- data/{license.txt → LICENSE.md} +0 -0
- data/README.md +111 -0
- data/examples/simple.rb +10 -10
- data/examples/sinatra_simple.rb +19 -0
- data/examples/sinatra_synchrony.rb +16 -24
- data/geoloqi.gemspec +3 -2
- data/lib/geoloqi.rb +45 -2
- data/lib/geoloqi/config.rb +7 -1
- data/lib/geoloqi/data/ca-certificates.crt +3376 -0
- data/lib/geoloqi/session.rb +73 -7
- data/lib/geoloqi/version.rb +4 -1
- data/spec/env.rb +1 -1
- data/spec/geoloqi_config.rb +1 -1
- data/spec/geoloqi_session_spec.rb +21 -20
- data/spec/geoloqi_spec.rb +2 -2
- metadata +23 -11
- data/README.markdown +0 -158
- data/examples/sinatra.rb +0 -30
data/lib/geoloqi/session.rb
CHANGED
@@ -1,6 +1,26 @@
|
|
1
1
|
module Geoloqi
|
2
|
+
# This class is used to instantiate a session object. It is designed to be thread safe, and multiple sessions can be used simultaneously,
|
3
|
+
# allowing for one ruby application to potentially handle multiple Geoloqi applications.
|
4
|
+
#
|
5
|
+
# @example
|
6
|
+
# # Instantiate a session with your access token (obtained from the Geoloqi Developers Site):
|
7
|
+
# session = Geoloqi::Session.new :access_token => 'YOUR ACCESS TOKEN'
|
8
|
+
#
|
9
|
+
# # Instantiate a session with a custom config:
|
10
|
+
# session = Geoloqi::Session.new :access_token => 'YOUR ACCESS TOKEN', :config => {:use_hashie_mash => true}
|
11
|
+
#
|
12
|
+
# # Instantiate a session with OAuth2 credentials (obtained from the Geoloqi Developers Site):
|
13
|
+
# session = Geoloqi::Session.new :config => {:client_id => 'CLIENT ID', :client_secret => 'CLIENT SECRET'}
|
14
|
+
#
|
15
|
+
# # Get profile:
|
16
|
+
# result = session.get 'account/profile'
|
2
17
|
class Session
|
18
|
+
# This is the auth Hash, which is provided by the OAuth2 response. This can be stored externally and used to re-initialize the session.
|
19
|
+
# @return [Hash]
|
3
20
|
attr_reader :auth
|
21
|
+
|
22
|
+
# This is the config object attached to this session. It is unique to this session.
|
23
|
+
# @return [Geoloqi::Config]
|
4
24
|
attr_accessor :config
|
5
25
|
|
6
26
|
def initialize(opts={})
|
@@ -9,7 +29,7 @@ module Geoloqi
|
|
9
29
|
self.auth = opts[:auth] || {}
|
10
30
|
self.auth[:access_token] = opts[:access_token] if opts[:access_token]
|
11
31
|
|
12
|
-
@connection = Faraday.new(:url => Geoloqi.api_url) do |builder|
|
32
|
+
@connection = Faraday.new(:url => Geoloqi.api_url, :ssl => {:verify => true, :ca_file => Geoloqi::SSL_CERT_FILE}) do |builder|
|
13
33
|
builder.adapter @config.adapter || :net_http
|
14
34
|
end
|
15
35
|
end
|
@@ -18,47 +38,93 @@ module Geoloqi
|
|
18
38
|
@auth = hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
19
39
|
end
|
20
40
|
|
41
|
+
# The access token for this session.
|
42
|
+
# @return [String]
|
21
43
|
def access_token
|
22
44
|
@auth[:access_token]
|
23
45
|
end
|
24
46
|
|
47
|
+
# Determines if the access token exists.
|
48
|
+
# @return [Boolean]
|
25
49
|
def access_token?
|
26
50
|
!access_token.nil?
|
27
51
|
end
|
28
52
|
|
53
|
+
# The authorize url for this session.
|
54
|
+
#
|
55
|
+
# @return [String]
|
29
56
|
def authorize_url(redirect_uri=@config.redirect_uri, opts={})
|
30
57
|
Geoloqi.authorize_url @config.client_id, redirect_uri, opts
|
31
58
|
end
|
32
59
|
|
60
|
+
# Makes a GET request to the Geoloqi API server and returns response.
|
61
|
+
#
|
62
|
+
# @param [String] path
|
63
|
+
# Path to the resource being requested (example: '/account/profile').
|
64
|
+
#
|
65
|
+
# @param [String, Hash] query (optional)
|
66
|
+
# A query string or Hash to be appended to the request.
|
67
|
+
#
|
68
|
+
# @param [Hash] headers (optional)
|
69
|
+
# Adds and overwrites headers in request sent to server.
|
70
|
+
#
|
71
|
+
# @return [Hash] by default, [Hashie::Mash] if enabled in config.
|
72
|
+
# @example
|
73
|
+
# # Get your user profile
|
74
|
+
# response = session.get 'YOUR ACCESS TOKEN', 'account/profile'
|
75
|
+
#
|
76
|
+
# # Get the last 5 locations
|
77
|
+
# response = session.get 'YOUR ACCESS TOKEN', 'account/profile', :count => 5
|
33
78
|
def get(path, query=nil, headers={})
|
34
79
|
run :get, path, query, headers
|
35
80
|
end
|
36
81
|
|
82
|
+
# Makes a POST request to the Geoloqi API server and returns response.
|
83
|
+
#
|
84
|
+
# @param [String] path
|
85
|
+
# Path to the resource being requested (example: '/account/profile').
|
86
|
+
#
|
87
|
+
# @param [String, Hash] query (optional)
|
88
|
+
# A query string or Hash to be converted to POST parameters.
|
89
|
+
#
|
90
|
+
# @param [Hash] headers (optional)
|
91
|
+
# Adds and overwrites headers in request sent to server.
|
92
|
+
#
|
93
|
+
# @return [Hash] by default, [Hashie::Mash] if enabled in config.
|
94
|
+
# @example
|
95
|
+
# # Create a new layer
|
96
|
+
# Geoloqi.post 'YOUR ACCESS TOKEN', 'layer/create', :name => 'Portland Food Carts'
|
37
97
|
def post(path, query=nil, headers={})
|
38
98
|
run :post, path, query, headers
|
39
99
|
end
|
40
100
|
|
101
|
+
# Makes a one-time request to the Geoloqi API server.
|
102
|
+
#
|
103
|
+
# @return [Hash] by default, [Hashie::Mash] if enabled in config.
|
104
|
+
# @example
|
105
|
+
# # Create a new layer
|
106
|
+
# session.post 'YOUR ACCESS TOKEN', 'layer/create', :name => 'Northeast Portland'
|
41
107
|
def run(meth, path, query=nil, headers={})
|
42
108
|
renew_access_token! if auth[:expires_at] && Time.rfc2822(auth[:expires_at]) <= Time.now && !(path =~ /^\/?oauth\/token$/)
|
43
109
|
retry_attempt = 0
|
44
110
|
|
45
111
|
begin
|
46
112
|
response = execute meth, path, query, headers
|
47
|
-
hash = JSON.parse response.body
|
113
|
+
hash = JSON.parse response.body, :symbolize_names => @config.symbolize_names
|
48
114
|
|
49
|
-
if hash.is_a?(Hash) && hash[
|
50
|
-
if @config.use_dynamic_exceptions && !hash[
|
51
|
-
exception_class_name = hash[
|
115
|
+
if hash.is_a?(Hash) && hash[:error] && @config.throw_exceptions
|
116
|
+
if @config.use_dynamic_exceptions && !hash[:error].nil? && !hash[:error].empty?
|
117
|
+
exception_class_name = hash[:error].gsub(/\W+/, '_').split('_').collect {|w| w.capitalize}.join+'Error'
|
52
118
|
Geoloqi.const_set exception_class_name, Class.new(Geoloqi::ApiError) unless Geoloqi.const_defined? exception_class_name
|
53
119
|
raise_class = Geoloqi.const_get exception_class_name
|
54
120
|
else
|
55
121
|
raise_class = ApiError
|
56
122
|
end
|
57
|
-
raise raise_class.new(response.status, hash[
|
123
|
+
raise raise_class.new(response.status, hash[:error], hash[:error_description])
|
58
124
|
end
|
59
125
|
rescue Geoloqi::ApiError
|
60
126
|
raise Error.new('Unable to procure fresh access token from API on second attempt') if retry_attempt > 0
|
61
|
-
if hash[
|
127
|
+
if hash[:error] == 'expired_token' && !(hash[:error_description] =~ /The auth code expired/)
|
62
128
|
renew_access_token!
|
63
129
|
retry_attempt += 1
|
64
130
|
retry
|
data/lib/geoloqi/version.rb
CHANGED
data/spec/env.rb
CHANGED
data/spec/geoloqi_config.rb
CHANGED
@@ -27,7 +27,7 @@ describe Geoloqi::Config do
|
|
27
27
|
it 'displays log information if logger is provided and query is nil' do
|
28
28
|
stub_request(:get, api_url('account/username')).
|
29
29
|
with(:headers => {'Authorization'=>'OAuth access_token1234'}).
|
30
|
-
to_return(:body => {
|
30
|
+
to_return(:body => {:username => 'bulbasaurrulzok'}.to_json)
|
31
31
|
|
32
32
|
io = StringIO.new
|
33
33
|
Geoloqi.config :client_id => CLIENT_ID, :client_secret => CLIENT_SECRET, :logger => io
|
@@ -15,7 +15,7 @@ describe Geoloqi::Session do
|
|
15
15
|
it 'should throw api error exception' do
|
16
16
|
stub_request(:get, api_url('badmethodcall')).
|
17
17
|
with(:headers => auth_headers).
|
18
|
-
to_return(:status => 404, :body => {
|
18
|
+
to_return(:status => 404, :body => {:error => 'not_found'}.to_json)
|
19
19
|
|
20
20
|
expect { rescuing {Geoloqi::Session.new(:access_token => 'access_token1234').get('badmethodcall')}.class == Geoloqi::ApiError }
|
21
21
|
end
|
@@ -23,13 +23,14 @@ describe Geoloqi::Session do
|
|
23
23
|
|
24
24
|
describe 'custom exceptions scheme' do
|
25
25
|
before do
|
26
|
-
@session = Geoloqi::Session.new :access_token => 'access_token1234', :config => {:
|
26
|
+
@session = Geoloqi::Session.new :access_token => 'access_token1234', :config => {:throw_exceptions => true,
|
27
|
+
:use_dynamic_exceptions => true}
|
27
28
|
end
|
28
29
|
|
29
30
|
it 'should throw api error exception with custom name' do
|
30
31
|
stub_request(:get, api_url('specialerror')).
|
31
32
|
with(:headers => auth_headers).
|
32
|
-
to_return(:status => 404, :body => {
|
33
|
+
to_return(:status => 404, :body => {:error => 'not_found'}.to_json)
|
33
34
|
|
34
35
|
expect { rescuing {@session.get('specialerror')}.class == Geoloqi::NotFoundError }
|
35
36
|
end
|
@@ -37,7 +38,7 @@ describe Geoloqi::Session do
|
|
37
38
|
it 'should throw api error exception without custom name if empty' do
|
38
39
|
stub_request(:get, api_url('specialerror')).
|
39
40
|
with(:headers => auth_headers).
|
40
|
-
to_return(:status => 404, :body => {
|
41
|
+
to_return(:status => 404, :body => {:error => ''}.to_json)
|
41
42
|
|
42
43
|
expect { rescuing {@session.get('specialerror')}.class == Geoloqi::ApiError }
|
43
44
|
end
|
@@ -51,10 +52,10 @@ describe Geoloqi::Session do
|
|
51
52
|
it 'should not throw api error exception' do
|
52
53
|
stub_request(:get, api_url('badmethodcall')).
|
53
54
|
with(:headers => auth_headers).
|
54
|
-
to_return(:status => 404, :body => {
|
55
|
+
to_return(:status => 404, :body => {:error => 'not_found'}.to_json)
|
55
56
|
|
56
57
|
response = @session.get 'badmethodcall'
|
57
|
-
expect {response[
|
58
|
+
expect {response[:error] == 'not_found'}
|
58
59
|
end
|
59
60
|
end
|
60
61
|
|
@@ -66,7 +67,7 @@ describe Geoloqi::Session do
|
|
66
67
|
it 'should respond to method calls in addition to hash' do
|
67
68
|
stub_request(:get, api_url('account/username')).
|
68
69
|
with(:headers => {'Authorization'=>'OAuth access_token1234'}).
|
69
|
-
to_return(:body => {
|
70
|
+
to_return(:body => {:username => 'bulbasaurrulzok'}.to_json)
|
70
71
|
|
71
72
|
response = @session.get 'account/username'
|
72
73
|
expect { response['username'] == 'bulbasaurrulzok' }
|
@@ -95,18 +96,18 @@ describe Geoloqi::Session do
|
|
95
96
|
it 'successfully makes call with array' do
|
96
97
|
stub_request(:post, api_url('play_record_at_geoloqi_hq')).
|
97
98
|
with(:headers => auth_headers, :body => [{:artist => 'Television'}].to_json).
|
98
|
-
to_return(:body => {
|
99
|
+
to_return(:body => {:result => 'ok'}.to_json)
|
99
100
|
|
100
|
-
expect { @session.post('play_record_at_geoloqi_hq', [{:artist => 'Television'}])[
|
101
|
+
expect { @session.post('play_record_at_geoloqi_hq', [{:artist => 'Television'}])[:result] == 'ok' }
|
101
102
|
end
|
102
103
|
|
103
104
|
it 'successfully makes call to api' do
|
104
105
|
stub_request(:get, api_url('layer/info/Gx')).
|
105
106
|
with(:headers => auth_headers).
|
106
|
-
to_return(:status => 200, :body => {
|
107
|
+
to_return(:status => 200, :body => {:layer_id => 'Gx'}.to_json)
|
107
108
|
|
108
109
|
%w{/layer/info/Gx layer/info/Gx}.each do |path|
|
109
|
-
expect { @session.get(path)[
|
110
|
+
expect { @session.get(path)[:layer_id] == 'Gx' }
|
110
111
|
end
|
111
112
|
end
|
112
113
|
|
@@ -118,15 +119,15 @@ describe Geoloqi::Session do
|
|
118
119
|
end
|
119
120
|
|
120
121
|
it 'makes a location/history call with get and hash params' do
|
121
|
-
expect { @session.get('location/history', :count => 2)[
|
122
|
+
expect { @session.get('location/history', :count => 2)[:points].count == 2 }
|
122
123
|
end
|
123
124
|
|
124
125
|
it 'makes a location/history call with get and query string directly in path' do
|
125
|
-
expect { @session.get('location/history?count=2')[
|
126
|
+
expect { @session.get('location/history?count=2')[:points].count == 2 }
|
126
127
|
end
|
127
128
|
|
128
129
|
it 'makes a location/history call with get and query string params' do
|
129
|
-
expect { @session.get('location/history', 'count=2')[
|
130
|
+
expect { @session.get('location/history', 'count=2')[:points].count == 2 }
|
130
131
|
end
|
131
132
|
end
|
132
133
|
end
|
@@ -182,7 +183,7 @@ describe Geoloqi::Session do
|
|
182
183
|
it 'fails with an exception' do
|
183
184
|
stub_request(:post, api_url('message/send')).
|
184
185
|
with(:headers => auth_headers('hey brah whats up let me in its cool 8)')).
|
185
|
-
to_return(:status => 401, :body => {
|
186
|
+
to_return(:status => 401, :body => {:error => 'invalid_token'}.to_json)
|
186
187
|
|
187
188
|
begin
|
188
189
|
@session.post 'message/send'
|
@@ -215,9 +216,9 @@ describe Geoloqi::Session do
|
|
215
216
|
response = @session.get_auth '1234', 'http://example.com'
|
216
217
|
|
217
218
|
{:access_token => 'access_token1234',
|
218
|
-
|
219
|
-
|
220
|
-
|
219
|
+
:scope => nil,
|
220
|
+
:expires_in => '86400',
|
221
|
+
:refresh_token => 'refresh_token1234'}.each do |k,v|
|
221
222
|
expect { response[k] == v }
|
222
223
|
end
|
223
224
|
end
|
@@ -246,7 +247,7 @@ describe Geoloqi::Session do
|
|
246
247
|
response = @session.get 'account/username'
|
247
248
|
|
248
249
|
expect { @session.auth[:access_token] == 'access_token1234' }
|
249
|
-
expect { response[
|
250
|
+
expect { response[:username] == 'bulbasaurrulzok' }
|
250
251
|
end
|
251
252
|
|
252
253
|
it 'does not attempt to refresh for auth code expire' do
|
@@ -292,7 +293,7 @@ describe Geoloqi::Session do
|
|
292
293
|
|
293
294
|
stub_request(:get, api_url('account/username')).
|
294
295
|
with(:headers => {'Authorization'=>'OAuth access_token4567'}).
|
295
|
-
to_return(:body => {
|
296
|
+
to_return(:body => {:username => 'pikachu4lyfe'}.to_json)
|
296
297
|
|
297
298
|
@session.get 'account/username'
|
298
299
|
expect { @session.auth[:access_token] == 'access_token4567' }
|
data/spec/geoloqi_spec.rb
CHANGED
@@ -33,7 +33,7 @@ describe Geoloqi do
|
|
33
33
|
to_return(:body => {:result => 'ok'}.to_json)
|
34
34
|
|
35
35
|
response = Geoloqi.get ACCESS_TOKEN, '/quick_get', {:lol => 'cats'}, 'Special' => 'header'
|
36
|
-
expect { response[
|
36
|
+
expect { response[:result] == 'ok' }
|
37
37
|
end
|
38
38
|
|
39
39
|
it 'makes post request' do
|
@@ -43,6 +43,6 @@ describe Geoloqi do
|
|
43
43
|
to_return(:body => {:result => 'ok'}.to_json)
|
44
44
|
|
45
45
|
response = Geoloqi.post ACCESS_TOKEN, '/quick_post', {:lol => 'dogs'}, 'Special' => 'header'
|
46
|
-
expect { response[
|
46
|
+
expect { response[:result] == 'ok' }
|
47
47
|
end
|
48
48
|
end
|
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
|
+
version: 0.9.35
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Kyle Drake
|
@@ -11,8 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date:
|
15
|
-
default_executable:
|
14
|
+
date: 2012-01-08 00:00:00 Z
|
16
15
|
dependencies:
|
17
16
|
- !ruby/object:Gem::Dependency
|
18
17
|
name: json
|
@@ -55,7 +54,7 @@ dependencies:
|
|
55
54
|
requirements:
|
56
55
|
- - "="
|
57
56
|
- !ruby/object:Gem::Version
|
58
|
-
version: 0.
|
57
|
+
version: 0.6.0
|
59
58
|
type: :development
|
60
59
|
version_requirements: *id004
|
61
60
|
- !ruby/object:Gem::Dependency
|
@@ -91,10 +90,21 @@ dependencies:
|
|
91
90
|
version: 1.0.0
|
92
91
|
type: :development
|
93
92
|
version_requirements: *id007
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: yard
|
95
|
+
prerelease: false
|
96
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: "0"
|
102
|
+
type: :development
|
103
|
+
version_requirements: *id008
|
94
104
|
description: Powerful, flexible, lightweight interface to the awesome Geoloqi platform API! Uses Faraday, and can be used with Ruby 1.9 and EM-Synchrony for really fast, highly concurrent development.
|
95
105
|
email:
|
96
|
-
-
|
97
|
-
- aaron@
|
106
|
+
- kyle@geoloqi.com
|
107
|
+
- aaron@geoloqi.com
|
98
108
|
executables: []
|
99
109
|
|
100
110
|
extensions: []
|
@@ -104,26 +114,27 @@ extra_rdoc_files: []
|
|
104
114
|
files:
|
105
115
|
- .gitignore
|
106
116
|
- .travis.yml
|
117
|
+
- .yardopts
|
107
118
|
- Gemfile
|
108
|
-
-
|
119
|
+
- LICENSE.md
|
120
|
+
- README.md
|
109
121
|
- Rakefile
|
110
122
|
- examples/simple.rb
|
111
|
-
- examples/
|
123
|
+
- examples/sinatra_simple.rb
|
112
124
|
- examples/sinatra_synchrony.rb
|
113
125
|
- geoloqi.gemspec
|
114
126
|
- lib/geoloqi.rb
|
115
127
|
- lib/geoloqi/config.rb
|
128
|
+
- lib/geoloqi/data/ca-certificates.crt
|
116
129
|
- lib/geoloqi/error.rb
|
117
130
|
- lib/geoloqi/response.rb
|
118
131
|
- lib/geoloqi/session.rb
|
119
132
|
- lib/geoloqi/version.rb
|
120
|
-
- license.txt
|
121
133
|
- spec/env.rb
|
122
134
|
- spec/geoloqi_api_error_spec.rb
|
123
135
|
- spec/geoloqi_config.rb
|
124
136
|
- spec/geoloqi_session_spec.rb
|
125
137
|
- spec/geoloqi_spec.rb
|
126
|
-
has_rdoc: true
|
127
138
|
homepage: http://github.com/geoloqi/geoloqi-ruby
|
128
139
|
licenses: []
|
129
140
|
|
@@ -147,9 +158,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
147
158
|
requirements: []
|
148
159
|
|
149
160
|
rubyforge_project: geoloqi
|
150
|
-
rubygems_version: 1.
|
161
|
+
rubygems_version: 1.8.9
|
151
162
|
signing_key:
|
152
163
|
specification_version: 3
|
153
164
|
summary: Powerful, flexible, lightweight interface to the awesome Geoloqi platform API
|
154
165
|
test_files: []
|
155
166
|
|
167
|
+
has_rdoc:
|
data/README.markdown
DELETED
@@ -1,158 +0,0 @@
|
|
1
|
-
Geoloqi Library for Ruby [![](https://secure.travis-ci.org/geoloqi/geoloqi-ruby.png)](http://travis-ci.org/geoloqi/geoloqi-ruby)
|
2
|
-
===
|
3
|
-
Powerful, flexible, lightweight interface to the awesome Geoloqi platform API! Uses Faraday, and can be used with Ruby 1.9 and EM-Synchrony for really fast, highly concurrent development.
|
4
|
-
|
5
|
-
This library was developed with two goals in mind: to be as simple as possible, but also to be very powerful to allow for much higher-end development (multiple Geoloqi apps per instance, concurrency, performance).
|
6
|
-
|
7
|
-
Installation
|
8
|
-
---
|
9
|
-
|
10
|
-
gem install geoloqi
|
11
|
-
|
12
|
-
Basic Usage
|
13
|
-
---
|
14
|
-
Geoloqi uses OAuth2 for authentication, but if you're only working with your own account, you don't need to go through the authorization steps! Simply go to your account settings on the [Geoloqi site](http://geoloqi.com), click on "Connections" and copy the OAuth 2 Access Token. You can use this token to run the following examples.
|
15
|
-
|
16
|
-
If you just need to make simple requests, you can just make a simple get or post request from Geoloqi:
|
17
|
-
|
18
|
-
require 'geoloqi'
|
19
|
-
result = Geoloqi.get 'YOUR ACCESS TOKEN', 'layer/info/Gx'
|
20
|
-
puts response.inspect
|
21
|
-
|
22
|
-
# or a POST!
|
23
|
-
result = Geoloqi.post 'YOUR ACCESS TOKEN', 'layer/create', :name => 'Test Layer'
|
24
|
-
|
25
|
-
If you're using Geoloqi with OAuth or making multiple requests, we recommend using the Geoloqi::Session class:
|
26
|
-
|
27
|
-
require 'geoloqi'
|
28
|
-
geoloqi = Geoloqi::Session.new :access_token => 'YOUR ACCESS TOKEN'
|
29
|
-
response = geoloqi.get 'layer/info/Gx'
|
30
|
-
puts response.inspect
|
31
|
-
|
32
|
-
Which returns a hash with the following:
|
33
|
-
|
34
|
-
{"layer_id"=>"Gx", "user_id"=>"4", "type"=>"normal", "name"=>"USGS Earthquakes",
|
35
|
-
"description"=>"Real-time notifications of earthquakes near you.",
|
36
|
-
"icon"=>"http://beta.geoloqi.com/images/earthquake-layer.png", "public"=>"1",
|
37
|
-
"url"=>"https://a.geoloqi.com/layer/description/Gx", "subscription"=>false, "settings"=>false}
|
38
|
-
|
39
|
-
Both GET and POST are supported. To send a POST to create a place (in this case, the entire city of Portland, Oregon):
|
40
|
-
|
41
|
-
response = geoloqi.post 'place/create', {
|
42
|
-
"layer_id" => "1Wn",
|
43
|
-
"name" => "3772756364",
|
44
|
-
"latitude" => "45.5037078163837",
|
45
|
-
"longitude" => "-122.622699737549",
|
46
|
-
"radius" => "3467.44",
|
47
|
-
"extra" => {
|
48
|
-
"description" => "Portland",
|
49
|
-
"url" => "http://en.wikipedia.org/wiki/Portland"
|
50
|
-
}
|
51
|
-
}
|
52
|
-
|
53
|
-
This returns response['place_id'], which you can use to store and/or remove the place:
|
54
|
-
|
55
|
-
response = geoloqi.post "place/delete/#{response['place_id']}"
|
56
|
-
|
57
|
-
Which returns response['result'] with a value of "ok".
|
58
|
-
|
59
|
-
You can send query string parameters with get requests too:
|
60
|
-
|
61
|
-
geoloqi.get 'location/history', :count => 2
|
62
|
-
# or
|
63
|
-
geoloqi.get 'location/history?count=2'
|
64
|
-
|
65
|
-
Hashie::Mash support
|
66
|
-
---
|
67
|
-
Want to access in a more OOP/JSON style way? Use Hashie::Mash as the response object:
|
68
|
-
|
69
|
-
require 'geoloqi'
|
70
|
-
geoloqi = Geoloqi::Session.new :access_token => 'YOUR OAUTH2 ACCESS TOKEN GOES HERE', :config => {:use_hashie_mash => true}
|
71
|
-
response = geoloqi.get 'layer/info/Gx'
|
72
|
-
response.layer_id # this works
|
73
|
-
response['layer_id'] # this works too
|
74
|
-
response[:layer_id] # so does this
|
75
|
-
|
76
|
-
Implementing OAuth2
|
77
|
-
---
|
78
|
-
|
79
|
-
OAuth2 has been implemented so that it's very easy to store the session's authorization data in a hash. If the access token expires or becomes invalid, the refresh token is used to retrieve a fresh token. This is done automatically, so you don't have to worry about it. Just store the hash in a session, feed it back in on each request and you're good to go.
|
80
|
-
|
81
|
-
Here is a simple Sinatra example implementing the OAuth2 flow with Geoloqi:
|
82
|
-
|
83
|
-
require 'rubygems'
|
84
|
-
require 'sinatra'
|
85
|
-
require 'geoloqi'
|
86
|
-
|
87
|
-
GEOLOQI_REDIRECT_URI = 'http://yourwebsite.net'
|
88
|
-
|
89
|
-
enable :sessions
|
90
|
-
set :session_secret, 'PUT A SECRET WORD HERE' # Encrypts the cookie session.. recommended.
|
91
|
-
|
92
|
-
def geoloqi
|
93
|
-
@geoloqi ||= Geoloqi::Session.new :auth => session[:geoloqi_auth],
|
94
|
-
:config => {:client_id => 'YOUR OAUTH CLIENT ID',
|
95
|
-
:client_secret => 'YOUR CLIENT SECRET'}
|
96
|
-
end
|
97
|
-
|
98
|
-
# If the access token expires, Geoloqi::Session will refresh inline!
|
99
|
-
# This after block makes sure the session gets the updated config.
|
100
|
-
after do
|
101
|
-
session[:geoloqi_auth] = @geoloqi.auth
|
102
|
-
end
|
103
|
-
|
104
|
-
get '/?' do
|
105
|
-
geoloqi.get_auth(params[:code], GEOLOQI_REDIRECT_URI) if params[:code] && !geoloqi.access_token?
|
106
|
-
redirect geoloqi.authorize_url(GEOLOQI_REDIRECT_URI) unless geoloqi.access_token?
|
107
|
-
|
108
|
-
username = geoloqi.get('account/username')['username']
|
109
|
-
"You have successfully logged in as #{username}!"
|
110
|
-
end
|
111
|
-
|
112
|
-
Now, here's a power example: Uses [sinatra-synchrony](http://github.com/kyledrake/sinatra-synchrony) to provide a massive concurrency improvement via EventMachine. This will allow your app to serve other requests, the app will not hang while waiting for content from the Geoloqi API. Works on anything that supports Thin (Rack, EY, Heroku, etc):
|
113
|
-
|
114
|
-
# To install deps: gem install sinatra sinatra-synchrony geoloqi
|
115
|
-
# To run from command line: ruby sinatra_synchrony.rb -s thin
|
116
|
-
require 'rubygems'
|
117
|
-
require 'sinatra'
|
118
|
-
require 'sinatra/synchrony'
|
119
|
-
require 'geoloqi'
|
120
|
-
|
121
|
-
GEOLOQI_REDIRECT_URI = 'http://example.com'
|
122
|
-
|
123
|
-
enable :sessions
|
124
|
-
set :session_secret, 'PUT A SECRET WORD HERE'
|
125
|
-
|
126
|
-
configure do
|
127
|
-
Geoloqi.config :client_id => 'YOUR OAUTH CLIENT ID', :client_secret => 'YOUR CLIENT SECRET', :adapter => :em_synchrony
|
128
|
-
end
|
129
|
-
|
130
|
-
def geoloqi
|
131
|
-
@geoloqi ||= Geoloqi::Session.new :auth => session[:geoloqi_auth]
|
132
|
-
end
|
133
|
-
|
134
|
-
after do
|
135
|
-
session[:geoloqi_auth] = @geoloqi.auth
|
136
|
-
end
|
137
|
-
|
138
|
-
get '/?' do
|
139
|
-
session[:geoloqi_auth] = geoloqi.get_auth(params[:code], GEOLOQI_REDIRECT_URI) if params[:code] && !geoloqi.access_token?
|
140
|
-
redirect geoloqi.authorize_url(GEOLOQI_REDIRECT_URI) unless geoloqi.access_token?
|
141
|
-
username = geoloqi.get('account/username')['username']
|
142
|
-
"You have successfully logged in as #{username}!"
|
143
|
-
end
|
144
|
-
|
145
|
-
Found a bug?
|
146
|
-
---
|
147
|
-
Let us know! Send a pull request or a patch. Questions? Ask! We're here to help. File issues, we'll respond to them!
|
148
|
-
|
149
|
-
Authors
|
150
|
-
---
|
151
|
-
* Kyle Drake
|
152
|
-
* Aaron Parecki
|
153
|
-
|
154
|
-
TODO / Possible projects
|
155
|
-
---
|
156
|
-
* Plugin for Sinatra
|
157
|
-
* Rails plugin (works fine as-is, but maybe we can make it easier?)
|
158
|
-
* More Concrete API in addition to the simple one?
|