grape-batch 1.0.4 → 1.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3cde41da304a6addd17c9d57b4326bf16d394682
4
- data.tar.gz: 5f59e3a4857f129f6f16c07924067721c132c27e
3
+ metadata.gz: fe13afdcafd4a0e284de0e38970eb61da36775a9
4
+ data.tar.gz: b982801271bb9f54f12e5a9d426c741bf1adea05
5
5
  SHA512:
6
- metadata.gz: a74d5336ae59e801dede8e4e3297db10f60c3a4acc553f1dc0feced8256c3c0ca5c733fbfad24b2a0d705b02ab7bef176f703074a5eb4b8da352b006d300907a
7
- data.tar.gz: 2c4fa0a3ead2bbe33f712883404cfcf3e2da961fb7da4c33d90e977512cf93dba5b5dcbdde14a8ffc534f3eb6f97649f7f378e1c9d20a839f49d97d425ccfa58
6
+ metadata.gz: cb0dacb5ff92a2f6586650f9d5e2749d4298b31ffb01314255f125481f613d0eef2112eb10097a733172c99a0edf64ea4d153ac2cd3a607b2c1e45b06d41b1d3
7
+ data.tar.gz: 3050df5407794bea99483173142d65f2c7eb997b81e1aad6b62ee28ccd807688eef675cb4711e12d3b5abc7c21e081a610836cc4ef9b1fed91cb30d95253c8a9
data/.rspec CHANGED
@@ -1,3 +1,4 @@
1
1
  --color
2
2
  --require spec_helper
3
- --format documentation
3
+ --format documentation
4
+ --order rand
data/README.md CHANGED
@@ -40,13 +40,21 @@ use Grape::Batch::Base
40
40
  ```
41
41
 
42
42
  ### Settings
43
- You can customize the middleware with a hash.
43
+ Override any of these defaults in config/initializers/grape_batch.rb:
44
+
45
+ ```ruby
46
+ Grape::Batch.configure do |config|
47
+ config.limit = 10
48
+ config.path = '/batch'
49
+ config.formatter = Grape::Batch::Response
50
+ end
51
+ ```
44
52
 
45
53
  | Argument | Type | Default | Description
46
54
  | :---: | :---: | :---: | :---:
47
- | :limit | integer | 10 | Maximum number of batched requests allowed by the middleware
48
- | :path | string | /batch | Route on which the middleware is mounted on
49
- | :formatter | class | Grape::Batch::Response | The response formatter to use
55
+ | limit | integer | 10 | Maximum number of batched requests allowed by the middleware
56
+ | path | string | /batch | Route on which the middleware is mounted on
57
+ | formatter | class | Grape::Batch::Response | The response formatter to use
50
58
 
51
59
  #### Response formatter
52
60
  #####Default format (success)
@@ -95,4 +103,4 @@ POST http request on the default URL with a similar body:
95
103
  2. Create your feature branch (`git checkout -b my-new-feature`)
96
104
  3. Commit your changes (`git commit -am 'Add some feature'`)
97
105
  4. Push to the branch (`git push origin my-new-feature`)
98
- 5. Create a new Pull Request
106
+ 5. Create a new Pull Request
@@ -0,0 +1,30 @@
1
+ module Grape
2
+ module Batch
3
+ class Configuration
4
+ attr_accessor :path, :limit, :formatter
5
+
6
+ def initialize
7
+ @path = '/batch'
8
+ @limit = 10
9
+ @formatter = Grape::Batch::Response
10
+ end
11
+ end
12
+
13
+ # Set default configuration for Grape::Batch middleware
14
+ class << self
15
+ attr_accessor :configuration
16
+ end
17
+
18
+ def self.configuration
19
+ @configuration ||= Configuration.new
20
+ end
21
+
22
+ def self.configuration=(config)
23
+ @configuration = config
24
+ end
25
+
26
+ def self.configure
27
+ yield configuration
28
+ end
29
+ end
30
+ end
@@ -1,5 +1,5 @@
1
1
  module Grape
2
2
  module Batch
3
- VERSION = '1.0.4'
3
+ VERSION = '1.1.0'
4
4
  end
5
5
  end
data/lib/grape/batch.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'grape/batch/errors'
2
+ require 'grape/batch/configuration'
2
3
  require 'grape/batch/parser'
3
4
  require 'grape/batch/response'
4
5
  require 'grape/batch/version'
@@ -7,11 +8,9 @@ require 'multi_json'
7
8
  module Grape
8
9
  module Batch
9
10
  class Base
10
- def initialize(app, opt = {})
11
+ def initialize(app)
11
12
  @app = app
12
- @limit = opt[:limit] || 10
13
- @path = opt[:path] || '/batch'
14
- @response_klass = opt[:formatter] || Grape::Batch::Response
13
+ @response_klass = Grape::Batch.configuration.formatter
15
14
  end
16
15
 
17
16
  def call(env)
@@ -24,7 +23,7 @@ module Grape
24
23
  headers = {'Content-Type' => 'application/json'}
25
24
 
26
25
  begin
27
- batch_requests = Grape::Batch::Validator::parse(env, @limit)
26
+ batch_requests = Grape::Batch::Validator::parse(env, Grape::Batch.configuration.limit)
28
27
  result = dispatch(env, batch_requests)
29
28
  body = MultiJson.encode(result)
30
29
  rescue Grape::Batch::RequestBodyError, Grape::Batch::TooManyRequestsError => e
@@ -38,7 +37,7 @@ module Grape
38
37
  private
39
38
 
40
39
  def is_batch_request?(env)
41
- env['PATH_INFO'].start_with?(@path) &&
40
+ env['PATH_INFO'].start_with?(Grape::Batch.configuration.path) &&
42
41
  env['REQUEST_METHOD'] == 'POST' &&
43
42
  env['CONTENT_TYPE'] == 'application/json'
44
43
  end
@@ -161,4 +161,30 @@ RSpec.describe Grape::Batch::Base do
161
161
  end
162
162
  end
163
163
  end
164
+
165
+ describe '#configure' do
166
+ it { expect(Grape::Batch.configuration).to_not be_nil }
167
+
168
+ describe 'default_value' do
169
+ it { expect(Grape::Batch.configuration.path).to eq('/batch') }
170
+ it { expect(Grape::Batch.configuration.formatter).to eq(Grape::Batch::Response) }
171
+ it { expect(Grape::Batch.configuration.limit).to eq(10) }
172
+ end
173
+
174
+ describe '.configure' do
175
+ before do
176
+ allow( Grape::Batch).to receive(:configuration) do
177
+ config = Grape::Batch::Configuration.new
178
+ config.path = '/custom_path'
179
+ config.limit = 15
180
+ config
181
+ end
182
+ end
183
+
184
+ describe 'default_value' do
185
+ it { expect(Grape::Batch.configuration.path).to eq('/custom_path') }
186
+ it { expect(Grape::Batch.configuration.limit).to eq(15) }
187
+ end
188
+ end
189
+ end
164
190
  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.0.4
4
+ version: 1.1.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: 2014-11-19 00:00:00.000000000 Z
13
+ date: 2014-12-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rack
@@ -130,6 +130,7 @@ files:
130
130
  - Rakefile
131
131
  - grape-batch.gemspec
132
132
  - lib/grape/batch.rb
133
+ - lib/grape/batch/configuration.rb
133
134
  - lib/grape/batch/errors.rb
134
135
  - lib/grape/batch/parser.rb
135
136
  - lib/grape/batch/response.rb