geoloqi 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile +3 -0
- data/README.markdown +102 -0
- data/examples/simple.rb +26 -0
- data/examples/sinatra.rb +21 -0
- data/examples/sinatra_synchrony.rb +29 -0
- data/geoloqi.gemspec +25 -0
- data/lib/geoloqi.rb +26 -0
- data/lib/geoloqi/config.rb +18 -0
- data/lib/geoloqi/error.rb +10 -0
- data/lib/geoloqi/session.rb +123 -0
- data/lib/geoloqi/version.rb +3 -0
- data/spec/geoloqi_spec.rb +198 -0
- metadata +147 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Gemfile.lock
|
data/Gemfile
ADDED
data/README.markdown
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
Geoloqi Library for 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
|
+
require 'geoloqi'
|
17
|
+
geoloqi = Geoloqi::Session.new :access_token => 'YOUR OAUTH2 ACCESS TOKEN GOES HERE'
|
18
|
+
response = geoloqi.get 'layer/info/Gx'
|
19
|
+
puts response.inspect
|
20
|
+
|
21
|
+
This example returns a hash with the following:
|
22
|
+
|
23
|
+
{"layer_id"=>"Gx", "user_id"=>"4", "type"=>"normal", "name"=>"USGS Earthquakes",
|
24
|
+
"description"=>"Real-time notifications of earthquakes near you.",
|
25
|
+
"icon"=>"http://beta.geoloqi.com/images/earthquake-layer.png", "public"=>"1",
|
26
|
+
"url"=>"https://a.geoloqi.com/layer/description/Gx", "subscription"=>false, "settings"=>false}
|
27
|
+
|
28
|
+
Both GET and POST are supported. To send a POST to create a place (in this case, the entire city of Portland, Oregon):
|
29
|
+
|
30
|
+
response = geoloqi.post 'place/create', {
|
31
|
+
"layer_id" => "1Wn",
|
32
|
+
"name" => "3772756364",
|
33
|
+
"latitude" => "45.5037078163837",
|
34
|
+
"longitude" => "-122.622699737549",
|
35
|
+
"radius" => "3467.44",
|
36
|
+
"extra" => {
|
37
|
+
"description" => "Portland",
|
38
|
+
"url" => "http://en.wikipedia.org/wiki/Portland"
|
39
|
+
}
|
40
|
+
}
|
41
|
+
|
42
|
+
This returns response['place_id'], which you can use to store and/or remove the place:
|
43
|
+
|
44
|
+
response = geoloqi.post "place/delete/#{response['place_id']}"
|
45
|
+
|
46
|
+
Which returns response['result'] with a value of "ok".
|
47
|
+
|
48
|
+
Implementing OAuth2
|
49
|
+
---
|
50
|
+
|
51
|
+
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.
|
52
|
+
|
53
|
+
Here is a simple Sinatra example implementing the OAuth2 flow with Geoloqi:
|
54
|
+
|
55
|
+
require 'rubygems'
|
56
|
+
require 'sinatra'
|
57
|
+
require 'geoloqi'
|
58
|
+
|
59
|
+
GEOLOQI_REDIRECT_URI = 'http://yourwebsite.net'
|
60
|
+
|
61
|
+
enable :sessions
|
62
|
+
|
63
|
+
def geoloqi
|
64
|
+
@geoloqi ||= Geoloqi::Session.new :auth => session[:geoloqi_auth],
|
65
|
+
:config => {:client_id => 'YOUR OAUTH CLIENT ID',
|
66
|
+
:client_secret => 'YOUR CLIENT SECRET'}
|
67
|
+
end
|
68
|
+
|
69
|
+
get '/?' do
|
70
|
+
session[:geoloqi_auth] = geoloqi.get_auth(params[:code], GEOLOQI_REDIRECT_URI) if params[:code] && !geoloqi.access_token?
|
71
|
+
redirect geoloqi.authorize_url(GEOLOQI_REDIRECT_URI) unless geoloqi.access_token?
|
72
|
+
username = geoloqi.get('account/username')['username']
|
73
|
+
"You have successfully logged in as #{username}!"
|
74
|
+
end
|
75
|
+
|
76
|
+
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):
|
77
|
+
|
78
|
+
# To install deps: gem install sinatra sinatra-synchrony geoloqi
|
79
|
+
# To run from command line: ruby sinatra_synchrony.rb -s thin
|
80
|
+
require 'rubygems'
|
81
|
+
require 'sinatra'
|
82
|
+
require 'sinatra/synchrony'
|
83
|
+
require 'geoloqi'
|
84
|
+
|
85
|
+
GEOLOQI_REDIRECT_URI = 'http://example.com'
|
86
|
+
|
87
|
+
enable :sessions
|
88
|
+
|
89
|
+
configure do
|
90
|
+
Geoloqi.config :client_id => 'YOUR OAUTH CLIENT ID', :client_secret => 'YOUR CLIENT SECRET', :adapter => :em_synchrony
|
91
|
+
end
|
92
|
+
|
93
|
+
def geoloqi
|
94
|
+
@geoloqi ||= Geoloqi::Session.new :auth => session[:geoloqi_auth]
|
95
|
+
end
|
96
|
+
|
97
|
+
get '/?' do
|
98
|
+
session[:geoloqi_auth] = geoloqi.get_auth(params[:code], GEOLOQI_REDIRECT_URI) if params[:code] && !geoloqi.access_token?
|
99
|
+
redirect geoloqi.authorize_url(GEOLOQI_REDIRECT_URI) unless geoloqi.access_token?
|
100
|
+
username = geoloqi.get('account/username')['username']
|
101
|
+
"You have successfully logged in as #{username}!"
|
102
|
+
end
|
data/examples/simple.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# Create a layer, create a place on the layer, and then delete the place and the layer.
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'geoloqi'
|
5
|
+
|
6
|
+
geoloqi = Geoloqi::Session.new :oauth_token => 'YOUR OAUTH TOKEN GOES HERE'
|
7
|
+
|
8
|
+
layer_id = geoloqi.post('layer/create', :name => 'Test Layer')['layer_id']
|
9
|
+
|
10
|
+
puts geoloqi.get("layer/info/#{layer_id}")
|
11
|
+
|
12
|
+
place_id = geoloqi.post('place/create', {
|
13
|
+
"layer_id" => layer_id,
|
14
|
+
"name" => "Munich on the Willamette",
|
15
|
+
"latitude" => "45.5037078163837",
|
16
|
+
"longitude" => "-122.622699737549",
|
17
|
+
"radius" => "3467.44",
|
18
|
+
"extra" => {
|
19
|
+
"description" => "Portland",
|
20
|
+
"url" => "http://en.wikipedia.org/wiki/Portland,_Oregon"
|
21
|
+
}
|
22
|
+
})['place_id']
|
23
|
+
|
24
|
+
puts geoloqi.post("place/delete/#{place_id}")
|
25
|
+
|
26
|
+
puts geoloqi.post("layer/delete/#{layer_id}")
|
data/examples/sinatra.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# A simple Sinatra example demonstrating OAuth2 implementation with Geoloqi
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'sinatra'
|
5
|
+
require 'geoloqi'
|
6
|
+
|
7
|
+
GEOLOQI_REDIRECT_URI = 'http://example.com'
|
8
|
+
|
9
|
+
enable :sessions
|
10
|
+
|
11
|
+
def geoloqi
|
12
|
+
Geoloqi.config :client_id => 'YOUR OAUTH CLIENT ID', :client_secret => 'YOUR CLIENT SECRET'
|
13
|
+
@geoloqi ||= Geoloqi::Session.new :auth => session[:geoloqi_auth]
|
14
|
+
end
|
15
|
+
|
16
|
+
get '/?' do
|
17
|
+
session[:geoloqi_auth] = geoloqi.get_auth(params[:code], GEOLOQI_REDIRECT_URI) if params[:code] && !geoloqi.access_token?
|
18
|
+
redirect geoloqi.authorize_url(GEOLOQI_REDIRECT_URI) unless geoloqi.access_token?
|
19
|
+
username = geoloqi.get('account/username')['username']
|
20
|
+
"You have successfully logged in as #{username}!"
|
21
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# A simple Sinatra example demonstrating OAuth2 implementation with Geoloqi
|
2
|
+
# This version of the example is powerful! It uses sinatra-synchrony to implement real concurrency with EventMachine.
|
3
|
+
# Your calls to the Geoloqi api will not block the app from serving other requests!
|
4
|
+
# Run this example with Thin (which uses EventMachine under the hood): ruby sinatra_synchrony.rb -s thin
|
5
|
+
# Works on anything that supports Thin (Rack, EY, Heroku, etc..)
|
6
|
+
|
7
|
+
require 'rubygems'
|
8
|
+
require 'sinatra'
|
9
|
+
require 'sinatra/synchrony'
|
10
|
+
require 'geoloqi'
|
11
|
+
|
12
|
+
GEOLOQI_REDIRECT_URI = 'http://example.com'
|
13
|
+
|
14
|
+
enable :sessions
|
15
|
+
|
16
|
+
configure do
|
17
|
+
Geoloqi.config :client_id => 'YOUR OAUTH CLIENT ID', :client_secret => 'YOUR CLIENT SECRET', :adapter => :em_synchrony
|
18
|
+
end
|
19
|
+
|
20
|
+
def geoloqi
|
21
|
+
@geoloqi ||= Geoloqi::Session.new :auth => session[:geoloqi_auth]
|
22
|
+
end
|
23
|
+
|
24
|
+
get '/?' do
|
25
|
+
session[:geoloqi_auth] = geoloqi.get_auth(params[:code], GEOLOQI_REDIRECT_URI) if params[:code] && !geoloqi.access_token?
|
26
|
+
redirect geoloqi.authorize_url(GEOLOQI_REDIRECT_URI) unless geoloqi.access_token?
|
27
|
+
username = geoloqi.get('account/username')['username']
|
28
|
+
"You have successfully logged in as #{username}!"
|
29
|
+
end
|
data/geoloqi.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require './lib/geoloqi/version.rb'
|
2
|
+
Gem::Specification.new do |s|
|
3
|
+
s.name = 'geoloqi'
|
4
|
+
s.version = Geoloqi::VERSION
|
5
|
+
s.authors = ['Kyle Drake', 'Aaron Parecki']
|
6
|
+
s.email = ['kyledrake@gmail.com', 'aaron@parecki.com']
|
7
|
+
s.homepage = 'https://github.com/kyledrake/geoloqi-ruby'
|
8
|
+
s.summary = 'Powerful, flexible, lightweight interface to the awesome Geoloqi platform API'
|
9
|
+
s.description = 'Powerful, flexible, lightweight interface to the awesome Geoloqi platform API! Uses Faraday, '+
|
10
|
+
'and can be used with Ruby 1.9 and EM-Synchrony for really fast, highly concurrent development.'
|
11
|
+
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
s.require_paths = %w[lib]
|
14
|
+
s.rubyforge_project = s.name
|
15
|
+
s.required_rubygems_version = '>= 1.3.4'
|
16
|
+
|
17
|
+
s.add_dependency 'json'
|
18
|
+
s.add_dependency 'faraday'
|
19
|
+
|
20
|
+
s.add_development_dependency 'wrong', '= 0.5.0'
|
21
|
+
s.add_development_dependency 'minitest'
|
22
|
+
s.add_development_dependency 'em-http-request'
|
23
|
+
s.add_development_dependency 'em-synchrony'
|
24
|
+
s.add_development_dependency 'webmock'
|
25
|
+
end
|
data/lib/geoloqi.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'faraday'
|
3
|
+
require 'logger'
|
4
|
+
require 'geoloqi/config'
|
5
|
+
require 'geoloqi/error'
|
6
|
+
require 'geoloqi/session'
|
7
|
+
require 'geoloqi/version'
|
8
|
+
|
9
|
+
module Geoloqi
|
10
|
+
API_VERSION = 1
|
11
|
+
API_URL = 'https://api.geoloqi.com'
|
12
|
+
OAUTH_URL = 'https://beta.geoloqi.com/oauth/authorize'
|
13
|
+
@@adapter = :net_http
|
14
|
+
@@enable_logging = false
|
15
|
+
@@config = nil
|
16
|
+
|
17
|
+
def self.config(opts=nil)
|
18
|
+
return @@config if opts.nil?
|
19
|
+
@@config = Config.new opts
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.authorize_url(client_id=nil, redirect_uri=nil)
|
23
|
+
raise "client_id required to authorize url. Pass with Geoloqi.config" unless client_id
|
24
|
+
"#{OAUTH_URL}?response_type=code&client_id=#{Rack::Utils.escape client_id}&redirect_uri=#{Rack::Utils.escape redirect_uri}"
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Geoloqi
|
2
|
+
class Config
|
3
|
+
attr_accessor :client_id, :client_secret, :adapter, :enable_logging
|
4
|
+
def initialize(opts={})
|
5
|
+
opts.each {|k,v| send("#{k}=", v)}
|
6
|
+
self.enable_logging ||= false
|
7
|
+
raise ArgumentError, 'enable_logging must be boolean' unless [true, false].include? self.enable_logging
|
8
|
+
end
|
9
|
+
|
10
|
+
def client_id?
|
11
|
+
!client_id.nil? && !client_id.empty?
|
12
|
+
end
|
13
|
+
|
14
|
+
def client_secret?
|
15
|
+
!client_secret.nil? && !client_secret.empty?
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
module Geoloqi
|
2
|
+
class Session
|
3
|
+
attr_reader :auth
|
4
|
+
attr_accessor :config
|
5
|
+
|
6
|
+
def initialize(opts={})
|
7
|
+
opts[:config] = Geoloqi::Config.new opts[:config] if opts[:config].is_a? Hash
|
8
|
+
@config = opts[:config] || (Geoloqi.config || Geoloqi::Config.new)
|
9
|
+
self.auth = opts[:auth] || {}
|
10
|
+
self.auth[:access_token] = opts[:access_token] if opts[:access_token]
|
11
|
+
|
12
|
+
@connection = Faraday.new(:url => API_URL) do |builder|
|
13
|
+
builder.response :logger if @config.enable_logging
|
14
|
+
builder.adapter @config.adapter || :net_http
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def auth=(hash)
|
19
|
+
@auth = hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
20
|
+
end
|
21
|
+
|
22
|
+
def access_token
|
23
|
+
@auth[:access_token]
|
24
|
+
end
|
25
|
+
|
26
|
+
def access_token?
|
27
|
+
!access_token.nil?
|
28
|
+
end
|
29
|
+
|
30
|
+
def authorize_url(redirect_uri)
|
31
|
+
Geoloqi.authorize_url @config.client_id, redirect_uri
|
32
|
+
end
|
33
|
+
|
34
|
+
def get(path)
|
35
|
+
run :get, path
|
36
|
+
end
|
37
|
+
|
38
|
+
def post(path, body=nil)
|
39
|
+
run :post, path, body
|
40
|
+
end
|
41
|
+
|
42
|
+
def run(meth, path, body=nil)
|
43
|
+
renew_access_token! if auth[:expires_at] && auth[:expires_at] <= Time.now
|
44
|
+
|
45
|
+
body = body.to_json if body.is_a? Hash
|
46
|
+
|
47
|
+
retry_attempt = 0
|
48
|
+
begin
|
49
|
+
response = @connection.send(meth) do |req|
|
50
|
+
req.url "/#{API_VERSION.to_s}/#{path.gsub(/^\//, '')}"
|
51
|
+
req.headers = headers
|
52
|
+
req.body = body if body
|
53
|
+
end
|
54
|
+
|
55
|
+
json = JSON.parse response.body
|
56
|
+
raise ApiError.new(json['error'], json['error_description']) if json.is_a?(Hash) && json['error']
|
57
|
+
rescue Geoloqi::ApiError
|
58
|
+
raise Error.new('Unable to procure fresh access token from API on second attempt') if retry_attempt > 0
|
59
|
+
if json['error'] == 'expired_token'
|
60
|
+
retry_attempt += 1
|
61
|
+
retry
|
62
|
+
else
|
63
|
+
fail
|
64
|
+
end
|
65
|
+
end
|
66
|
+
json
|
67
|
+
end
|
68
|
+
|
69
|
+
# TODO: DRY and refactor
|
70
|
+
def renew_access_token!
|
71
|
+
require 'client_id and client_secret are required to get access token' unless @config.client_id? && @config.client_secret?
|
72
|
+
args = {:client_id => @config.client_id,
|
73
|
+
:client_secret => @config.client_secret,
|
74
|
+
:grant_type => 'refresh_token',
|
75
|
+
:refresh_token => auth[:refresh_token]}
|
76
|
+
|
77
|
+
response = @connection.post do |req|
|
78
|
+
req.url "/#{API_VERSION.to_s}/oauth/token"
|
79
|
+
req.headers = headers false
|
80
|
+
req.body = args.to_json
|
81
|
+
end
|
82
|
+
|
83
|
+
auth = JSON.parse response.body
|
84
|
+
|
85
|
+
# expires_at is likely incorrect. I'm chopping 5 seconds
|
86
|
+
# off to allow for a more graceful failover.
|
87
|
+
auth['expires_at'] = (Time.now + @expires_in.to_i)-5
|
88
|
+
|
89
|
+
self.auth = JSON.parse response.body
|
90
|
+
self.auth
|
91
|
+
end
|
92
|
+
|
93
|
+
def get_auth(code, redirect_uri)
|
94
|
+
require 'client_id and client_secret are required to get access token' unless @config.client_id? && @config.client_secret?
|
95
|
+
args = {:client_id => @config.client_id,
|
96
|
+
:client_secret => @config.client_secret,
|
97
|
+
:code => code,
|
98
|
+
:grant_type => 'authorization_code',
|
99
|
+
:redirect_uri => redirect_uri}
|
100
|
+
|
101
|
+
response = @connection.post do |req|
|
102
|
+
req.url "/#{API_VERSION.to_s}/oauth/token"
|
103
|
+
req.headers = headers false
|
104
|
+
req.body = args.to_json
|
105
|
+
end
|
106
|
+
|
107
|
+
auth = JSON.parse response.body
|
108
|
+
|
109
|
+
# expires_at is likely incorrect. I'm chopping 5 seconds
|
110
|
+
# off to allow for a more graceful failover.
|
111
|
+
auth['expires_at'] = (Time.now + @expires_in.to_i)-5
|
112
|
+
|
113
|
+
self.auth = JSON.parse response.body
|
114
|
+
self.auth
|
115
|
+
end
|
116
|
+
|
117
|
+
def headers(with_oauth=true)
|
118
|
+
headers = {'Content-Type' => 'application/json', 'User-Agent' => "geoloqi-ruby #{Geoloqi::VERSION}", 'Accept' => 'application/json'}
|
119
|
+
headers['Authorization'] = "OAuth #{access_token}" if with_oauth
|
120
|
+
headers
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,198 @@
|
|
1
|
+
raise ArgumentError, 'usage: be ruby spec/geoloqi_spec.rb "client_id" "client_secret" "access_token"' unless ARGV.length == 3
|
2
|
+
Bundler.setup
|
3
|
+
require './lib/geoloqi.rb'
|
4
|
+
require 'minitest/autorun'
|
5
|
+
require 'wrong'
|
6
|
+
require 'wrong/adapters/minitest'
|
7
|
+
require 'webmock'
|
8
|
+
|
9
|
+
Wrong.config.alias_assert :expect
|
10
|
+
|
11
|
+
describe Geoloqi do
|
12
|
+
it 'reads geoloqi config' do
|
13
|
+
Geoloqi.config :client_id => 'client_id', :client_secret => 'client_secret'
|
14
|
+
expect { Geoloqi.config.is_a?(Geoloqi::Config) }
|
15
|
+
expect { Geoloqi.config.client_id == 'client_id' }
|
16
|
+
expect { Geoloqi.config.client_secret == 'client_secret' }
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'returns authorize url' do
|
20
|
+
authorize_url = Geoloqi.authorize_url 'test', 'http://blah.blah/test'
|
21
|
+
expect { authorize_url == "#{Geoloqi::OAUTH_URL}?"+
|
22
|
+
'response_type=code&'+
|
23
|
+
"client_id=#{Rack::Utils.escape 'test'}&"+
|
24
|
+
"redirect_uri=#{Rack::Utils.escape 'http://blah.blah/test'}" }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe Geoloqi::Config do
|
29
|
+
it 'throws exception if non-boolean value is fed to logging' do
|
30
|
+
expect { rescuing { Geoloqi.config(:client_id => '', :client_secret => '', :enable_logging => :cats )}.class == ArgumentError }
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'correctly checks booleans for client_id and client_secret' do
|
34
|
+
[:client_id, :client_secret].each do |k|
|
35
|
+
expect { Geoloqi.config(k => '').send("#{k}?") == false }
|
36
|
+
expect { Geoloqi.config(k => nil).send("#{k}?") == false }
|
37
|
+
expect { Geoloqi.config(k => 'lol').send("#{k}?") == true }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe Geoloqi::Session do
|
43
|
+
describe 'with nothing passed' do
|
44
|
+
before do
|
45
|
+
@session = Geoloqi::Session.new
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should not find access token' do
|
49
|
+
expect { !@session.access_token? }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe 'with access token and no config' do
|
54
|
+
before do
|
55
|
+
@session = Geoloqi::Session.new :access_token => ARGV[2]
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'successfully makes call to api with forward slash' do
|
59
|
+
response = @session.get '/layer/info/Gx'
|
60
|
+
expect { response['layer_id'] == 'Gx' }
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'successfully makes call to api without forward slash' do
|
64
|
+
response = @session.get '/layer/info/Gx'
|
65
|
+
expect { response['layer_id'] == 'Gx' }
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'creates a layer, reads its info, and then deletes the layer' do
|
69
|
+
layer_id = @session.post('/layer/create', :name => 'Test Layer')['layer_id']
|
70
|
+
layer_info = @session.get "/layer/info/#{layer_id}"
|
71
|
+
layer_delete = @session.post "/layer/delete/#{layer_id}"
|
72
|
+
|
73
|
+
expect { layer_id.is_a?(String) }
|
74
|
+
expect { !layer_id.empty? }
|
75
|
+
expect { layer_info['name'] == 'Test Layer' }
|
76
|
+
expect { layer_delete['result'] == 'ok' }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe 'with oauth id, secret, and access token via Geoloqi::Config' do
|
81
|
+
it 'should load config' do
|
82
|
+
@session = Geoloqi::Session.new :access_token => ARGV[2], :config => Geoloqi::Config.new(:client_id => ARGV[0],
|
83
|
+
:client_secret => ARGV[1])
|
84
|
+
expect { @session.config.client_id == ARGV[0] }
|
85
|
+
expect { @session.config.client_secret == ARGV[1] }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe 'with em synchrony adapter and access token' do
|
90
|
+
it 'makes call to api' do
|
91
|
+
session = Geoloqi::Session.new :access_token => ARGV[2], :config => {:adapter => :em_synchrony}
|
92
|
+
response = session.get 'layer/info/Gx'
|
93
|
+
expect { response['layer_id'] == 'Gx' }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe 'with client id, client secret, and access token via direct hash' do
|
98
|
+
before do
|
99
|
+
@session = Geoloqi::Session.new :access_token => ARGV[2], :config => {:client_id => ARGV[0], :client_secret => ARGV[1]}
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'should return access token' do
|
103
|
+
expect { @session.access_token == ARGV[2] }
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'should recognize access token exists' do
|
107
|
+
expect { @session.access_token? }
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'gets authorize url' do
|
111
|
+
authorize_url = @session.authorize_url('http://blah.blah/test')
|
112
|
+
expect { authorize_url == "#{Geoloqi::OAUTH_URL}?"+
|
113
|
+
"response_type=code&"+
|
114
|
+
"client_id=#{Rack::Utils.escape ARGV[0]}&"+
|
115
|
+
"redirect_uri=#{Rack::Utils.escape 'http://blah.blah/test'}" }
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe 'with bunk access token' do
|
120
|
+
before do
|
121
|
+
@session = Geoloqi::Session.new :access_token => 'hey brah whats up let me in its cool 8)'
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'fails with an exception' do
|
125
|
+
expect { rescuing { @session.get 'message/send' }.message == 'invalid_token' }
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe 'with config' do
|
130
|
+
before do
|
131
|
+
@session = Geoloqi::Session.new :config => {:client_id => ARGV[0], :client_secret => ARGV[1]}
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'retrieves auth with mock' do
|
135
|
+
WebMock.disable_net_connect!
|
136
|
+
begin
|
137
|
+
response = @session.get_auth('1234', 'http://test.site/')
|
138
|
+
ensure
|
139
|
+
WebMock.allow_net_connect!
|
140
|
+
end
|
141
|
+
expect { response == {:access_token => 'access_token1234',
|
142
|
+
:scope => nil,
|
143
|
+
:expires_in => '86400',
|
144
|
+
:refresh_token => 'refresh_token1234'} }
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
describe 'with config and expired auth' do
|
149
|
+
before do
|
150
|
+
@session = Geoloqi::Session.new :config => {:client_id => ARGV[0], :client_secret => ARGV[1]},
|
151
|
+
:auth => { :access_token => 'access_token1234',
|
152
|
+
:scope => nil,
|
153
|
+
:expires_in => '86400',
|
154
|
+
:expires_at => Time.at(0),
|
155
|
+
:refresh_token => 'refresh_token1234' }
|
156
|
+
end
|
157
|
+
|
158
|
+
it 'retrieves new access token and retries query if expired' do
|
159
|
+
WebMock.disable_net_connect!
|
160
|
+
begin
|
161
|
+
@session.get('account/username')
|
162
|
+
ensure
|
163
|
+
WebMock.allow_net_connect!
|
164
|
+
end
|
165
|
+
expect { @session.auth[:access_token] == 'access_token4567' }
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
include WebMock::API
|
171
|
+
|
172
|
+
stub_request(:post, "https://api.geoloqi.com/1/oauth/token").
|
173
|
+
with(:body => {:client_id => ARGV[0],
|
174
|
+
:client_secret => ARGV[1],
|
175
|
+
:code => "1234",
|
176
|
+
:grant_type => "authorization_code",
|
177
|
+
:redirect_uri => "http://test.site/"}.to_json).
|
178
|
+
to_return(:status => 200,
|
179
|
+
:body => {:access_token => 'access_token1234',
|
180
|
+
:scope => nil,
|
181
|
+
:expires_in => '86400',
|
182
|
+
:refresh_token => 'refresh_token1234'}.to_json)
|
183
|
+
|
184
|
+
stub_request(:post, "https://api.geoloqi.com/1/oauth/token").
|
185
|
+
with(:body => {:client_id => ARGV[0],
|
186
|
+
:client_secret => ARGV[1],
|
187
|
+
:grant_type => "refresh_token",
|
188
|
+
:refresh_token => "refresh_token1234"}.to_json).
|
189
|
+
to_return(:status => 200,
|
190
|
+
:body => {:access_token => 'access_token4567',
|
191
|
+
:scope => nil,
|
192
|
+
:expires_in => '5000',
|
193
|
+
:refresh_token => 'refresh_token4567'}.to_json)
|
194
|
+
|
195
|
+
stub_request(:get, "https://api.geoloqi.com/1/account/username").
|
196
|
+
with(:headers => {'Authorization'=>'OAuth access_token4567'}).
|
197
|
+
to_return(:status => 200,
|
198
|
+
:body => {'username' => 'pikachu4lyfe'}.to_json)
|
metadata
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geoloqi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.9.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kyle Drake
|
9
|
+
- Aaron Parecki
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
|
14
|
+
date: 2011-06-05 00:00:00 -07:00
|
15
|
+
default_executable:
|
16
|
+
dependencies:
|
17
|
+
- !ruby/object:Gem::Dependency
|
18
|
+
name: json
|
19
|
+
prerelease: false
|
20
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
21
|
+
none: false
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: "0"
|
26
|
+
type: :runtime
|
27
|
+
version_requirements: *id001
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: faraday
|
30
|
+
prerelease: false
|
31
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
32
|
+
none: false
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: "0"
|
37
|
+
type: :runtime
|
38
|
+
version_requirements: *id002
|
39
|
+
- !ruby/object:Gem::Dependency
|
40
|
+
name: wrong
|
41
|
+
prerelease: false
|
42
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - "="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.5.0
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id003
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: minitest
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
type: :development
|
60
|
+
version_requirements: *id004
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: em-http-request
|
63
|
+
prerelease: false
|
64
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
type: :development
|
71
|
+
version_requirements: *id005
|
72
|
+
- !ruby/object:Gem::Dependency
|
73
|
+
name: em-synchrony
|
74
|
+
prerelease: false
|
75
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: "0"
|
81
|
+
type: :development
|
82
|
+
version_requirements: *id006
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: webmock
|
85
|
+
prerelease: false
|
86
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: "0"
|
92
|
+
type: :development
|
93
|
+
version_requirements: *id007
|
94
|
+
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
|
+
email:
|
96
|
+
- kyledrake@gmail.com
|
97
|
+
- aaron@parecki.com
|
98
|
+
executables: []
|
99
|
+
|
100
|
+
extensions: []
|
101
|
+
|
102
|
+
extra_rdoc_files: []
|
103
|
+
|
104
|
+
files:
|
105
|
+
- .gitignore
|
106
|
+
- Gemfile
|
107
|
+
- README.markdown
|
108
|
+
- examples/simple.rb
|
109
|
+
- examples/sinatra.rb
|
110
|
+
- examples/sinatra_synchrony.rb
|
111
|
+
- geoloqi.gemspec
|
112
|
+
- lib/geoloqi.rb
|
113
|
+
- lib/geoloqi/config.rb
|
114
|
+
- lib/geoloqi/error.rb
|
115
|
+
- lib/geoloqi/session.rb
|
116
|
+
- lib/geoloqi/version.rb
|
117
|
+
- spec/geoloqi_spec.rb
|
118
|
+
has_rdoc: true
|
119
|
+
homepage: https://github.com/kyledrake/geoloqi-ruby
|
120
|
+
licenses: []
|
121
|
+
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: "0"
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
none: false
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 1.3.4
|
139
|
+
requirements: []
|
140
|
+
|
141
|
+
rubyforge_project: geoloqi
|
142
|
+
rubygems_version: 1.6.2
|
143
|
+
signing_key:
|
144
|
+
specification_version: 3
|
145
|
+
summary: Powerful, flexible, lightweight interface to the awesome Geoloqi platform API
|
146
|
+
test_files: []
|
147
|
+
|