geoloqi 0.9.36 → 0.9.37
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/README.md +19 -13
- data/examples/simple.rb +5 -5
- data/examples/sinatra_synchrony.rb +3 -1
- data/lib/geoloqi.rb +1 -0
- data/lib/geoloqi/batch.rb +44 -0
- data/lib/geoloqi/config.rb +1 -1
- data/lib/geoloqi/session.rb +16 -0
- data/lib/geoloqi/version.rb +1 -1
- data/spec/geoloqi/session_spec.rb +34 -0
- metadata +4 -3
data/README.md
CHANGED
@@ -31,22 +31,28 @@ If you're using Geoloqi with OAuth or making multiple requests, we recommend usi
|
|
31
31
|
|
32
32
|
Which returns a hash with the following:
|
33
33
|
|
34
|
-
{
|
35
|
-
|
36
|
-
|
37
|
-
|
34
|
+
{:layer_id => "Gx",
|
35
|
+
:user_id => "4",
|
36
|
+
:type => "normal",
|
37
|
+
:name => "USGS Earthquakes",
|
38
|
+
:description => "Real-time notifications of earthquakes near you.",
|
39
|
+
:icon => "http://beta.geoloqi.com/images/earthquake-layer.png",
|
40
|
+
:public => "1",
|
41
|
+
:url => "https://a.geoloqi.com/layer/description/Gx",
|
42
|
+
:subscription => false,
|
43
|
+
:settings => false}
|
38
44
|
|
39
45
|
Both GET and POST are supported. To send a POST to create a place (in this case, the entire city of Portland, Oregon):
|
40
46
|
|
41
47
|
response = geoloqi.post 'place/create', {
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
48
|
+
:layer_id => "1Wn",
|
49
|
+
:name => "3772756364",
|
50
|
+
:latitude => "45.5037078163837",
|
51
|
+
:longitude => "-122.622699737549",
|
52
|
+
:radius => "3467.44",
|
53
|
+
:extra => {
|
54
|
+
:description => "Portland",
|
55
|
+
:url => "http://en.wikipedia.org/wiki/Portland"
|
50
56
|
}
|
51
57
|
}
|
52
58
|
|
@@ -88,7 +94,7 @@ Implementing OAuth2 is not difficult, because we've done all the hard work for y
|
|
88
94
|
end
|
89
95
|
|
90
96
|
get '/?' do
|
91
|
-
username = geoloqi.get('account/username')[
|
97
|
+
username = geoloqi.get('account/username')[:username]
|
92
98
|
"You have successfully logged in as #{username}!"
|
93
99
|
end
|
94
100
|
|
data/examples/simple.rb
CHANGED
@@ -10,12 +10,12 @@ layer_id = geoloqi.post('layer/create', :name => 'Test Layer')[:layer_id]
|
|
10
10
|
puts geoloqi.get("layer/info/#{layer_id}")
|
11
11
|
|
12
12
|
place_id = geoloqi.post('place/create', {
|
13
|
-
:layer_id
|
14
|
-
:name
|
15
|
-
:latitude
|
13
|
+
:layer_id => layer_id,
|
14
|
+
:name => "Munich on the Willamette",
|
15
|
+
:latitude => "45.5037078163837",
|
16
16
|
:longitude => "-122.622699737549",
|
17
|
-
:radius
|
18
|
-
:extra
|
17
|
+
:radius => "3467.44",
|
18
|
+
:extra => {
|
19
19
|
:description => "Portland",
|
20
20
|
:url => "http://en.wikipedia.org/wiki/Portland,_Oregon"
|
21
21
|
}
|
@@ -1,9 +1,11 @@
|
|
1
1
|
# A simple Sinatra example demonstrating OAuth2 implementation with Geoloqi
|
2
2
|
|
3
|
-
# This version of the example uses sinatra-synchrony to implement
|
3
|
+
# This version of the example uses sinatra-synchrony to implement an EventMachine-based app.
|
4
4
|
# Run this example with Thin (which uses EventMachine under the hood): ruby sinatra_synchrony.rb -s thin
|
5
5
|
# Works on anything that supports Thin (Rack, EY, Heroku, etc..)
|
6
6
|
# To install deps: gem install sinatra sinatra-synchrony geoloqi
|
7
|
+
#
|
8
|
+
# More information: http://kyledrake.net/sinatra-synchrony
|
7
9
|
|
8
10
|
require 'sinatra'
|
9
11
|
require 'sinatra/geoloqi'
|
data/lib/geoloqi.rb
CHANGED
@@ -0,0 +1,44 @@
|
|
1
|
+
module Geoloqi
|
2
|
+
# This class is a builder/DSL class used to construct batch queries against the Geoloqi API. The best way to use this is
|
3
|
+
# to use it from the Session class.
|
4
|
+
# @see Session#batch
|
5
|
+
class Batch
|
6
|
+
# This keeps the batcher from going overboard.
|
7
|
+
PER_REQUEST_LIMIT = 200
|
8
|
+
|
9
|
+
class NotImplementedError < StandardError; end
|
10
|
+
|
11
|
+
def initialize(session, &block)
|
12
|
+
@jobs = []
|
13
|
+
@session = session
|
14
|
+
self.instance_eval(&block)
|
15
|
+
end
|
16
|
+
|
17
|
+
def post(path, query=nil, headers={})
|
18
|
+
@jobs << {
|
19
|
+
:relative_url => path,
|
20
|
+
:body => query,
|
21
|
+
:headers => headers
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
def get(path, query=nil, headers={})
|
26
|
+
raise NotImplementedError, 'get requests are not yet implemented in batch'
|
27
|
+
end
|
28
|
+
|
29
|
+
def run!
|
30
|
+
results = []
|
31
|
+
|
32
|
+
until @jobs.empty?
|
33
|
+
queued_jobs = @jobs.slice! 0, PER_REQUEST_LIMIT
|
34
|
+
|
35
|
+
results << @session.post('batch/run', {
|
36
|
+
:access_token => @session.access_token,
|
37
|
+
:batch => queued_jobs
|
38
|
+
})[:result]
|
39
|
+
end
|
40
|
+
|
41
|
+
results.flatten!
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/geoloqi/config.rb
CHANGED
@@ -46,7 +46,7 @@ module Geoloqi
|
|
46
46
|
#
|
47
47
|
# @return [Boolean]
|
48
48
|
# @example
|
49
|
-
# Geoloqi.config
|
49
|
+
# Geoloqi.config.throw_exceptions = false
|
50
50
|
attr_accessor :throw_exceptions
|
51
51
|
|
52
52
|
# Use dynamic error class names, which inherit from Geoloqi::ApiError. This may be deprecated in a future release.
|
data/lib/geoloqi/session.rb
CHANGED
@@ -178,6 +178,22 @@ module Geoloqi
|
|
178
178
|
Response.new raw.status, raw.headers, raw.body
|
179
179
|
end
|
180
180
|
|
181
|
+
# Make a batch request to the Geoloqi API. Compiles POST requests (provided in a block) and returns an array of the results,
|
182
|
+
# which includes the response code, headers, and body. Calls are made synchronously on the server, and the results are returned in
|
183
|
+
# the same order. This should be much faster for scenarios where inserting/updating hundreds or thousands of records is needed.
|
184
|
+
# @return [Array] - An array of Hash objects containing the response code, headers, and body.
|
185
|
+
# @see Batch
|
186
|
+
# @example
|
187
|
+
# # Create 3 layers at once, return responses in an array
|
188
|
+
# responses_array = geoloqi_session.batch do
|
189
|
+
# post 'layer/create', :name => 'Layer 1'
|
190
|
+
# post 'layer/create', :name => 'Layer 2'
|
191
|
+
# post 'layer/create', :name => 'Layer 3'
|
192
|
+
# end
|
193
|
+
def batch(&block)
|
194
|
+
Batch.new(self, &block).run!
|
195
|
+
end
|
196
|
+
|
181
197
|
# Used to retrieve the access token from the Geoloqi OAuth2 server. This is fairly low level and you shouldn't need to use it directly.
|
182
198
|
#
|
183
199
|
# @return [Hash] - The auth hash used to persist the session object.
|
data/lib/geoloqi/version.rb
CHANGED
@@ -81,6 +81,40 @@ describe Geoloqi::Session do
|
|
81
81
|
@session = Geoloqi::Session.new :access_token => ACCESS_TOKEN
|
82
82
|
end
|
83
83
|
|
84
|
+
it 'successfully makes a batch request' do
|
85
|
+
stub_request(:post, api_url('batch/run')).
|
86
|
+
with(:body => {
|
87
|
+
:access_token => 'access_token1234',
|
88
|
+
:batch => [
|
89
|
+
{:relative_url => '/layer/create', :body => {:name => 'Test 1'}, :headers => {}},
|
90
|
+
{:relative_url => '/layer/create', :body => {:name => 'Test 2'}, :headers => {}}
|
91
|
+
]
|
92
|
+
}.to_json,
|
93
|
+
:headers => {
|
94
|
+
'Authorization' => 'OAuth access_token1234',
|
95
|
+
'Content-Type' => 'application/json'}).
|
96
|
+
to_return(:status => 200, :body => {:result => [
|
97
|
+
{:code => 201,
|
98
|
+
:headers => [{:name => "Date", :value => "Wed, 08 Feb 2012 02:16:11 GMT"}],
|
99
|
+
:body => {:layer_id => "abc", :name => "Test 1"},
|
100
|
+
:time_ms => 11.373
|
101
|
+
},
|
102
|
+
{:code => 201,
|
103
|
+
:headers => [{:name => "Date", :value => "Wed, 08 Feb 2012 02:16:11 GMT"}],
|
104
|
+
:body => { :layer_id => "def", :name => "Test 2" },
|
105
|
+
:time_ms => 10.735
|
106
|
+
}]}.to_json)
|
107
|
+
|
108
|
+
response = @session.batch do
|
109
|
+
post '/layer/create', :name => 'Test 1'
|
110
|
+
post '/layer/create', :name => 'Test 2'
|
111
|
+
end
|
112
|
+
|
113
|
+
expect { response.first[:code] == 201 }
|
114
|
+
expect { response.first[:body][:layer_id] == 'abc' }
|
115
|
+
expect { response.last[:body][:layer_id] == 'def' }
|
116
|
+
end
|
117
|
+
|
84
118
|
it 'throws an exception on a hard request error' do
|
85
119
|
stub_request(:get, api_url('crashing_method')).
|
86
120
|
with(:headers => auth_headers).
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 9
|
8
|
-
-
|
9
|
-
version: 0.9.
|
8
|
+
- 37
|
9
|
+
version: 0.9.37
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Kyle Drake
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-02-08 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -147,6 +147,7 @@ files:
|
|
147
147
|
- examples/sinatra_synchrony.rb
|
148
148
|
- geoloqi.gemspec
|
149
149
|
- lib/geoloqi.rb
|
150
|
+
- lib/geoloqi/batch.rb
|
150
151
|
- lib/geoloqi/config.rb
|
151
152
|
- lib/geoloqi/data/ca-certificates.crt
|
152
153
|
- lib/geoloqi/error.rb
|