rack-jsonparser 0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rack/json_parser.rb +53 -0
  3. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8fc14e3e1e363152ec7848e6894c1642e6afa970
4
+ data.tar.gz: f0c9b28330554052704a816c4166e6f68e5bfb2f
5
+ SHA512:
6
+ metadata.gz: 409d62951bb8fa5b66fa43bdf865c75f1b0ba232b89fd1492e52ef4d5db93ba2b91cef4e711b6e97b19508b82337343bd694ca24bd677090660490efcac28178
7
+ data.tar.gz: 2742bb8b49dd20f8f9a2544194d4d8275b6b339434ae5bb62f8661988722bf3f1ed3293f648a9665bf741e0c9f842eb9ddaa3c45a6d94d967a1d96da4619f0c1
@@ -0,0 +1,53 @@
1
+ require 'oj'
2
+
3
+ module Rack
4
+ # Rack middleware which transforms JSON during the request and response.
5
+ # Configurable so request/response processing can be turned off if desired.
6
+ # Only transforms if the Content-Type header is set and includes
7
+ # 'application/json'. This allows for several types of response content to be
8
+ # served by the application without interference by this middleware. This
9
+ # however requires the client and app to set the 'Content-Type' correctly.
10
+ class JSONParser
11
+ ENV_PAYLOAD_KEY = 'payload'
12
+
13
+ def initialize(app, transform_request: true, transform_response: true)
14
+ @app = app
15
+ @transform_request = transform_request
16
+ @transform_response = transform_response
17
+ end
18
+
19
+ def call(env)
20
+ # load the json string into a Hash instance
21
+ if transform_request?(env)
22
+ env[ENV_PAYLOAD_KEY] = Oj.load(env['rack.input'])
23
+ end
24
+
25
+ status, headers, body = @app.call(env)
26
+
27
+ 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
+ body = Oj.dump(body)
31
+ headers['CONTENT_LENGTH'] = body.length.to_s
32
+ body = [body] unless body.is_a?(Array)
33
+ end
34
+
35
+ [status, headers, body]
36
+ end
37
+
38
+ private
39
+
40
+ def transform_request?(env)
41
+ @transform_request &&
42
+ env['CONTENT_TYPE'] == 'application/json' &&
43
+ env['rack.input'] &&
44
+ true # so the return value is true if all prior conditions are true
45
+ end
46
+
47
+ def transform_response?(headers)
48
+ @transform_response &&
49
+ headers['CONTENT_TYPE'] == 'application/json' &&
50
+ true # so the return value is true if all prior conditions are true
51
+ end
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-jsonparser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Telford
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Rack middleware for processing JSON requests and responses using the
14
+ 'oj' gem.
15
+ email:
16
+ - michael.telford@live.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/rack/json_parser.rb
22
+ homepage: https://github.com/michaeltelford/rack-jsonparser
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.6.12
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Rack middleware for processing JSON requests and responses.
46
+ test_files: []