rack-json 0.5.1
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 +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +7 -0
- data/lib/rack-json.rb +1 -0
- data/lib/rack/json.rb +47 -0
- data/lib/rack/json/version.rb +5 -0
- data/rack-json.gemspec +24 -0
- data/spec/rack/json_spec.rb +81 -0
- data/spec/spec_helper.rb +4 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 841d2fae48460a3a2c9886fbda48c5f7973ffeee
|
4
|
+
data.tar.gz: cac44ccb71e5d2dd221ade98547cc7572f706f5d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fc17e1206ee42e1d29400d3320de7e9e1385538af008f450cf46ca35520154cc451cec3834bd47d7c542b0058627c68fde90278ef4451d0d57d9d2518cda2a4f
|
7
|
+
data.tar.gz: 886f1e2283cc0a23dd61de21d3129825e95c8cfdc791cf5f3a863009b60c73178c25b288d63179b51c7571469947e688108cc835005cb7325280b45f82f05b25
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jason Staten
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Rack::Json
|
2
|
+
|
3
|
+
Rack middleware for parsing JSON requests.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rack-json'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rack-json
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Add it to your config.ru
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
use Rack::JSON
|
25
|
+
end
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/rack-json.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rack/json'
|
data/lib/rack/json.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "rack/json/version"
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class JSON
|
5
|
+
|
6
|
+
APPLICATION_JSON = 'application/json'.freeze
|
7
|
+
FORM_HASH = 'rack.request.form_hash'.freeze
|
8
|
+
FORM_INPUT = 'rack.request.form_input'.freeze
|
9
|
+
|
10
|
+
# Override to use a different parser
|
11
|
+
def parse_json(json)
|
12
|
+
::JSON.parse(json, :create_additions => false)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Override to use a different parser
|
16
|
+
def json_parser_error_class
|
17
|
+
::JSON::ParserError
|
18
|
+
end
|
19
|
+
|
20
|
+
# Override for different error behavior
|
21
|
+
def on_parse_error(e)
|
22
|
+
message = "Invalid JSON"
|
23
|
+
[400, {'Content-Length' => message.length}, [message]]
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(app)
|
27
|
+
@app = app
|
28
|
+
end
|
29
|
+
|
30
|
+
def call(env)
|
31
|
+
request = Rack::Request.new env
|
32
|
+
|
33
|
+
if request.media_type == APPLICATION_JSON && !(body = request.body.read).empty?
|
34
|
+
request.body.rewind
|
35
|
+
parsed = parse_json body
|
36
|
+
parsed = {'_json' => parsed} unless parsed.is_a? Hash
|
37
|
+
|
38
|
+
env.update FORM_HASH => parsed, FORM_INPUT => request.body
|
39
|
+
end
|
40
|
+
|
41
|
+
@app.call env
|
42
|
+
|
43
|
+
rescue json_parser_error_class => e
|
44
|
+
on_parse_error e
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/rack-json.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rack/json/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rack-json"
|
8
|
+
spec.version = Rack::Json::VERSION
|
9
|
+
spec.authors = ["Jason Staten"]
|
10
|
+
spec.email = ["jstaten07@gmail.com"]
|
11
|
+
spec.description = %q{Rack middleware for parsing json}
|
12
|
+
spec.summary = %q{Rack middleware for parsing json}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rack"
|
24
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Rack::JSON do
|
5
|
+
let(:app) { MiniTest::Mock.new }
|
6
|
+
let(:input) { "foo=bar" }
|
7
|
+
let(:content_type) { "application/x-www-form-url-encoded" }
|
8
|
+
let(:env) { Rack::MockRequest.env_for "/", :method => 'POST', :input => input, 'CONTENT_TYPE' => content_type }
|
9
|
+
|
10
|
+
before do
|
11
|
+
app.expect :call, [200, {}, []], [env]
|
12
|
+
end
|
13
|
+
|
14
|
+
subject { Rack::JSON.new app }
|
15
|
+
|
16
|
+
it 'calls app' do
|
17
|
+
subject.call env
|
18
|
+
app.verify
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'doesnt read body by default' do
|
22
|
+
env['rack.input'] = MiniTest::Mock.new
|
23
|
+
subject.call env
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'application/json requests' do
|
27
|
+
let(:content_type) { 'application/json; charset=utf-8' }
|
28
|
+
let(:params) { Rack::Request.new(env).POST }
|
29
|
+
|
30
|
+
it 'rewinds input' do
|
31
|
+
subject.call env
|
32
|
+
Rack::Request.new(env).body.read.must_equal input
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'when input is a hash' do
|
36
|
+
let(:input) { '{"qux": "bin"}' }
|
37
|
+
|
38
|
+
it 'adds a parsed hash to POST params' do
|
39
|
+
subject.call env
|
40
|
+
params['qux'].must_equal 'bin'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe 'when input is an array' do
|
45
|
+
let(:input) { '["a", "b", "c"]' }
|
46
|
+
|
47
|
+
it 'adds a parsed array to POST params as _json' do
|
48
|
+
subject.call env
|
49
|
+
params['_json'].must_equal ['a', 'b', 'c']
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe 'when input has a json_class' do
|
54
|
+
let(:input) { '{"json_class": "JSON::GenericObject", "key": "val" }' }
|
55
|
+
|
56
|
+
it 'does not create additions' do
|
57
|
+
subject.call env
|
58
|
+
params['json_class'].must_equal 'JSON::GenericObject'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe 'when input is invalid' do
|
63
|
+
let(:input) { '{"json_class": ' }
|
64
|
+
|
65
|
+
it 'responds with a bad request message' do
|
66
|
+
result = subject.call env
|
67
|
+
response = Rack::MockResponse.new(*result)
|
68
|
+
response.must_be :bad_request?
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe 'when input is empty' do
|
73
|
+
let(:input) { '' }
|
74
|
+
|
75
|
+
it 'calls app' do
|
76
|
+
subject.call env
|
77
|
+
app.verify
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-json
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jason Staten
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rack
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Rack middleware for parsing json
|
56
|
+
email:
|
57
|
+
- jstaten07@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/rack-json.rb
|
68
|
+
- lib/rack/json.rb
|
69
|
+
- lib/rack/json/version.rb
|
70
|
+
- rack-json.gemspec
|
71
|
+
- spec/rack/json_spec.rb
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
homepage: ''
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.0.3
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Rack middleware for parsing json
|
97
|
+
test_files:
|
98
|
+
- spec/rack/json_spec.rb
|
99
|
+
- spec/spec_helper.rb
|