olive_branch 2.1.1 → 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -2
- data/lib/olive_branch/middleware.rb +33 -8
- data/lib/olive_branch/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af62cb65fbea81bf6ad9a11e59412a2b8a834c94
|
4
|
+
data.tar.gz: 716f051af8d506fb0ebd1d9091c532654209baf6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 833e3fb79c0f420ab19feb5b58b5b696671e755ced9d1c52556be334e4f2f0a1345118cede1fb18f1cd77a3c75e9b2a1a097b246362b996d6335649e4ee105a3
|
7
|
+
data.tar.gz: 775099adb481be8ab774b68e2a8eb1abeb01e7084304d879df953d6616d637701e35f1b15afa3b96ceb3085531f24630041ddf84f4ed11de963367f6ddbb7535
|
data/README.md
CHANGED
@@ -46,18 +46,38 @@ end
|
|
46
46
|
|
47
47
|
```
|
48
48
|
|
49
|
+
A default inflection can be specified so you don't have to include the `X-Key-Inflection` header on every request.
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
config.middleware.use OliveBranch::Middleware, inflection: 'camel'
|
53
|
+
```
|
54
|
+
|
49
55
|
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.
|
50
56
|
|
57
|
+
### Filtering
|
58
|
+
|
59
|
+
#### Content type
|
60
|
+
|
51
61
|
It is also possible to include a custom content type check in the same manner
|
52
62
|
|
53
63
|
```ruby
|
54
64
|
config.middleware.use OliveBranch::Middleware, content_type_check: -> (content_type) { content_type == "my/content-type" }
|
55
65
|
```
|
56
66
|
|
57
|
-
|
67
|
+
#### Excluding URLs
|
68
|
+
|
69
|
+
Additionally you can define a custom check by passing a proc
|
70
|
+
|
71
|
+
For params transforming
|
58
72
|
|
59
73
|
```ruby
|
60
|
-
config.middleware.use OliveBranch::Middleware,
|
74
|
+
config.middleware.use OliveBranch::Middleware, exclude_params: -> (env) { env['PATH_INFO'].match(/^\/do_not_transform/) }
|
75
|
+
```
|
76
|
+
|
77
|
+
Or response transforming
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
config.middleware.use OliveBranch::Middleware, exclude_response: -> (env) { env['PATH_INFO'].match(/^\/do_not_transform/) }
|
61
81
|
```
|
62
82
|
|
63
83
|
* * *
|
@@ -5,6 +5,10 @@ module OliveBranch
|
|
5
5
|
def self.content_type_check(content_type)
|
6
6
|
content_type =~ /application\/json/
|
7
7
|
end
|
8
|
+
|
9
|
+
def self.default_exclude(env)
|
10
|
+
false
|
11
|
+
end
|
8
12
|
end
|
9
13
|
|
10
14
|
class Transformations
|
@@ -43,18 +47,17 @@ module OliveBranch
|
|
43
47
|
@camelize = args[:camelize] || Transformations.method(:camelize)
|
44
48
|
@dasherize = args[:dasherize] || Transformations.method(:dasherize)
|
45
49
|
@content_type_check = args[:content_type_check] || Checks.method(:content_type_check)
|
50
|
+
@exclude_response = args[:exclude_response] || Checks.method(:default_exclude)
|
51
|
+
@exclude_params = args[:exclude_params] || Checks.method(:default_exclude)
|
46
52
|
@default_inflection = args[:inflection]
|
47
53
|
end
|
48
54
|
|
49
55
|
def call(env)
|
50
|
-
|
51
|
-
|
52
|
-
if inflection && @content_type_check.call(env["CONTENT_TYPE"])
|
53
|
-
Transformations.underscore_params(env)
|
54
|
-
end
|
56
|
+
Transformations.underscore_params(env) unless exclude_params?(env)
|
55
57
|
|
56
58
|
@app.call(env).tap do |_status, headers, response|
|
57
|
-
next
|
59
|
+
next if exclude_response?(env, headers)
|
60
|
+
|
58
61
|
response.each do |body|
|
59
62
|
begin
|
60
63
|
new_response = MultiJson.load(body)
|
@@ -62,7 +65,7 @@ module OliveBranch
|
|
62
65
|
next
|
63
66
|
end
|
64
67
|
|
65
|
-
Transformations.transform(new_response, inflection_method(
|
68
|
+
Transformations.transform(new_response, inflection_method(env))
|
66
69
|
|
67
70
|
body.replace(MultiJson.dump(new_response))
|
68
71
|
end
|
@@ -71,7 +74,29 @@ module OliveBranch
|
|
71
74
|
|
72
75
|
private
|
73
76
|
|
74
|
-
def
|
77
|
+
def exclude_params?(env)
|
78
|
+
exclude?(env, env["CONTENT_TYPE"], @exclude_params)
|
79
|
+
end
|
80
|
+
|
81
|
+
def exclude_response?(env, headers)
|
82
|
+
exclude?(env, headers["Content-Type"], @exclude_response)
|
83
|
+
end
|
84
|
+
|
85
|
+
def exclude?(env, content_type, block)
|
86
|
+
!inflection_type(env) || !valid_content_type?(content_type) || block.call(env)
|
87
|
+
end
|
88
|
+
|
89
|
+
def valid_content_type?(content_type)
|
90
|
+
@content_type_check.call(content_type)
|
91
|
+
end
|
92
|
+
|
93
|
+
def inflection_type(env)
|
94
|
+
env["HTTP_X_KEY_INFLECTION"] || @default_inflection
|
95
|
+
end
|
96
|
+
|
97
|
+
def inflection_method(env)
|
98
|
+
inflection = inflection_type(env)
|
99
|
+
|
75
100
|
if inflection == "camel"
|
76
101
|
@camelize
|
77
102
|
elsif inflection == "dash"
|
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.2
|
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:
|
12
|
+
date: 2018-01-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|