Class: OliveBranch::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/olive_branch/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, args = {}) ⇒ Middleware

Returns a new instance of Middleware.



49
50
51
52
53
54
55
56
57
58
59
60
# 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

Instance Method Details

#call(env) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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