class OliveBranch::Middleware

Public Class Methods

new(app, args = {}) click to toggle source
# File lib/olive_branch/middleware.rb, line 49
def initialize(app, args = {})
  @app = app
  @camelize = args[:camelize] || Transformations.method(:camelize)
  @dasherize = args[:dasherize] || Transformations.method(:dasherize)
  @pascalize = args[:pascalize] || Transformations.method(:pascalize)
  @content_type_check = args[:content_type_check] || Checks.method(:content_type_check)
  @exclude_response = args[:exclude_response] || Checks.method(:default_exclude)
  @exclude_params = args[:exclude_params] || Checks.method(:default_exclude)
  @default_inflection = args[:inflection]
  @inflection_header = args.fetch(:inflection_header, 'Key-Inflection').gsub(/[^a-z0-9]/i, '_').upcase
  @inflection_header = "HTTP_#{@inflection_header}" unless @inflection_header.start_with?('HTTP_')
end

Public Instance Methods

call(env) click to toggle source
# File lib/olive_branch/middleware.rb, line 62
def call(env)
  Transformations.underscore_params(env) unless exclude_params?(env)
  status, headers, response = @app.call(env)

  return [status, headers, response] if exclude_response?(env, headers)

  new_responses = []

  response.each do |body|
    begin
      new_response = MultiJson.load(body)
    rescue MultiJson::ParseError
      new_responses << body
      next
    end

    Transformations.transform(new_response, inflection_method(env))

    new_responses << MultiJson.dump(new_response)
  end

  [status, headers, new_responses]
end

Private Instance Methods

exclude?(env, content_type, block) click to toggle source
# File lib/olive_branch/middleware.rb, line 97
def exclude?(env, content_type, block)
  !inflection_type(env) || !valid_content_type?(content_type) || block.call(env)
end
exclude_params?(env) click to toggle source
# File lib/olive_branch/middleware.rb, line 88
def exclude_params?(env)
  exclude?(env, env["CONTENT_TYPE"], @exclude_params)
end
exclude_rails_route?(env) click to toggle source
# File lib/olive_branch/middleware.rb, line 101
def exclude_rails_route?(env)
  env['PATH_INFO'].to_s.start_with?('/rails')
end
exclude_response?(env, headers) click to toggle source
# File lib/olive_branch/middleware.rb, line 92
def exclude_response?(env, headers)
  exclude_rails_route?(env) ||
    exclude?(env, headers['Content-Type'], @exclude_response)
end
inflection_method(env) click to toggle source
# File lib/olive_branch/middleware.rb, line 113
def inflection_method(env)
  inflection = inflection_type(env)

  if inflection == "camel"
    @camelize
  elsif inflection == "dash"
    @dasherize
  elsif inflection == 'pascal'
    @pascalize
  else
    # probably misconfigured, do nothing
    -> (string) { string }
  end
end
inflection_type(env) click to toggle source
# File lib/olive_branch/middleware.rb, line 109
def inflection_type(env)
  env[@inflection_header] || @default_inflection
end
valid_content_type?(content_type) click to toggle source
# File lib/olive_branch/middleware.rb, line 105
def valid_content_type?(content_type)
  @content_type_check.call(content_type)
end