Module: OliveBranchMiddlewareExtension

Included in:
OliveBranch::Middleware
Defined in:
lib/olive_branch_patch.rb

Overview

This will monkey patch olivebranch middleware 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.

Constant Summary collapse

VA_KEY_REGEX =
/("[^"]+VA[^"]*"):/

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/olive_branch_patch.rb', line 8

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

#un_camel_va_keys!(json) ⇒ Object (private)



26
27
28
29
30
31
32
33
34
# File 'lib/olive_branch_patch.rb', line 26

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