cp-sparrow 0.0.12 → 0.0.14
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/.gitignore +2 -1
- data/.ruby-version +1 -1
- data/LICENSE.txt +1 -1
- data/README.md +96 -13
- data/lib/sparrow.rb +21 -4
- data/lib/sparrow/configuration.rb +23 -2
- data/lib/sparrow/core_ext/hash.rb +2 -0
- data/lib/sparrow/dependencies.rb +15 -0
- data/lib/sparrow/http_message.rb +103 -0
- data/lib/sparrow/logger.rb +53 -0
- data/lib/sparrow/middleware.rb +44 -63
- data/lib/sparrow/request_http_message.rb +22 -0
- data/lib/sparrow/request_middleware.rb +8 -10
- data/lib/sparrow/response_http_message.rb +49 -0
- data/lib/sparrow/response_middleware.rb +32 -28
- data/lib/sparrow/response_steward.rb +13 -0
- data/lib/sparrow/route_parser.rb +28 -5
- data/lib/sparrow/steward.rb +125 -0
- data/lib/sparrow/strategies.rb +6 -0
- data/lib/sparrow/strategies/form_hash.rb +27 -11
- data/lib/sparrow/strategies/ignore.rb +33 -5
- data/lib/sparrow/strategies/json_format_strategies.rb +4 -0
- data/lib/sparrow/strategies/json_format_strategies/array_json_format_strategy.rb +23 -0
- data/lib/sparrow/strategies/json_format_strategies/default_json_format_strategy.rb +11 -2
- data/lib/sparrow/strategies/json_format_strategies/json_format_strategy.rb +21 -6
- data/lib/sparrow/strategies/json_format_strategies/rack_body_json_format_strategy.rb +22 -0
- data/lib/sparrow/strategies/key_transformation.rb +2 -0
- data/lib/sparrow/strategies/key_transformation/camelize_key.rb +45 -17
- data/lib/sparrow/strategies/key_transformation/underscore_key.rb +17 -6
- data/lib/sparrow/strategies/raw_input.rb +29 -7
- data/lib/sparrow/strategies/transform_params.rb +46 -21
- data/lib/sparrow/transformable.rb +28 -20
- data/lib/sparrow/version.rb +1 -1
- data/sparrow.gemspec +1 -1
- data/spec/integration/apps/rails_app/config/boot.rb +1 -1
- data/spec/integration/rack/camel_caser_spec.rb +2 -2
- data/spec/unit/camelize_key_spec.rb +9 -7
- data/spec/unit/configuration_spec.rb +3 -0
- data/spec/unit/http_message.rb +66 -0
- data/spec/unit/logger_spec.rb +6 -0
- data/spec/unit/{camel_caser_spec.rb → sparrow_spec.rb} +8 -2
- data/spec/unit/steward_spec.rb +31 -0
- metadata +24 -13
- data/lib/sparrow/path_normalizer.rb +0 -10
- data/lib/sparrow/strategies/json_format_strategies/array_strategy.rb +0 -17
- data/lib/sparrow/strategies/json_format_strategies/rack_body.rb +0 -17
- data/lib/sparrow/strategies/key_transformation/key_normalizer.rb +0 -9
- data/spec/unit/path_normalizer_spec.rb +0 -23
@@ -1,24 +1,15 @@
|
|
1
|
-
require 'sparrow/strategies/transform_params'
|
2
|
-
require 'sparrow/strategies/json_format_strategies/json_format_strategy'
|
3
|
-
|
4
|
-
if ActiveSupport::VERSION::STRING.match(/3\.\d+\.\d+/)
|
5
|
-
require 'active_support/core_ext/object/to_param'
|
6
|
-
require 'active_support/core_ext/object/to_query'
|
7
|
-
end
|
8
|
-
|
9
1
|
module Sparrow
|
2
|
+
##
|
3
|
+
# Encapsulates json transform methods. This is basically the core entry point
|
4
|
+
# for all conversions done by the middleware.
|
10
5
|
module Transformable
|
6
|
+
##
|
7
|
+
# Does the conversion based on the selected strategy and HTTP header
|
8
|
+
# parameters provided
|
11
9
|
def transform_params
|
12
10
|
transform_params_strategy.transform(ensure_json)
|
13
11
|
end
|
14
12
|
|
15
|
-
def transform_strategy
|
16
|
-
default =
|
17
|
-
Sparrow.configuration.default_json_key_transformation_strategy(type)
|
18
|
-
strategy = json_format || default
|
19
|
-
strategy.to_sym
|
20
|
-
end
|
21
|
-
|
22
13
|
def handle
|
23
14
|
transform_query_string
|
24
15
|
end
|
@@ -32,19 +23,23 @@ module Sparrow
|
|
32
23
|
end
|
33
24
|
|
34
25
|
private
|
26
|
+
|
35
27
|
def json_format
|
36
|
-
if respond_to?(:env)
|
28
|
+
if respond_to?(:env)
|
37
29
|
env[Sparrow.configuration.json_format_header(type)]
|
38
30
|
else
|
39
31
|
nil
|
40
32
|
end
|
41
33
|
end
|
42
34
|
|
35
|
+
# Make sure we are always dealing with a Ruby representation of a JSON
|
36
|
+
# String, i.e. a Hash
|
43
37
|
def ensure_json
|
44
38
|
json_params = if !params.is_a?(Hash)
|
45
39
|
Sparrow::Strategies::JsonFormatStrategy.convert(params)
|
46
|
-
elsif params.is_a?(Hash) &&
|
47
|
-
|
40
|
+
elsif params.is_a?(Hash) &&
|
41
|
+
params.values == [nil] &&
|
42
|
+
params.keys.length == 1
|
48
43
|
params.keys.first
|
49
44
|
else
|
50
45
|
params
|
@@ -62,15 +57,28 @@ module Sparrow
|
|
62
57
|
end
|
63
58
|
|
64
59
|
def transform_params_strategy
|
65
|
-
|
66
|
-
|
60
|
+
transform_strategy_options = {
|
61
|
+
camelize_ignore_uppercase_keys: Sparrow.configuration.camelize_ignore_uppercase_keys,
|
62
|
+
camelize_strategy: Sparrow.configuration.camelize_strategy
|
63
|
+
|
64
|
+
}
|
65
|
+
transform_params = Sparrow::Strategies::TransformParams
|
66
|
+
transform_params.new(transform_strategy, transform_strategy_options)
|
67
67
|
end
|
68
68
|
|
69
|
+
##
|
70
|
+
# Usualy Query String parameters want to get transformed as well
|
69
71
|
def transform_query_string
|
70
72
|
env_query_hash = Rack::Utils.parse_nested_query(env['QUERY_STRING'])
|
71
73
|
transformed_hash = transform_params_strategy.transform(env_query_hash)
|
72
74
|
env['QUERY_STRING'] = transformed_hash.to_param
|
73
75
|
end
|
74
76
|
|
77
|
+
def transform_strategy
|
78
|
+
default =
|
79
|
+
Sparrow.configuration.default_json_key_transformation_strategy(type)
|
80
|
+
strategy = json_format || default
|
81
|
+
strategy.to_sym
|
82
|
+
end
|
75
83
|
end
|
76
84
|
end
|
data/lib/sparrow/version.rb
CHANGED
data/sparrow.gemspec
CHANGED
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
|
|
15
15
|
such as camelCase and snake_case. Both converting responses and requests are
|
16
16
|
supported.
|
17
17
|
}
|
18
|
-
spec.homepage = ""
|
18
|
+
spec.homepage = "https://github.com/GateprotectGmbH/sparrow"
|
19
19
|
spec.license = "MIT"
|
20
20
|
|
21
21
|
spec.files = `git ls-files -z`.split("\x0")
|
@@ -31,7 +31,7 @@ describe "camel caser middleware", type: :rack do
|
|
31
31
|
post '/', json
|
32
32
|
end
|
33
33
|
|
34
|
-
subject do
|
34
|
+
subject do
|
35
35
|
MultiJson.load(last_response.body)
|
36
36
|
end
|
37
37
|
|
@@ -43,7 +43,7 @@ describe "camel caser middleware", type: :rack do
|
|
43
43
|
|
44
44
|
it 'converted the snake case fake key to camelCase' do
|
45
45
|
expect(subject['fake_key']).to_not be_present
|
46
|
-
expect(subject['fakeKey']).to eq
|
46
|
+
expect(subject['fakeKey']).to eq false
|
47
47
|
end
|
48
48
|
end
|
49
49
|
end
|
@@ -1,19 +1,21 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe Sparrow::Strategies::CamelizeKey do
|
3
|
+
describe Sparrow::Strategies::KeyTransformation::CamelizeKey do
|
4
4
|
|
5
|
-
subject
|
5
|
+
subject(:camelize_key_strategy) do
|
6
|
+
Sparrow::Strategies::KeyTransformation::CamelizeKey.new
|
7
|
+
end
|
6
8
|
|
7
9
|
describe '#transform_key' do
|
8
10
|
|
9
|
-
it 'should camelize
|
10
|
-
output =
|
11
|
-
expect(output).to eq(
|
11
|
+
it 'should camelize its inputs (defaulting to lower case camelizing)' do
|
12
|
+
output = camelize_key_strategy.transform_key('wireless_configuration')
|
13
|
+
expect(output).to eq('wirelessConfiguration')
|
12
14
|
end
|
13
15
|
|
14
16
|
it 'should leave all_uppercase strings as they are' do
|
15
|
-
output =
|
16
|
-
expect(output).to eq(
|
17
|
+
output = camelize_key_strategy.transform_key('DE')
|
18
|
+
expect(output).to eq('DE')
|
17
19
|
end
|
18
20
|
end
|
19
21
|
end
|
@@ -6,11 +6,14 @@ module Sparrow
|
|
6
6
|
its(:json_response_format_header) do
|
7
7
|
is_expected.to eq 'response-json-format'
|
8
8
|
end
|
9
|
+
its(:enable_logging) { is_expected.to be false }
|
9
10
|
its(:excluded_routes) { is_expected.to eq [] }
|
10
11
|
its(:default_json_request_key_transformation_strategy) do
|
11
12
|
is_expected.to eq :camelize
|
12
13
|
end
|
13
14
|
|
15
|
+
its(:camelize_strategy) { is_expected.to eq :lower }
|
16
|
+
|
14
17
|
its(:default_json_response_key_transformation_strategy) do
|
15
18
|
is_expected.to eq :camelize
|
16
19
|
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Sparrow
|
2
|
+
describe HttpMessage, type: :unit do
|
3
|
+
let(:env) do
|
4
|
+
{
|
5
|
+
'Content-Type' => 'application/json; charset=utf-8',
|
6
|
+
'Accept' => 'application/json',
|
7
|
+
'PATH_INFO' => '/api/status.json',
|
8
|
+
'rack.input' => '',
|
9
|
+
'rack.request.form_hash' => {
|
10
|
+
'panda' => 'bamboo'
|
11
|
+
}
|
12
|
+
}
|
13
|
+
end
|
14
|
+
subject(:request) { described_class.new(env) }
|
15
|
+
|
16
|
+
its(:path) { is_expected.to eq '/api/status.json' }
|
17
|
+
its(:accept) { is_expected.to eq 'application/json' }
|
18
|
+
it { is_expected.to be_form_hash }
|
19
|
+
|
20
|
+
describe '#content_type' do
|
21
|
+
it 'takes the content type until the ;' do
|
22
|
+
expect(request.content_type).to eq 'application/json'
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'without env' do
|
26
|
+
let(:env) { {} }
|
27
|
+
it 'allows an unset content type' do
|
28
|
+
expect(request.content_type).to eq nil
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'wraps the environment hash' do
|
34
|
+
expect(request.env).to eq env
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#request' do
|
38
|
+
it 'wraps the real request object' do
|
39
|
+
expect(request.request).to be_present
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'when in a Rails environment' do
|
43
|
+
it 'wraps a ActionDispatch::Request object' do
|
44
|
+
require 'rails'
|
45
|
+
expect(request.request).to be_an_instance_of ::ActionDispatch::Request
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'when in a Rack environment' do
|
50
|
+
before do
|
51
|
+
# unload Rails
|
52
|
+
Object.send(:remove_const, :Rails)
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'wraps a Rack::Request object' do
|
56
|
+
expect(request.request).to be_an_instance_of ::Rack::Request
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'delegates all unknown methods to the wrapped request object' do
|
62
|
+
# use params accessor as example
|
63
|
+
expect(request.params).to be
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Sparrow do
|
4
|
-
describe 'configuration' do
|
4
|
+
describe '.configuration' do
|
5
5
|
it 'should return a Configuration object' do
|
6
6
|
expect(Sparrow.configuration).to(
|
7
7
|
be_an_instance_of(Sparrow::Configuration))
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
describe 'configure' do
|
11
|
+
describe '.configure' do
|
12
12
|
it 'should yield the configuration and save it' do
|
13
13
|
Sparrow.configure do |configuration|
|
14
14
|
configuration.json_request_format_header = 'panda'
|
@@ -20,4 +20,10 @@ describe Sparrow do
|
|
20
20
|
expect(configuration.excluded_routes).to eq ['panda']
|
21
21
|
end
|
22
22
|
end
|
23
|
+
|
24
|
+
describe '.logger' do
|
25
|
+
it 'returns a Sparrow::Logger' do
|
26
|
+
expect(Sparrow.logger).to be_kind_of Sparrow::Logger
|
27
|
+
end
|
28
|
+
end
|
23
29
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Sparrow
|
4
|
+
describe Steward, type: :unit do
|
5
|
+
let(:env) do
|
6
|
+
{
|
7
|
+
'PATH_INFO' => '/api/state',
|
8
|
+
'rack.input' => 'teststring',
|
9
|
+
'CONTENT-TYPE' => 'application/json',
|
10
|
+
'ACCEPT' => 'application/json'
|
11
|
+
}
|
12
|
+
end
|
13
|
+
let(:http_message) { RequestHttpMessage.new(env) }
|
14
|
+
let(:excluded_routes) { ['/api/model/certificates'] }
|
15
|
+
let(:allowed_accepts) { ['application/json'] }
|
16
|
+
let(:allowed_content_types) { ['application/json'] }
|
17
|
+
subject(:steward) do
|
18
|
+
Steward.new(http_message,
|
19
|
+
allowed_accepts: allowed_accepts,
|
20
|
+
allowed_content_types: allowed_content_types,
|
21
|
+
excluded_routes: excluded_routes)
|
22
|
+
end
|
23
|
+
|
24
|
+
it { is_expected.to have_processable_http_message }
|
25
|
+
its(:http_message) { is_expected.to eq http_message }
|
26
|
+
its(:route_parser) { is_expected.to be_present }
|
27
|
+
its(:excluded_routes) { is_expected.to eq excluded_routes }
|
28
|
+
its(:allowed_accepts) { is_expected.to eq allowed_accepts }
|
29
|
+
its(:allowed_content_types) { is_expected.to eq allowed_content_types }
|
30
|
+
end
|
31
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cp-sparrow
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Schmidt
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-11-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -158,20 +158,28 @@ files:
|
|
158
158
|
- lib/sparrow.rb
|
159
159
|
- lib/sparrow/configuration.rb
|
160
160
|
- lib/sparrow/core_ext/hash.rb
|
161
|
+
- lib/sparrow/dependencies.rb
|
162
|
+
- lib/sparrow/http_message.rb
|
163
|
+
- lib/sparrow/logger.rb
|
161
164
|
- lib/sparrow/middleware.rb
|
162
|
-
- lib/sparrow/path_normalizer.rb
|
163
165
|
- lib/sparrow/railtie.rb
|
166
|
+
- lib/sparrow/request_http_message.rb
|
164
167
|
- lib/sparrow/request_middleware.rb
|
168
|
+
- lib/sparrow/response_http_message.rb
|
165
169
|
- lib/sparrow/response_middleware.rb
|
170
|
+
- lib/sparrow/response_steward.rb
|
166
171
|
- lib/sparrow/route_parser.rb
|
172
|
+
- lib/sparrow/steward.rb
|
173
|
+
- lib/sparrow/strategies.rb
|
167
174
|
- lib/sparrow/strategies/form_hash.rb
|
168
175
|
- lib/sparrow/strategies/ignore.rb
|
169
|
-
- lib/sparrow/strategies/json_format_strategies
|
176
|
+
- lib/sparrow/strategies/json_format_strategies.rb
|
177
|
+
- lib/sparrow/strategies/json_format_strategies/array_json_format_strategy.rb
|
170
178
|
- lib/sparrow/strategies/json_format_strategies/default_json_format_strategy.rb
|
171
179
|
- lib/sparrow/strategies/json_format_strategies/json_format_strategy.rb
|
172
|
-
- lib/sparrow/strategies/json_format_strategies/
|
180
|
+
- lib/sparrow/strategies/json_format_strategies/rack_body_json_format_strategy.rb
|
181
|
+
- lib/sparrow/strategies/key_transformation.rb
|
173
182
|
- lib/sparrow/strategies/key_transformation/camelize_key.rb
|
174
|
-
- lib/sparrow/strategies/key_transformation/key_normalizer.rb
|
175
183
|
- lib/sparrow/strategies/key_transformation/underscore_key.rb
|
176
184
|
- lib/sparrow/strategies/raw_input.rb
|
177
185
|
- lib/sparrow/strategies/transform_params.rb
|
@@ -219,12 +227,14 @@ files:
|
|
219
227
|
- spec/support/rack_app_helper.rb
|
220
228
|
- spec/support/rails_app_helper.rb
|
221
229
|
- spec/support/unit_spec_helper.rb
|
222
|
-
- spec/unit/camel_caser_spec.rb
|
223
230
|
- spec/unit/camelize_key_spec.rb
|
224
231
|
- spec/unit/configuration_spec.rb
|
225
|
-
- spec/unit/
|
232
|
+
- spec/unit/http_message.rb
|
233
|
+
- spec/unit/logger_spec.rb
|
226
234
|
- spec/unit/route_parser_spec.rb
|
227
|
-
|
235
|
+
- spec/unit/sparrow_spec.rb
|
236
|
+
- spec/unit/steward_spec.rb
|
237
|
+
homepage: https://github.com/GateprotectGmbH/sparrow
|
228
238
|
licenses:
|
229
239
|
- MIT
|
230
240
|
metadata: {}
|
@@ -244,7 +254,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
244
254
|
version: '0'
|
245
255
|
requirements: []
|
246
256
|
rubyforge_project:
|
247
|
-
rubygems_version: 2.4.
|
257
|
+
rubygems_version: 2.4.6
|
248
258
|
signing_key:
|
249
259
|
specification_version: 4
|
250
260
|
summary: A POC to have a Rack middleware parsing the params keys into underscore
|
@@ -290,9 +300,10 @@ test_files:
|
|
290
300
|
- spec/support/rack_app_helper.rb
|
291
301
|
- spec/support/rails_app_helper.rb
|
292
302
|
- spec/support/unit_spec_helper.rb
|
293
|
-
- spec/unit/camel_caser_spec.rb
|
294
303
|
- spec/unit/camelize_key_spec.rb
|
295
304
|
- spec/unit/configuration_spec.rb
|
296
|
-
- spec/unit/
|
305
|
+
- spec/unit/http_message.rb
|
306
|
+
- spec/unit/logger_spec.rb
|
297
307
|
- spec/unit/route_parser_spec.rb
|
298
|
-
|
308
|
+
- spec/unit/sparrow_spec.rb
|
309
|
+
- spec/unit/steward_spec.rb
|
@@ -1,17 +0,0 @@
|
|
1
|
-
require 'sparrow/strategies/json_format_strategies/json_format_strategy'
|
2
|
-
|
3
|
-
module Sparrow
|
4
|
-
module Strategies
|
5
|
-
class ArrayStrategy < JsonFormatStrategy
|
6
|
-
register_json_format
|
7
|
-
|
8
|
-
def match?(input)
|
9
|
-
input.is_a? Array
|
10
|
-
end
|
11
|
-
|
12
|
-
def convert(input)
|
13
|
-
input.first
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/json_format_strategy'
|
2
|
-
|
3
|
-
module Sparrow
|
4
|
-
module Strategies
|
5
|
-
class RackBody < JsonFormatStrategy
|
6
|
-
register_json_format
|
7
|
-
|
8
|
-
def match?(input)
|
9
|
-
input.respond_to?(:body)
|
10
|
-
end
|
11
|
-
|
12
|
-
def convert(input)
|
13
|
-
input.body
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|