grape-batch 1.1.3 → 1.1.4
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -2
- data/README.md +25 -0
- data/lib/grape/batch/configuration.rb +5 -3
- data/lib/grape/batch/version.rb +1 -1
- data/lib/grape/batch.rb +17 -12
- data/spec/requests_spec.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 483ecfa0b036413cd49c11f55a3c59529488e7a8
|
4
|
+
data.tar.gz: 32baccf14eaa0a23afff45f9d86802d42323defb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a78f087d10ac368f6fc7db6aec78c87bad58268da4fe374a19269eb199dc1d0d2cd4a3894b89cb3739b46a66b6606d01ed44e9b067113c8073706b79083d230
|
7
|
+
data.tar.gz: 678c766c786aba5a407851a28322bf3ec447f629a80086593cbe65242800f4ba2f1902a86ef3101a512cc275576730c14ebde618c7742d50a016be7e939d08b6
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -47,6 +47,9 @@ Grape::Batch.configure do |config|
|
|
47
47
|
config.limit = 10
|
48
48
|
config.path = '/batch'
|
49
49
|
config.formatter = Grape::Batch::Response
|
50
|
+
config.logger = nil
|
51
|
+
config.session_header = 'HTTP_X_SESSION_TOKEN'
|
52
|
+
config.session_proc = Proc.new {}
|
50
53
|
end
|
51
54
|
```
|
52
55
|
|
@@ -97,6 +100,28 @@ POST http request on the default URL with a similar body:
|
|
97
100
|
|
98
101
|
'body' is optional.
|
99
102
|
|
103
|
+
### Sessions
|
104
|
+
It's possible ensure a single session during the execution of the batch. You have to specify your session/token/auth header and your session Proc. Before running the batch, the Proc is executed with the data extracted from the specified session header and stored in rack env 'api.session' key.
|
105
|
+
```ruby
|
106
|
+
# Example
|
107
|
+
# Considering the config
|
108
|
+
Grape::Batch.configure do |config|
|
109
|
+
config.session_header = 'HTTP_X_SESSION_TOKEN'
|
110
|
+
config.session_proc = Proc.new {|token| User.where(token: token).first }
|
111
|
+
end
|
112
|
+
|
113
|
+
# You can build a Grape helper like this
|
114
|
+
module AuthHelpers
|
115
|
+
def current_user
|
116
|
+
if env['api.session']
|
117
|
+
env['api.session']
|
118
|
+
else
|
119
|
+
# do authentication stuff
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
```
|
124
|
+
|
100
125
|
## Contributing
|
101
126
|
|
102
127
|
1. Fork it ( https://github.com/c4mprod/grape-batch/fork )
|
@@ -1,13 +1,15 @@
|
|
1
1
|
module Grape
|
2
2
|
module Batch
|
3
3
|
class Configuration
|
4
|
-
attr_accessor :path, :limit, :formatter, :logger
|
4
|
+
attr_accessor :path, :limit, :formatter, :logger, :session_proc, :session_header
|
5
5
|
|
6
6
|
def initialize
|
7
|
-
@path
|
7
|
+
@path = '/batch'
|
8
8
|
@limit = 10
|
9
9
|
@formatter = Grape::Batch::Response
|
10
10
|
@logger = nil
|
11
|
+
@session_header = 'HTTP_X_SESSION_TOKEN'
|
12
|
+
@session_proc = Proc.new {}
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
@@ -28,4 +30,4 @@ module Grape
|
|
28
30
|
yield configuration
|
29
31
|
end
|
30
32
|
end
|
31
|
-
end
|
33
|
+
end
|
data/lib/grape/batch/version.rb
CHANGED
data/lib/grape/batch.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'active_support'
|
2
2
|
require 'grape/batch/log_subscriber'
|
3
|
-
|
4
3
|
require 'grape/batch/version'
|
5
4
|
require 'grape/batch/errors'
|
6
5
|
require 'grape/batch/configuration'
|
@@ -50,10 +49,14 @@ module Grape
|
|
50
49
|
ActiveSupport::Notifications.instrument 'dispatch.batch' do |event|
|
51
50
|
event[:requests] = []
|
52
51
|
|
52
|
+
session_data = env[Grape::Batch.configuration.session_header]
|
53
|
+
env['api.session'] = Grape::Batch.configuration.session_proc.call(session_data)
|
54
|
+
|
53
55
|
# iterate
|
56
|
+
batch_env = env.dup
|
54
57
|
batch_requests.map do |request|
|
55
58
|
# init env for Grape resource
|
56
|
-
tmp_env = prepare_tmp_env(
|
59
|
+
tmp_env = prepare_tmp_env(batch_env, request)
|
57
60
|
status, headers, response = @app.call(tmp_env)
|
58
61
|
|
59
62
|
# format response
|
@@ -69,18 +72,20 @@ module Grape
|
|
69
72
|
method = request['method']
|
70
73
|
path = request['path']
|
71
74
|
body = request['body'].is_a?(Hash) ? request['body'] : {}
|
75
|
+
query_string = ''
|
76
|
+
rack_input = '{}'
|
72
77
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
if method == 'GET'
|
78
|
-
env['rack.input'] = StringIO.new('{}')
|
79
|
-
env['QUERY_STRING'] = URI.encode_www_form(HashConverter.encode(body).to_a)
|
80
|
-
else
|
81
|
-
env['rack.input'] = StringIO.new(MultiJson.encode(body))
|
82
|
-
end
|
78
|
+
if method == 'GET'
|
79
|
+
query_string = URI.encode_www_form(HashConverter.encode(body).to_a)
|
80
|
+
else
|
81
|
+
rack_input = StringIO.new(MultiJson.encode(body))
|
83
82
|
end
|
83
|
+
|
84
|
+
tmp_env['REQUEST_METHOD'] = method
|
85
|
+
tmp_env['PATH_INFO'] = path
|
86
|
+
tmp_env['QUERY_STRING'] = query_string
|
87
|
+
tmp_env['rack.input'] = rack_input
|
88
|
+
tmp_env
|
84
89
|
end
|
85
90
|
end
|
86
91
|
end
|
data/spec/requests_spec.rb
CHANGED
@@ -25,7 +25,6 @@ RSpec.describe Grape::Batch::Base do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
describe '/api' do
|
28
|
-
|
29
28
|
describe 'GET /hello' do
|
30
29
|
let(:response) { request.get('/api/v1/hello') }
|
31
30
|
|
@@ -191,6 +190,7 @@ RSpec.describe Grape::Batch::Base do
|
|
191
190
|
config = Grape::Batch::Configuration.new
|
192
191
|
config.path = '/custom_path'
|
193
192
|
config.limit = 15
|
193
|
+
config.session_proc = Proc.new { 3 + 2 }
|
194
194
|
config
|
195
195
|
end
|
196
196
|
end
|
@@ -198,6 +198,7 @@ RSpec.describe Grape::Batch::Base do
|
|
198
198
|
describe 'default_value' do
|
199
199
|
it { expect(Grape::Batch.configuration.path).to eq('/custom_path') }
|
200
200
|
it { expect(Grape::Batch.configuration.limit).to eq(15) }
|
201
|
+
it { expect(Grape::Batch.configuration.session_proc.call).to eq(5) }
|
201
202
|
end
|
202
203
|
end
|
203
204
|
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: 1.1.
|
4
|
+
version: 1.1.4
|
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: 2015-
|
13
|
+
date: 2015-07-17 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rack
|