rack-jsonparser 0.1.1 → 0.1.2

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 +4 -4
  2. data/lib/rack/json_parser.rb +14 -2
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b9896202e4e62ec43e8ac5763b890d3285a57ba1
4
- data.tar.gz: 5a8b48301176d0944e77a9b5e9abf41aca1b7840
3
+ metadata.gz: fb813b2a4e003337017e6e1a03532b7d7d7ce300
4
+ data.tar.gz: 2d3c7a0008ea0afc5518d17dd65acbb497be0667
5
5
  SHA512:
6
- metadata.gz: a99240a7520e3d98d32e6bf4900b638607d47b9a95d81444158221b3203b4630c88de0dd83d65fd2ad52c4a7686fc3a74aa091893ae67d0cc98e514792a42cdf
7
- data.tar.gz: 4fb7593dfa7522f049f6c3db7cb07bb993ffb6ba0a3c8aaf1033f34658af6d9c20ad4b2862f462a969a8e1b5f876b73219ffcacb45c785313492b25b13f1b8d1
6
+ metadata.gz: eb2a8f12b356b2e487f2c73647d90d13e916d64bce2db55d4e7357b775ef220a553f6adafa7730fc904291b1f28337ceb0c18ec1b33c53d2dad1b9fa61e2f3be
7
+ data.tar.gz: c825ba038737d3309ff29c72ff391099a8623a0b5de9a162aa659a6ef1620fbb3e0883fc7dfdb71f1ec5511991c088183ac37e387d387cefa9197fb88aa1c2a3
@@ -10,6 +10,9 @@ module Rack
10
10
  class JSONParser
11
11
  ENV_PAYLOAD_KEY = 'payload'.freeze
12
12
 
13
+ # Called via the rack `use` method. Used to register the middleware and
14
+ # optionally toggle request and response processing of JSON to Object.
15
+ # By default both the request and response is processed and transformed.
13
16
  def initialize(app, transform_request: true, transform_response: true)
14
17
  @app = app
15
18
  @transform_request = transform_request
@@ -19,6 +22,8 @@ module Rack
19
22
  # Loads the request JSON string into a Hash instance.
20
23
  # Expects the app response body to be an object instance e.g. Hash,
21
24
  # putting the object in an array will likely cause unexpected JSON.
25
+ # If the response body is processed then the `CONTENT_LENGTH` header will
26
+ # be set to the body.length.
22
27
  def call(env)
23
28
  if transform_request?(env)
24
29
  env[ENV_PAYLOAD_KEY] = Oj.load(env['rack.input'])
@@ -26,7 +31,7 @@ module Rack
26
31
 
27
32
  status, headers, body = @app.call(env)
28
33
 
29
- if body && transform_response?(headers)
34
+ if transform_response?(headers, body)
30
35
  body = Oj.dump(body)
31
36
  headers['CONTENT_LENGTH'] = body.length.to_s
32
37
  body = [body] unless body.is_a?(Array)
@@ -37,6 +42,9 @@ module Rack
37
42
 
38
43
  private
39
44
 
45
+ # Determine whether or not to transform the JSON request from the client
46
+ # into an Object. Takes into account the `@transform_request` variable and
47
+ # request parameters such as headers and request body.
40
48
  def transform_request?(env)
41
49
  @transform_request &&
42
50
  env['CONTENT_TYPE'] == 'application/json' &&
@@ -44,9 +52,13 @@ module Rack
44
52
  true # so the return value is true if all prior conditions are true
45
53
  end
46
54
 
47
- def transform_response?(headers)
55
+ # Determine whether or not to transform the JSON response from the app
56
+ # into a JSON string. Takes into account the `@transform_response` variable
57
+ # and response parameters such as headers and response body.
58
+ def transform_response?(headers, body)
48
59
  @transform_response &&
49
60
  headers['CONTENT_TYPE'] == 'application/json' &&
61
+ body &&
50
62
  true # so the return value is true if all prior conditions are true
51
63
  end
52
64
  end
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.1
4
+ version: 0.1.2
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-21 00:00:00.000000000 Z
11
+ date: 2017-10-28 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.