grape-batch 2.2.2 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +5 -5
- data/lib/grape/batch.rb +1 -69
- data/lib/grape/batch/base.rb +60 -0
- data/lib/grape/batch/configuration.rb +1 -1
- data/lib/grape/batch/version.rb +1 -1
- data/spec/api.rb +2 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 583bc57ae53b64c0cc53821e877304bb47cd9b35
|
4
|
+
data.tar.gz: 14017133fe2585ee354c727be4b5489ba9fae277
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 77f43acc1c9b95c13254cdc4bc2b8f434a1ddf3988e6e3c1e824680a2ceb04bac86dbebdfc36ff0ade1dd9789a97636429cbb2d9c4b7477d0dc556a70ea38b90
|
7
|
+
data.tar.gz: 0dcb7894c03a989116613554104c6c4e091fe1602b97610ccd9add986a9f487fd39431936b79aaf784a9a31e0cf493af5f153b3c9cf24b5093eea04473516e5b
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
# 2.3.0 (22nd March 2016)
|
2
|
+
* Remove request session and token headers from response headers
|
3
|
+
* Code refactor
|
4
|
+
|
1
5
|
# 2.2.2 (22nd March 2016)
|
2
6
|
* SESSION_HEADER is no longer duplicated because when the header contains an ActiveRecord object or something similar, it changes the object id
|
3
7
|
|
data/README.md
CHANGED
@@ -136,13 +136,13 @@ module Twitter
|
|
136
136
|
resource :session do
|
137
137
|
get do
|
138
138
|
# route logic
|
139
|
-
|
139
|
+
request.env['HTTP_X_API_TOKEN'] = 'my_fresh_token'
|
140
140
|
# some other logic
|
141
141
|
end
|
142
142
|
end
|
143
143
|
end
|
144
144
|
|
145
|
-
# This route will
|
145
|
+
# This route will register a token. The header will be kept and passed to the following requests by Grape::Batch.
|
146
146
|
```
|
147
147
|
|
148
148
|
```ruby
|
@@ -156,14 +156,14 @@ module Twitter
|
|
156
156
|
|
157
157
|
resource :session do
|
158
158
|
get do
|
159
|
-
# route logic
|
160
|
-
|
159
|
+
# route logic
|
160
|
+
request.env['api.session'] = OpenStruct(id: '123456')
|
161
161
|
# some other logic
|
162
162
|
end
|
163
163
|
end
|
164
164
|
end
|
165
165
|
|
166
|
-
# This route will return a session object. The
|
166
|
+
# This route will return a session object. The session object will be kept and passed to the following requests by Grape::Batch.
|
167
167
|
```
|
168
168
|
|
169
169
|
## Contributing
|
data/lib/grape/batch.rb
CHANGED
@@ -5,79 +5,11 @@ require 'grape/batch/request'
|
|
5
5
|
require 'grape/batch/response'
|
6
6
|
require 'grape/batch/validator'
|
7
7
|
require 'grape/batch/version'
|
8
|
+
require 'grape/batch/base'
|
8
9
|
require 'multi_json'
|
9
10
|
|
10
11
|
module Grape
|
11
12
|
module Batch
|
12
|
-
# Gem main class
|
13
|
-
class Base
|
14
|
-
SESSION_HEADER = 'api.session'.freeze
|
15
|
-
TOKEN_HEADER = 'HTTP_X_API_TOKEN'.freeze
|
16
13
|
|
17
|
-
def initialize(app)
|
18
|
-
@app = app
|
19
|
-
@response_klass = Grape::Batch.configuration.formatter
|
20
|
-
@batch_size_limit = Grape::Batch.configuration.limit
|
21
|
-
@api_path = Grape::Batch.configuration.path
|
22
|
-
@session_proc = Grape::Batch.configuration.session_proc
|
23
|
-
@logger = Grape::Batch::Logger.new
|
24
|
-
end
|
25
|
-
|
26
|
-
def call(env)
|
27
|
-
return @app.call(env) unless batch_request?(env)
|
28
|
-
@logger.prepare(env).batch_begin
|
29
|
-
batch_call(env)
|
30
|
-
end
|
31
|
-
|
32
|
-
def batch_call(env)
|
33
|
-
begin
|
34
|
-
status = 200
|
35
|
-
batch_requests = Grape::Batch::Validator.parse(env, @batch_size_limit)
|
36
|
-
body = MultiJson.encode(dispatch(env, batch_requests))
|
37
|
-
rescue Grape::Batch::RequestBodyError, Grape::Batch::TooManyRequestsError => e
|
38
|
-
e.class == TooManyRequestsError ? status = 429 : status = 400
|
39
|
-
body = e.message
|
40
|
-
end
|
41
|
-
|
42
|
-
@logger.batch_end
|
43
|
-
Rack::Response.new(body, status, 'Content-Type' => 'application/json')
|
44
|
-
end
|
45
|
-
|
46
|
-
private
|
47
|
-
|
48
|
-
def batch_request?(env)
|
49
|
-
env['PATH_INFO'].start_with?(@api_path) &&
|
50
|
-
env['REQUEST_METHOD'] == 'POST' &&
|
51
|
-
env['CONTENT_TYPE'] == 'application/json'
|
52
|
-
end
|
53
|
-
|
54
|
-
def dispatch(env, batch_requests)
|
55
|
-
# Prepare batch request env
|
56
|
-
@request_env = env.dup
|
57
|
-
# Call session proc
|
58
|
-
@request_env[SESSION_HEADER] = @session_proc.call(@request_env)
|
59
|
-
|
60
|
-
# Call batch request
|
61
|
-
batch_requests.map do |batch_request|
|
62
|
-
batch_env = Grape::Batch::Request.new(@request_env, batch_request).build
|
63
|
-
status, headers, response = @app.call(batch_env)
|
64
|
-
|
65
|
-
update_request_env_session_from_headers(headers)
|
66
|
-
update_request_env_token_from_headers(headers)
|
67
|
-
|
68
|
-
@response_klass.format(status, headers, response)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def update_request_env_session_from_headers(headers)
|
73
|
-
return if !headers[SESSION_HEADER] || @request_env[SESSION_HEADER]
|
74
|
-
@request_env[SESSION_HEADER] = headers[SESSION_HEADER]
|
75
|
-
end
|
76
|
-
|
77
|
-
def update_request_env_token_from_headers(headers)
|
78
|
-
return if !headers[TOKEN_HEADER] || @request_env[TOKEN_HEADER]
|
79
|
-
@request_env[TOKEN_HEADER] = headers[TOKEN_HEADER].dup
|
80
|
-
end
|
81
|
-
end
|
82
14
|
end
|
83
15
|
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Grape
|
2
|
+
module Batch
|
3
|
+
class Base
|
4
|
+
SESSION_HEADER = 'api.session'.freeze
|
5
|
+
TOKEN_HEADER = 'HTTP_X_API_TOKEN'.freeze
|
6
|
+
|
7
|
+
def initialize(app)
|
8
|
+
@app = app
|
9
|
+
@logger = Grape::Batch::Logger.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(env)
|
13
|
+
return @app.call(env) unless batch_request?(env)
|
14
|
+
|
15
|
+
# Handle batch requests
|
16
|
+
@logger.prepare(env).batch_begin
|
17
|
+
body, status = batch_call(env)
|
18
|
+
@logger.batch_end
|
19
|
+
|
20
|
+
# Return Rack formatted response
|
21
|
+
Rack::Response.new(body, status, 'Content-Type' => 'application/json')
|
22
|
+
end
|
23
|
+
|
24
|
+
def batch_call(env)
|
25
|
+
batch_requests = Grape::Batch::Validator.parse(env, Grape::Batch.configuration.limit)
|
26
|
+
[MultiJson.encode(dispatch(env, batch_requests)), 200]
|
27
|
+
|
28
|
+
rescue Grape::Batch::RequestBodyError, Grape::Batch::TooManyRequestsError => e
|
29
|
+
[e.message, e.class == TooManyRequestsError ? 429 : 400]
|
30
|
+
end
|
31
|
+
|
32
|
+
def dispatch(env, batch_requests)
|
33
|
+
call_api_session_proc(env)
|
34
|
+
|
35
|
+
# Call batch request
|
36
|
+
batch_requests.map do |batch_request|
|
37
|
+
batch_env = Grape::Batch::Request.new(env, batch_request).build
|
38
|
+
call_batched_request(batch_env)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def call_batched_request(env)
|
43
|
+
status, headers, response = @app.call(env)
|
44
|
+
Grape::Batch.configuration.formatter.format(status, headers, response)
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def batch_request?(env)
|
50
|
+
env['PATH_INFO'].start_with?(Grape::Batch.configuration.path) &&
|
51
|
+
env['REQUEST_METHOD'] == 'POST' && env['CONTENT_TYPE'] == 'application/json'
|
52
|
+
end
|
53
|
+
|
54
|
+
def call_api_session_proc(env)
|
55
|
+
return unless Grape::Batch.configuration.session_proc
|
56
|
+
env[SESSION_HEADER] = Grape::Batch.configuration.session_proc.call(env)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/grape/batch/version.rb
CHANGED
data/spec/api.rb
CHANGED
@@ -59,7 +59,7 @@ module Twitter
|
|
59
59
|
|
60
60
|
resource :login do
|
61
61
|
get do
|
62
|
-
|
62
|
+
request.env['HTTP_X_API_TOKEN'] = 'user_token'
|
63
63
|
|
64
64
|
'login successful'
|
65
65
|
end
|
@@ -75,7 +75,7 @@ module Twitter
|
|
75
75
|
|
76
76
|
resource :session do
|
77
77
|
get do
|
78
|
-
|
78
|
+
request.env['api.session'] = OpenStruct.new(nick: 'Bob')
|
79
79
|
|
80
80
|
'session reloaded'
|
81
81
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-batch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lionel Oto
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2016-
|
13
|
+
date: 2016-05-12 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: multi_json
|
@@ -146,6 +146,7 @@ files:
|
|
146
146
|
- Rakefile
|
147
147
|
- grape-batch.gemspec
|
148
148
|
- lib/grape/batch.rb
|
149
|
+
- lib/grape/batch/base.rb
|
149
150
|
- lib/grape/batch/configuration.rb
|
150
151
|
- lib/grape/batch/converter.rb
|
151
152
|
- lib/grape/batch/errors.rb
|
@@ -177,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
177
178
|
version: '0'
|
178
179
|
requirements: []
|
179
180
|
rubyforge_project:
|
180
|
-
rubygems_version: 2.4.
|
181
|
+
rubygems_version: 2.4.5
|
181
182
|
signing_key:
|
182
183
|
specification_version: 4
|
183
184
|
summary: Extends Grape::API to support request batching
|