cp-sparrow 0.0.14 → 0.0.15
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 +1 -0
- data/.ruby-version +1 -1
- data/.travis.yml +2 -5
- data/README.md +6 -3
- data/Rakefile +8 -7
- data/lib/cp-sparrow.rb +1 -0
- data/lib/sparrow/configuration.rb +5 -1
- data/lib/sparrow/core_ext/hash.rb +22 -17
- data/lib/sparrow/dependencies.rb +11 -2
- data/lib/sparrow/logger.rb +1 -1
- data/lib/sparrow/railtie.rb +3 -3
- data/lib/sparrow/request_http_message.rb +1 -1
- data/lib/sparrow/response_middleware.rb +5 -5
- data/lib/sparrow/route_parser.rb +1 -1
- data/lib/sparrow/strategies/ignore.rb +2 -2
- data/lib/sparrow/strategies/json_format_strategies/default_json_format_strategy.rb +1 -1
- data/lib/sparrow/strategies/json_format_strategies/json_format_strategy.rb +5 -4
- data/lib/sparrow/strategies/key_transformation/underscore_key.rb +1 -1
- data/lib/sparrow/strategies/transform_params.rb +5 -1
- data/lib/sparrow/transformable.rb +8 -8
- data/lib/sparrow/version.rb +1 -1
- data/sparrow.gemspec +5 -3
- data/spec/integration/apps/rack_app/app.rb +22 -5
- data/spec/integration/apps/rack_app/config.ru +2 -2
- data/spec/integration/apps/rails_app/README.rdoc +15 -248
- data/spec/integration/apps/rails_app/Rakefile +1 -2
- data/spec/integration/apps/rails_app/app/assets/images/.keep +0 -0
- data/spec/integration/apps/rails_app/app/assets/javascripts/application.js +4 -6
- data/spec/integration/apps/rails_app/app/assets/stylesheets/application.css +6 -4
- data/spec/integration/apps/rails_app/app/controllers/application_controller.rb +3 -1
- data/spec/integration/apps/rails_app/app/controllers/concerns/.keep +0 -0
- data/spec/integration/apps/rails_app/app/controllers/errors_controller.rb +10 -0
- data/spec/integration/apps/rails_app/app/controllers/welcome_controller.rb +0 -5
- data/spec/integration/apps/rails_app/app/mailers/.keep +0 -0
- data/spec/integration/apps/rails_app/app/models/.keep +0 -0
- data/spec/integration/apps/rails_app/app/models/concerns/.keep +0 -0
- data/spec/integration/apps/rails_app/app/views/layouts/application.html.erb +3 -3
- data/spec/integration/apps/rails_app/bin/bundle +3 -0
- data/spec/integration/apps/rails_app/bin/rails +4 -0
- data/spec/integration/apps/rails_app/bin/rake +4 -0
- data/spec/integration/apps/rails_app/bin/setup +29 -0
- data/spec/integration/apps/rails_app/config.ru +2 -2
- data/spec/integration/apps/rails_app/config/application.rb +6 -43
- data/spec/integration/apps/rails_app/config/boot.rb +4 -9
- data/spec/integration/apps/rails_app/config/database.yml +25 -0
- data/spec/integration/apps/rails_app/config/environment.rb +3 -3
- data/spec/integration/apps/rails_app/config/environments/development.rb +23 -13
- data/spec/integration/apps/rails_app/config/environments/production.rb +46 -31
- data/spec/integration/apps/rails_app/config/environments/test.rb +20 -10
- data/spec/integration/apps/rails_app/config/initializers/assets.rb +11 -0
- data/spec/integration/apps/rails_app/config/initializers/cookies_serializer.rb +3 -0
- data/spec/integration/apps/rails_app/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/integration/apps/rails_app/config/initializers/inflections.rb +6 -5
- data/spec/integration/apps/rails_app/config/initializers/mime_types.rb +0 -1
- data/spec/integration/apps/rails_app/config/initializers/secret_token.rb +1 -1
- data/spec/integration/apps/rails_app/config/initializers/session_store.rb +1 -6
- data/spec/integration/apps/rails_app/config/initializers/wrap_parameters.rb +6 -2
- data/spec/integration/apps/rails_app/config/locales/en.yml +20 -2
- data/spec/integration/apps/rails_app/config/routes.rb +21 -29
- data/spec/integration/apps/rails_app/config/secrets.yml +22 -0
- data/spec/integration/apps/rails_app/lib/assets/.keep +0 -0
- data/spec/integration/apps/rails_app/public/404.html +54 -13
- data/spec/integration/apps/rails_app/public/422.html +54 -13
- data/spec/integration/apps/rails_app/public/500.html +53 -12
- data/spec/integration/rack/camel_caser_spec.rb +18 -18
- data/spec/integration/rack/error_spec.rb +17 -0
- data/spec/integration/rails/camel_caser_accept_header_spec.rb +3 -3
- data/spec/integration/rails/camel_caser_spec.rb +30 -19
- data/spec/spec_helper.rb +1 -2
- data/spec/support/rails_app_helper.rb +1 -1
- data/spec/unit/configuration_spec.rb +2 -2
- metadata +46 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6203b666448091c6aa36a5df6892f488a8bc96b
|
4
|
+
data.tar.gz: 9baa9565eba84c01ce3c0c742403892101c96a48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 151bd8ceaf5864f652d6479fa4bcfe84151b84fe3045b298a4208fd9edaed7a1ca1b573c758062f019824657bef7384f5d4ff5b5bf1f937f225273ae531cc834
|
7
|
+
data.tar.gz: c18506cee9974098896291c53ed2b836110411717600598ae7bde84b1b23dbce326e11c87e7a717a8e3e1292ae7fc5405c373861011b9dded0227af41748c775
|
data/.gitignore
CHANGED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.3.1
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,14 +1,17 @@
|
|
1
1
|
# Sparrow
|
2
2
|
|
3
|
+
[](http://www.deviantart.com/art/Little-Sparrow-357282461)
|
4
|
+
|
3
5
|
A Rack middleware for converting the params keys and JSON response keys of a Rack application.
|
4
6
|
|
5
7
|
[](https://travis-ci.org/GateprotectGmbH/sparrow) [](http://badge.fury.io/rb/cp-sparrow)
|
8
|
+
[](https://codeclimate.com/github/GateprotectGmbH/sparrow)
|
6
9
|
|
7
10
|
## Installation
|
8
11
|
|
9
12
|
Add this line to your application's Gemfile:
|
10
13
|
|
11
|
-
gem 'cp-sparrow'
|
14
|
+
gem 'cp-sparrow'
|
12
15
|
|
13
16
|
And then execute:
|
14
17
|
|
@@ -58,7 +61,7 @@ Example:
|
|
58
61
|
|
59
62
|
### default_json_request_key_transformation_strategy
|
60
63
|
|
61
|
-
> `String`. Default: `
|
64
|
+
> `String`. Default: `underscore`
|
62
65
|
|
63
66
|
Defines how the middleware should treat incoming parameters via Request. Which means how they get tranformed, i.e. defining _underscore_ here means that incoming parameters get underscore. Possible values are _underscore_ and _camelize_
|
64
67
|
|
@@ -133,7 +136,7 @@ Example:
|
|
133
136
|
|
134
137
|
> `Symbol`. Default: `:lower`
|
135
138
|
|
136
|
-
Defines which strategy to use when camelizing keys. Possible options are `:lower` and `:upper`, which tells the middleware to
|
139
|
+
Defines which strategy to use when camelizing keys. Possible options are `:lower` and `:upper`, which tells the middleware to
|
137
140
|
start camelized keys with an uppercased character or with a lowercased character.
|
138
141
|
|
139
142
|
Example:
|
data/Rakefile
CHANGED
@@ -4,8 +4,7 @@ require "rspec/core/rake_task"
|
|
4
4
|
RSpec::Core::RakeTask.new(:spec)
|
5
5
|
|
6
6
|
RAILS_VERSIONS = [
|
7
|
-
"
|
8
|
-
"4.0.13"
|
7
|
+
"4.2.6"
|
9
8
|
]
|
10
9
|
|
11
10
|
def run_tests_for_version(version)
|
@@ -16,7 +15,7 @@ def run_tests_for_version(version)
|
|
16
15
|
commands << "bundle install"
|
17
16
|
commands << "bundle exec rspec"
|
18
17
|
|
19
|
-
system({'RAILS_VERSION' => version}, commands.join('
|
18
|
+
system({'RAILS_VERSION' => version}, commands.join(' && '))
|
20
19
|
end
|
21
20
|
|
22
21
|
task :all do
|
@@ -24,13 +23,15 @@ task :all do
|
|
24
23
|
puts "Testing gem for rails version: #{version}"
|
25
24
|
success = run_tests_for_version(version)
|
26
25
|
|
27
|
-
|
26
|
+
unless success
|
28
27
|
puts "Test suite aborted, errors occured."
|
29
28
|
exit($?.exitstatus)
|
30
29
|
end
|
31
30
|
end
|
32
31
|
end
|
33
32
|
|
34
|
-
task :default do
|
35
|
-
|
36
|
-
end
|
33
|
+
# task :default do
|
34
|
+
# run_tests_for_version(ENV['RAILS_VERSION'] || '4.2.6')
|
35
|
+
# end
|
36
|
+
|
37
|
+
task default: :spec
|
data/lib/cp-sparrow.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'sparrow'
|
@@ -21,7 +21,7 @@ module Sparrow
|
|
21
21
|
@json_request_format_header = 'request-json-format'
|
22
22
|
@json_response_format_header = 'response-json-format'
|
23
23
|
@excluded_routes = []
|
24
|
-
@default_json_request_key_transformation_strategy = :
|
24
|
+
@default_json_request_key_transformation_strategy = :underscore
|
25
25
|
@default_json_response_key_transformation_strategy = :camelize
|
26
26
|
@camelize_ignore_uppercase_keys = true
|
27
27
|
@allowed_content_types = %w[
|
@@ -35,6 +35,10 @@ module Sparrow
|
|
35
35
|
@camelize_strategy = :lower
|
36
36
|
end
|
37
37
|
|
38
|
+
def active_support_legacy_version
|
39
|
+
/3\.\d+\.\d+/
|
40
|
+
end
|
41
|
+
|
38
42
|
##
|
39
43
|
# @param type [String] the http message type.
|
40
44
|
# Must be either 'request' or 'response'.
|
@@ -1,23 +1,28 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
module Sparrow
|
2
|
+
module CoreExt
|
3
|
+
module Hash
|
4
|
+
refine ::Hash do
|
5
|
+
# Defines deep_transform_keys as available in ActiveSupport >= 4.0.2.
|
6
|
+
# See {http://apidock.com/rails/v4.2.1/Hash/deep_transform_keys}
|
7
|
+
def deep_transform_keys(&block)
|
8
|
+
deep_transform_in_object(self, &block)
|
9
|
+
end
|
7
10
|
|
8
|
-
|
11
|
+
private
|
9
12
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def deep_transform_in_object(obj, &block)
|
14
|
+
case obj
|
15
|
+
when ::Hash
|
16
|
+
obj.each_with_object({}) do |(key,value), result|
|
17
|
+
result[yield(key)] = deep_transform_in_object(value, &block)
|
18
|
+
end
|
19
|
+
when Array
|
20
|
+
obj.map { |el| deep_transform_in_object(el, &block) }
|
21
|
+
else
|
22
|
+
obj
|
23
|
+
end
|
15
24
|
end
|
16
|
-
|
17
|
-
obj.map { |el| deep_transform_in_object(el, &block) }
|
18
|
-
else
|
19
|
-
obj
|
25
|
+
end
|
20
26
|
end
|
21
27
|
end
|
22
28
|
end
|
23
|
-
|
data/lib/sparrow/dependencies.rb
CHANGED
@@ -2,14 +2,23 @@ require 'active_support/core_ext/object'
|
|
2
2
|
require 'active_support/core_ext/string'
|
3
3
|
require 'active_support/version'
|
4
4
|
|
5
|
-
|
5
|
+
module Sparrow
|
6
|
+
module_function
|
7
|
+
|
8
|
+
def uses_active_support_legacy_version?
|
9
|
+
active_support_legacy_version = /3\.\d+\.\d+/
|
10
|
+
ActiveSupport::VERSION::STRING.match(active_support_legacy_version)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
if Sparrow.uses_active_support_legacy_version?
|
6
15
|
require 'sparrow/core_ext/hash'
|
7
16
|
require 'active_support/core_ext/object/to_param'
|
8
17
|
require 'active_support/core_ext/object/to_query'
|
9
18
|
end
|
10
19
|
|
11
20
|
unless defined?(Rails)
|
12
|
-
require 'active_support/
|
21
|
+
require 'active_support/logger'
|
13
22
|
end
|
14
23
|
|
15
24
|
require 'singleton'
|
data/lib/sparrow/logger.rb
CHANGED
data/lib/sparrow/railtie.rb
CHANGED
@@ -2,10 +2,10 @@ module Sparrow
|
|
2
2
|
class Railtie < Rails::Railtie
|
3
3
|
initializer 'sparrow.insert_middleware' do |app|
|
4
4
|
# handle request
|
5
|
-
app.config.middleware.insert_after
|
6
|
-
|
5
|
+
app.config.middleware.insert_after Rails::Rack::Logger,
|
6
|
+
Sparrow::RequestMiddleware
|
7
7
|
# handle response
|
8
|
-
app.config.middleware.use
|
8
|
+
app.config.middleware.use Sparrow::ResponseMiddleware
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|
@@ -15,14 +15,14 @@ module Sparrow
|
|
15
15
|
private
|
16
16
|
|
17
17
|
def steward
|
18
|
+
configuration = Sparrow.configuration
|
18
19
|
ResponseSteward.new(http_message,
|
19
|
-
allowed_content_types:
|
20
|
-
allowed_accepts:
|
21
|
-
excluded_routes:
|
22
|
-
ignored_response_codes:
|
20
|
+
allowed_content_types: configuration.allowed_content_types,
|
21
|
+
allowed_accepts: configuration.allowed_accepts,
|
22
|
+
excluded_routes: configuration.excluded_routes,
|
23
|
+
ignored_response_codes: configuration.ignored_response_codes)
|
23
24
|
end
|
24
25
|
|
25
|
-
|
26
26
|
def converted_response_body
|
27
27
|
# return the original body if we are not going to process it
|
28
28
|
return body unless steward.has_processable_http_message?
|
data/lib/sparrow/route_parser.rb
CHANGED
@@ -46,8 +46,8 @@ module Sparrow
|
|
46
46
|
input = @env[HttpMessage::RACK_INPUT_KEY].gets
|
47
47
|
|
48
48
|
begin
|
49
|
-
@env[HttpMessage::FORM_HASH_KEY] =
|
50
|
-
rescue
|
49
|
+
@env[HttpMessage::FORM_HASH_KEY] = JSON.parse(input)
|
50
|
+
rescue JSON::ParserError
|
51
51
|
# ignore
|
52
52
|
ensure
|
53
53
|
@env[HttpMessage::RACK_INPUT_KEY].rewind
|
@@ -9,7 +9,7 @@ module Sparrow
|
|
9
9
|
class JsonFormatStrategy
|
10
10
|
##
|
11
11
|
# Empty constructor. Does nothing.
|
12
|
-
def initialize(*
|
12
|
+
def initialize(*_args)
|
13
13
|
end
|
14
14
|
|
15
15
|
##
|
@@ -29,22 +29,23 @@ module Sparrow
|
|
29
29
|
# i.e. an Array, a String or a RackBody
|
30
30
|
# @return [String] the formatted JSON
|
31
31
|
def self.convert(body)
|
32
|
-
strategy = json_format_strategies.detect do |
|
33
|
-
|
32
|
+
strategy = json_format_strategies.detect do |strategy_candidate|
|
33
|
+
strategy_candidate.match?(body)
|
34
34
|
end
|
35
35
|
strategy.convert(body)
|
36
36
|
end
|
37
37
|
|
38
|
-
private
|
39
38
|
def self.init(*args)
|
40
39
|
@@json_format_strategies ||= Array.new(args)
|
41
40
|
end
|
41
|
+
private_class_method :init
|
42
42
|
|
43
43
|
def self.json_format_strategies
|
44
44
|
init
|
45
45
|
default = Sparrow::Strategies::DefaultJsonFormatStrategy.instance
|
46
46
|
@@json_format_strategies.reject(&:blank?) + [default]
|
47
47
|
end
|
48
|
+
private_class_method :json_format_strategies
|
48
49
|
end
|
49
50
|
end
|
50
51
|
end
|
@@ -3,6 +3,10 @@ module Sparrow
|
|
3
3
|
##
|
4
4
|
# Core class to trigger the conversion
|
5
5
|
class TransformParams
|
6
|
+
if Sparrow.uses_active_support_legacy_version?
|
7
|
+
using Sparrow::CoreExt::Hash
|
8
|
+
end
|
9
|
+
|
6
10
|
##
|
7
11
|
# the strategy stating how to convert the JSON
|
8
12
|
# @return [KeyTransformation] key transformation strategy
|
@@ -51,7 +55,7 @@ module Sparrow
|
|
51
55
|
private
|
52
56
|
|
53
57
|
def create_key_transformation_strategy(key_transformation_strategy)
|
54
|
-
class_name = "#{key_transformation_strategy
|
58
|
+
class_name = "#{key_transformation_strategy}_key"
|
55
59
|
class_name = class_name.camelize
|
56
60
|
full_strategy_class_name =
|
57
61
|
"Sparrow::Strategies::KeyTransformation::#{class_name}"
|
@@ -16,10 +16,10 @@ module Sparrow
|
|
16
16
|
|
17
17
|
def json_body
|
18
18
|
json = parsable_json_string(transform_params)
|
19
|
-
|
19
|
+
JSON.parse(json)
|
20
20
|
json
|
21
|
-
rescue
|
22
|
-
|
21
|
+
rescue JSON::ParserError
|
22
|
+
JSON.generate(ensure_json)
|
23
23
|
end
|
24
24
|
|
25
25
|
private
|
@@ -38,19 +38,19 @@ module Sparrow
|
|
38
38
|
json_params = if !params.is_a?(Hash)
|
39
39
|
Sparrow::Strategies::JsonFormatStrategy.convert(params)
|
40
40
|
elsif params.is_a?(Hash) &&
|
41
|
-
params.values
|
42
|
-
params.keys.
|
41
|
+
params.values.compact.empty? &&
|
42
|
+
params.keys.one?
|
43
43
|
params.keys.first
|
44
44
|
else
|
45
45
|
params
|
46
46
|
end
|
47
|
-
return
|
47
|
+
return JSON.parse(json_params) if json_params.is_a?(String)
|
48
48
|
json_params
|
49
49
|
end
|
50
50
|
|
51
51
|
def parsable_json_string(transformable_params)
|
52
52
|
if [Hash, Array].include? transformable_params.class
|
53
|
-
|
53
|
+
JSON.generate(transformable_params)
|
54
54
|
else
|
55
55
|
transformable_params
|
56
56
|
end
|
@@ -67,7 +67,7 @@ module Sparrow
|
|
67
67
|
end
|
68
68
|
|
69
69
|
##
|
70
|
-
#
|
70
|
+
# Usually Query String parameters want to get transformed as well
|
71
71
|
def transform_query_string
|
72
72
|
env_query_hash = Rack::Utils.parse_nested_query(env['QUERY_STRING'])
|
73
73
|
transformed_hash = transform_params_strategy.transform(env_query_hash)
|
data/lib/sparrow/version.rb
CHANGED
data/sparrow.gemspec
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
rails_version = ENV["RAILS_VERSION"] || "
|
3
|
+
rails_version = ENV["RAILS_VERSION"] || "4.2.6"
|
4
|
+
ruby_version = File.open(File.join(__dir__, '.ruby-version')).read.chomp
|
4
5
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
6
|
require 'sparrow/version'
|
6
7
|
|
@@ -17,19 +18,20 @@ supported.
|
|
17
18
|
}
|
18
19
|
spec.homepage = "https://github.com/GateprotectGmbH/sparrow"
|
19
20
|
spec.license = "MIT"
|
21
|
+
spec.required_ruby_version = ">= #{ruby_version}"
|
20
22
|
|
21
23
|
spec.files = `git ls-files -z`.split("\x0")
|
22
24
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
23
25
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
24
26
|
spec.require_paths = ["lib"]
|
25
27
|
|
26
|
-
spec.add_runtime_dependency "activesupport", "
|
28
|
+
spec.add_runtime_dependency "activesupport", "<= 4.2.6"
|
29
|
+
|
27
30
|
spec.add_development_dependency "pry"
|
28
31
|
spec.add_development_dependency "bundler", "~> 1.5"
|
29
32
|
spec.add_development_dependency "rake"
|
30
33
|
spec.add_development_dependency "rspec", '~> 3'
|
31
34
|
spec.add_development_dependency "rspec-its"
|
32
35
|
spec.add_development_dependency "rack-test"
|
33
|
-
spec.add_development_dependency "multi_json"
|
34
36
|
spec.add_development_dependency "rails", rails_version
|
35
37
|
end
|
@@ -1,17 +1,34 @@
|
|
1
1
|
class App
|
2
2
|
|
3
3
|
def call(env)
|
4
|
+
@env = env
|
4
5
|
params = env["rack.input"].gets
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
@params = JSON.parse(params || '{}')
|
7
|
+
header = {
|
8
|
+
'Content-Type' => 'application/json',
|
9
|
+
'Accept' => 'application/json'
|
10
|
+
}
|
11
|
+
|
12
|
+
response = Rack::Response.new(
|
13
|
+
json_response(params),
|
14
|
+
status,
|
15
|
+
header)
|
8
16
|
response.finish
|
9
17
|
end
|
10
18
|
|
11
19
|
private
|
12
20
|
|
21
|
+
def status
|
22
|
+
path = @env['PATH_INFO']
|
23
|
+
if path =~ /error$/
|
24
|
+
@params['error_code'] || 500
|
25
|
+
else
|
26
|
+
200
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
13
30
|
def json_response(params)
|
14
|
-
hsh_params =
|
31
|
+
hsh_params = @params
|
15
32
|
keys = hsh_params.reduce([]) do |result, (key, value)|
|
16
33
|
result << key
|
17
34
|
if value.is_a?(Hash)
|
@@ -19,7 +36,7 @@ class App
|
|
19
36
|
end
|
20
37
|
result
|
21
38
|
end.flatten
|
22
|
-
|
39
|
+
JSON.generate({
|
23
40
|
keys: keys,
|
24
41
|
fakeKey: true,
|
25
42
|
fake_key: false
|