olive_branch 2.1.2 → 2.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -9
- data/lib/olive_branch/middleware.rb +15 -11
- data/lib/olive_branch/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a07c5fbe243ac3032362eea45fa6d25c69e407f
|
4
|
+
data.tar.gz: 2977031f9cdf6abd823f21974e4647054f73440d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c01b70ec3056840cfd42a429a81656372106251dc568fde88cf561796b132d794b62ac75c74c3808a185bfa0d4f4a76771ac558f4d468560d17653216d03776b
|
7
|
+
data.tar.gz: b7e9938aec19f96a77d73a3ba64a394cd58e201ac40a5e3036fceecfc3c55b54098bff659aae9df6fb8d8c03bb860ce5b86a0ec330b73482ce52be2a921181c9
|
data/README.md
CHANGED
@@ -9,17 +9,20 @@ This gem lets your API users pass in and receive camelCased or dash-cased keys,
|
|
9
9
|
|
10
10
|
1. Add this to your Gemfile and then `bundle install`:
|
11
11
|
|
12
|
-
|
12
|
+
```ruby
|
13
|
+
gem "olive_branch"
|
14
|
+
```
|
13
15
|
|
14
16
|
2. Add this to `config/applcation.rb`:
|
15
17
|
|
16
|
-
|
18
|
+
```ruby
|
19
|
+
config.middleware.use OliveBranch::Middleware
|
20
|
+
```
|
17
21
|
|
18
22
|
## Use
|
19
23
|
|
20
24
|
Include a `X-Key-Inflection` header with values of `camel`, `dash`, or `snake` in your JSON API requests.
|
21
25
|
|
22
|
-
|
23
26
|
For more examples, see [our blog post](https://www.viget.com/articles/introducing-olivebranch).
|
24
27
|
|
25
28
|
## Optimizations and configuration
|
@@ -42,14 +45,13 @@ end
|
|
42
45
|
|
43
46
|
...
|
44
47
|
|
45
|
-
|
46
|
-
|
48
|
+
config.middleware.use OliveBranch::Middleware, camelize: FastCamel.method(:camelize)
|
47
49
|
```
|
48
50
|
|
49
51
|
A default inflection can be specified so you don't have to include the `X-Key-Inflection` header on every request.
|
50
52
|
|
51
53
|
```ruby
|
52
|
-
|
54
|
+
config.middleware.use OliveBranch::Middleware, inflection: 'camel'
|
53
55
|
```
|
54
56
|
|
55
57
|
A benchmark of this compared to the standard implementation shows a saving of ~75% rails response times for a complex response payload, or a ~400% improvement, but there is a risk of memory usage ballooning if you have dynamic keys. You can make this method as complex as required, but keep in mind that it will end up being called a _lot_ in a busy app, so it's worth thinking about how to do what you need in the fastest manner possible.
|
@@ -61,7 +63,9 @@ A benchmark of this compared to the standard implementation shows a saving of ~7
|
|
61
63
|
It is also possible to include a custom content type check in the same manner
|
62
64
|
|
63
65
|
```ruby
|
64
|
-
|
66
|
+
config.middleware.use OliveBranch::Middleware, content_type_check: -> (content_type) {
|
67
|
+
content_type == "my/content-type"
|
68
|
+
}
|
65
69
|
```
|
66
70
|
|
67
71
|
#### Excluding URLs
|
@@ -71,13 +75,25 @@ Additionally you can define a custom check by passing a proc
|
|
71
75
|
For params transforming
|
72
76
|
|
73
77
|
```ruby
|
74
|
-
|
78
|
+
config.middleware.use OliveBranch::Middleware, exclude_params: -> (env) {
|
79
|
+
env['PATH_INFO'].match(/^\/do_not_transform/)
|
80
|
+
}
|
75
81
|
```
|
76
82
|
|
77
83
|
Or response transforming
|
78
84
|
|
79
85
|
```ruby
|
80
|
-
|
86
|
+
config.middleware.use OliveBranch::Middleware, exclude_response: -> (env) {
|
87
|
+
env['PATH_INFO'].match(/^\/do_not_transform/)
|
88
|
+
}
|
89
|
+
```
|
90
|
+
|
91
|
+
## Troubleshooting
|
92
|
+
|
93
|
+
We've seen a few folks raise issues that inbound transformations are not taking place. This is often due to the fact that OliveBranch, by default, is only transforming keys when a request's Content-Type is `application/json`. If you would like to force inbound transformation on every request, you must define an override for the `content_type_check` functionality:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
config.middleware.use OliveBranch::Middleware, content_type_check: -> (content_type) { true }
|
81
97
|
```
|
82
98
|
|
83
99
|
* * *
|
@@ -54,22 +54,26 @@ module OliveBranch
|
|
54
54
|
|
55
55
|
def call(env)
|
56
56
|
Transformations.underscore_params(env) unless exclude_params?(env)
|
57
|
+
status, headers, response = @app.call(env)
|
57
58
|
|
58
|
-
|
59
|
-
next if exclude_response?(env, headers)
|
59
|
+
return [status, headers, response] if exclude_response?(env, headers)
|
60
60
|
|
61
|
-
|
62
|
-
begin
|
63
|
-
new_response = MultiJson.load(body)
|
64
|
-
rescue MultiJson::ParseError
|
65
|
-
next
|
66
|
-
end
|
61
|
+
new_responses = []
|
67
62
|
|
68
|
-
|
69
|
-
|
70
|
-
|
63
|
+
response.each do |body|
|
64
|
+
begin
|
65
|
+
new_response = MultiJson.load(body)
|
66
|
+
rescue MultiJson::ParseError
|
67
|
+
new_responses << body
|
68
|
+
next
|
71
69
|
end
|
70
|
+
|
71
|
+
Transformations.transform(new_response, inflection_method(env))
|
72
|
+
|
73
|
+
new_responses << MultiJson.dump(new_response)
|
72
74
|
end
|
75
|
+
|
76
|
+
[status, headers, new_responses]
|
73
77
|
end
|
74
78
|
|
75
79
|
private
|
data/lib/olive_branch/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: olive_branch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eli Fatsi
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-
|
12
|
+
date: 2018-08-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -142,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
142
|
version: '0'
|
143
143
|
requirements: []
|
144
144
|
rubyforge_project:
|
145
|
-
rubygems_version: 2.5.
|
145
|
+
rubygems_version: 2.5.2
|
146
146
|
signing_key:
|
147
147
|
specification_version: 4
|
148
148
|
summary: Handle camel/snake/dash case conversion
|