Class: Grape::Middleware::Base

Inherits:
Object
  • Object
show all
Includes:
DSL::Headers, Helpers
Defined in:
lib/grape/middleware/base.rb

Constant Summary collapse

TEXT_HTML =
'text/html'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DSL::Headers

#header

Methods included from Helpers

#context

Constructor Details

#initialize(app, **options) ⇒ Base

Returns a new instance of Base.

Parameters:

  • app (Rack Application)

    The standard argument for a Rack middleware.

  • options (Hash)

    A hash of options, simply stored for use by subclasses.


17
18
19
20
21
# File 'lib/grape/middleware/base.rb', line 17

def initialize(app, **options)
  @app = app
  @options = default_options.merge(options)
  @app_response = nil
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app


10
11
12
# File 'lib/grape/middleware/base.rb', line 10

def app
  @app
end

#envObject (readonly)

Returns the value of attribute env


10
11
12
# File 'lib/grape/middleware/base.rb', line 10

def env
  @env
end

#optionsObject (readonly)

Returns the value of attribute options


10
11
12
# File 'lib/grape/middleware/base.rb', line 10

def options
  @options
end

Instance Method Details

#afterResponse?

This method is abstract.

Called after the application is called in the middleware lifecycle.

Returns:

  • (Response, nil)

    a Rack SPEC response or nil to call the application afterwards.


57
# File 'lib/grape/middleware/base.rb', line 57

def after; end

#beforeObject

This method is abstract.

Called before the application is called in the middleware lifecycle.


52
# File 'lib/grape/middleware/base.rb', line 52

def before; end

#call(env) ⇒ Object


27
28
29
# File 'lib/grape/middleware/base.rb', line 27

def call(env)
  dup.call!(env).to_a
end

#call!(env) ⇒ Object


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/grape/middleware/base.rb', line 31

def call!(env)
  @env = env
  before
  begin
    @app_response = @app.call(@env)
  ensure
    begin
      after_response = after
    rescue StandardError => e
      warn "caught error of type #{e.class} in after callback inside #{self.class.name} : #{e.message}"
      raise e
    end
  end

  response = after_response || @app_response
  merge_headers response
  response
end

#content_typeObject


72
73
74
# File 'lib/grape/middleware/base.rb', line 72

def content_type
  content_type_for(env[Grape::Env::API_FORMAT] || options[:format]) || TEXT_HTML
end

#content_type_for(format) ⇒ Object


64
65
66
# File 'lib/grape/middleware/base.rb', line 64

def content_type_for(format)
  HashWithIndifferentAccess.new(content_types)[format]
end

#content_typesObject


68
69
70
# File 'lib/grape/middleware/base.rb', line 68

def content_types
  ContentTypes.content_types_for(options[:content_types])
end

#default_optionsObject


23
24
25
# File 'lib/grape/middleware/base.rb', line 23

def default_options
  {}
end

#mime_typesObject


76
77
78
79
80
# File 'lib/grape/middleware/base.rb', line 76

def mime_types
  @mime_type ||= content_types.each_pair.with_object({}) do |(k, v), types_without_params|
    types_without_params[v.split(';').first] = k
  end
end

#responseObject


59
60
61
62
# File 'lib/grape/middleware/base.rb', line 59

def response
  return @app_response if @app_response.is_a?(Rack::Response)
  Rack::Response.new(@app_response[2], @app_response[0], @app_response[1])
end