rack-jsonparser 0.1.0 → 0.1.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 +4 -4
- data/README.md +108 -0
- data/lib/rack/json_parser.rb +5 -5
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9896202e4e62ec43e8ac5763b890d3285a57ba1
|
4
|
+
data.tar.gz: 5a8b48301176d0944e77a9b5e9abf41aca1b7840
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a99240a7520e3d98d32e6bf4900b638607d47b9a95d81444158221b3203b4630c88de0dd83d65fd2ad52c4a7686fc3a74aa091893ae67d0cc98e514792a42cdf
|
7
|
+
data.tar.gz: 4fb7593dfa7522f049f6c3db7cb07bb993ffb6ba0a3c8aaf1033f34658af6d9c20ad4b2862f462a969a8e1b5f876b73219ffcacb45c785313492b25b13f1b8d1
|
data/README.md
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
# Rack::JSONParser Middleware
|
2
|
+
|
3
|
+
Rack middleware which transforms an incoming request's JSON string into a `Hash` and any outgoing ruby `Object` back into a JSON string to be returned to the client.
|
4
|
+
|
5
|
+
- The client sends/receives only JSON strings
|
6
|
+
- The `app` (rack application) using the middleware sends/receives only ruby objects (no JSON parsing)
|
7
|
+
- The `Rack::JSONParser` middleware handles the parsing of the request/response to/from JSON/Object so you don't have to
|
8
|
+
|
9
|
+
The difference between this and other middleware doing the same job is that it's fast! This middleware uses the '[oj](https://github.com/ohler55/oj)' gem which gives faster processing than the `json` gem. This middleware is also configurable enabling predictive and controlled parsing. It stays out of your way when you don't need it and is seamless when you do.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's `Gemfile`:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'rack-jsonparser'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself with:
|
24
|
+
|
25
|
+
$ gem install rack-jsonparser
|
26
|
+
|
27
|
+
Then `require` it in your rack application with:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'rack/json_parser'
|
31
|
+
```
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
Below is a sample rack app which uses the middleware to send and receive JSON:
|
36
|
+
|
37
|
+
> config.ru
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
require 'rack/json_parser'
|
41
|
+
|
42
|
+
# Notice how the `payload` is a Hash, not a JSON string
|
43
|
+
# We return a Hash instance (or any Ruby object) for the response body
|
44
|
+
# We can turn off the request/response parsing via the `use` method (defaults to true)
|
45
|
+
# Parsing will only occur if enabled AND the Content-Type is `application/json`
|
46
|
+
handler = proc do |env|
|
47
|
+
payload = env['payload']
|
48
|
+
full_name = payload['forenames'].push(payload['surname']).join(' ')
|
49
|
+
res_hash = { 'full_name' => full_name }
|
50
|
+
[200, { 'CONTENT_TYPE' => 'application/json' }, res_hash]
|
51
|
+
end
|
52
|
+
|
53
|
+
app = Rack::Builder.new do
|
54
|
+
use Rack::JSONParser, transform_request: true, transform_response: true
|
55
|
+
map('/hello') { run handler }
|
56
|
+
end
|
57
|
+
|
58
|
+
run app
|
59
|
+
```
|
60
|
+
|
61
|
+
Run the above `config.ru` rack app with:
|
62
|
+
|
63
|
+
$ rackup
|
64
|
+
|
65
|
+
Then query the app with:
|
66
|
+
|
67
|
+
```shell
|
68
|
+
curl -X POST \
|
69
|
+
http://localhost:9292/hello \
|
70
|
+
-H 'content-type: application/json' \
|
71
|
+
-d '{
|
72
|
+
"forenames": [
|
73
|
+
"Napolean",
|
74
|
+
"Neech"
|
75
|
+
],
|
76
|
+
"surname":"Manly"
|
77
|
+
}'
|
78
|
+
```
|
79
|
+
|
80
|
+
And the JSON response will be:
|
81
|
+
|
82
|
+
```json
|
83
|
+
{
|
84
|
+
"full_name":"Napolean Neech Manly"
|
85
|
+
}
|
86
|
+
```
|
87
|
+
|
88
|
+
## Configuration
|
89
|
+
|
90
|
+
The middleware is configurable so request/response processing can be turned off via the `use` method parameters if desired, see usage above.
|
91
|
+
|
92
|
+
In addition, the middleware only transforms the request/response if the `Content-Type` header is correctly set to `application/json`. This applies to both the request sent from the client and the response sent from the `app`; allowing for several types of response content to be served by the application without interference by the middleware. This however requires the client and rack `app` to set the `Content-Type` header correctly. If not, then the middleware will do nothing and simply pass through the request/response data which might lead to unexpected behavior.
|
93
|
+
|
94
|
+
If the middleware processes the response then it will also set/override the `Content-Length` header with the length of the JSON string being returned to the client.
|
95
|
+
|
96
|
+
## Ruby Version
|
97
|
+
|
98
|
+
This software was built with and supports:
|
99
|
+
|
100
|
+
- `~> 2.4.0`
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/michaeltelford/rack_jsonparser).
|
105
|
+
|
106
|
+
## License
|
107
|
+
|
108
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/lib/rack/json_parser.rb
CHANGED
@@ -8,7 +8,7 @@ module Rack
|
|
8
8
|
# served by the application without interference by this middleware. This
|
9
9
|
# however requires the client and app to set the 'Content-Type' correctly.
|
10
10
|
class JSONParser
|
11
|
-
ENV_PAYLOAD_KEY = 'payload'
|
11
|
+
ENV_PAYLOAD_KEY = 'payload'.freeze
|
12
12
|
|
13
13
|
def initialize(app, transform_request: true, transform_response: true)
|
14
14
|
@app = app
|
@@ -16,8 +16,10 @@ module Rack
|
|
16
16
|
@transform_response = transform_response
|
17
17
|
end
|
18
18
|
|
19
|
+
# Loads the request JSON string into a Hash instance.
|
20
|
+
# Expects the app response body to be an object instance e.g. Hash,
|
21
|
+
# putting the object in an array will likely cause unexpected JSON.
|
19
22
|
def call(env)
|
20
|
-
# load the json string into a Hash instance
|
21
23
|
if transform_request?(env)
|
22
24
|
env[ENV_PAYLOAD_KEY] = Oj.load(env['rack.input'])
|
23
25
|
end
|
@@ -25,8 +27,6 @@ module Rack
|
|
25
27
|
status, headers, body = @app.call(env)
|
26
28
|
|
27
29
|
if body && transform_response?(headers)
|
28
|
-
# expects the body to be an object instance e.g. Hash
|
29
|
-
# putting the object in an array will cause unexpected json
|
30
30
|
body = Oj.dump(body)
|
31
31
|
headers['CONTENT_LENGTH'] = body.length.to_s
|
32
32
|
body = [body] unless body.is_a?(Array)
|
@@ -35,7 +35,7 @@ module Rack
|
|
35
35
|
[status, headers, body]
|
36
36
|
end
|
37
37
|
|
38
|
-
|
38
|
+
private
|
39
39
|
|
40
40
|
def transform_request?(env)
|
41
41
|
@transform_request &&
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-jsonparser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Telford
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-21 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Rack middleware for processing JSON requests and responses using the
|
14
14
|
'oj' gem.
|
@@ -18,6 +18,7 @@ executables: []
|
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
+
- README.md
|
21
22
|
- lib/rack/json_parser.rb
|
22
23
|
homepage: https://github.com/michaeltelford/rack-jsonparser
|
23
24
|
licenses:
|
@@ -29,9 +30,9 @@ require_paths:
|
|
29
30
|
- lib
|
30
31
|
required_ruby_version: !ruby/object:Gem::Requirement
|
31
32
|
requirements:
|
32
|
-
- - "
|
33
|
+
- - "~>"
|
33
34
|
- !ruby/object:Gem::Version
|
34
|
-
version:
|
35
|
+
version: 2.4.0
|
35
36
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
37
|
requirements:
|
37
38
|
- - ">="
|