json_key_transformer_middleware 0.1.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/json_key_transformer_middleware/outgoing_json_formatter.rb +7 -3
- data/readme.md +35 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77715b1456ce9b3198f5532abdf6d48841756382
|
4
|
+
data.tar.gz: 0a5490b1e2a8acbc377f13b6f193d942859ee472
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f32752b22f280e93d4bef5269943c6ce524fbae82120e8c86d4c25ac098b2231bf2d486cd8ac2fc866bdf87fe445496cdfc8e0d4229d8f3d8fa4db6fdbaed35
|
7
|
+
data.tar.gz: 70ce636d6f001a75c80c681db263dff9c4bcbb5a242e6ced66d4ead38b2565aff904d2889f96e6566ef18ee4700499e2ef30d4b4b035dffd81970a1c31e91c55
|
@@ -12,9 +12,13 @@ module JsonKeyTransformerMiddleware
|
|
12
12
|
status, headers, body = @app.call(env)
|
13
13
|
|
14
14
|
new_body = body.each.map do |body_part|
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
begin
|
16
|
+
Oj.dump(
|
17
|
+
deep_transform_hash_keys(
|
18
|
+
Oj.load(body_part), :underscore_to_camel))
|
19
|
+
rescue
|
20
|
+
body_part
|
21
|
+
end
|
18
22
|
end
|
19
23
|
|
20
24
|
[status, headers, new_body]
|
data/readme.md
CHANGED
@@ -22,6 +22,41 @@ Or, install it directly:
|
|
22
22
|
|
23
23
|
Simply including the gem in the Gemfile for a Rails application will initialize the rack middleware which transforms incoming and outgoing JSON keys.
|
24
24
|
|
25
|
+
The purpose of this gem is to:
|
26
|
+
|
27
|
+
1. Transform incoming parameter names from JSON-style `camelCase` to Ruby-style `snake_case`. Clients send `camelCase` parameters, you work with `snake_case` params in your Rails controllers.
|
28
|
+
1. Transform outgoing parameter names from Ruby-style `snake_case` to JSON-style `camelCase`. When rendering a JSON response, you send a `snake_case` hash response, the client will receive a `camelCase` response.
|
29
|
+
|
30
|
+
In both cases the transformation process walks the entire incoming and outgoing data structure.
|
31
|
+
|
32
|
+
*Incoming transformation example*
|
33
|
+
|
34
|
+
From (JSON):
|
35
|
+
|
36
|
+
```
|
37
|
+
{"paramName": "value", "paramName2": [{"paramName3": "value"}]}
|
38
|
+
```
|
39
|
+
|
40
|
+
To (Rails params):
|
41
|
+
|
42
|
+
```
|
43
|
+
{"param_name" => "value", "param_name2" => [{"param_name3" => "value"}]}
|
44
|
+
```
|
45
|
+
|
46
|
+
*Outgoing transformation example*
|
47
|
+
|
48
|
+
From (Rails response):
|
49
|
+
|
50
|
+
```
|
51
|
+
{"param_name" => "value", "param_name2" => [{"param_name3" => "value"}]}
|
52
|
+
```
|
53
|
+
|
54
|
+
To (JSON):
|
55
|
+
|
56
|
+
```
|
57
|
+
{"paramName": "value", "paramName2": [{"paramName3": "value"}]}
|
58
|
+
```
|
59
|
+
|
25
60
|
## Notes
|
26
61
|
|
27
62
|
This gem serves a very specific purpose. As such, it only targets a specific version of Rails. Over time more versions of Rails may be supported.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_key_transformer_middleware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Rood
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|
@@ -42,14 +42,14 @@ dependencies:
|
|
42
42
|
name: rails
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: 5.0.0.beta1
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 5.0.0.beta1
|
55
55
|
description: Incoming JSON keys are transformed from camelCase to snake_case. Outgoing
|