-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy patholive_branch_patch.rb
41 lines (34 loc) · 1.25 KB
/
olive_branch_patch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true
# This will monkey patch olivebranch middleware https://github.com/vigetlabs/olive_branch/blob/master/lib/olive_branch/middleware.rb
# so that when the VA inflection changes a key like something_va_something, the results will
# have the old-inflection somethingVaSomething.
module OliveBranchMiddlewareExtension
def call(env)
result = super(env)
_status, headers, response = result
# olive branch uses this private method to determine if a tranformation should happen https://github.com/vigetlabs/olive_branch/blob/master/lib/olive_branch/middleware.rb#L92
return result if send(:exclude_response?, env, headers)
if env['HTTP_X_KEY_INFLECTION'] =~ /camel/i
response.each do |json|
un_camel_va_keys!(json)
end
end
result
end
private
VA_KEY_REGEX = /("[^"]+VA[^"]*"):/
def un_camel_va_keys!(json)
# rubocop:disable Style/PerlBackrefs
# gsub with a block explicitly sets backrefs correctly https://ruby-doc.org/core-2.6.6/String.html#method-i-gsub
json.gsub!(VA_KEY_REGEX) do
key = $1
"#{key.gsub('VA', 'Va')}:"
end
# rubocop:enable Style/PerlBackrefs
end
end
module OliveBranch
class Middleware
prepend OliveBranchMiddlewareExtension
end
end