Class: Grape::Middleware::Base

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

Constant Summary collapse

TEXT_HTML =
'text/html'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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) (defaults to: {})

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



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

def initialize(app, options = {})
  @app = app
  @options = default_options.merge(options)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



4
5
6
# File 'lib/grape/middleware/base.rb', line 4

def app
  @app
end

#envObject (readonly)

Returns the value of attribute env.



4
5
6
# File 'lib/grape/middleware/base.rb', line 4

def env
  @env
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/grape/middleware/base.rb', line 4

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.



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

def after
end

#beforeObject

This method is abstract.

Called before the application is called in the middleware lifecycle.



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

def before
end

#call(env) ⇒ Object



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

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

#call!(env) ⇒ Object



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

def call!(env)
  @env = env
  before
  @app_response = @app.call(@env)
  after || @app_response
end

#content_typeObject



53
54
55
# File 'lib/grape/middleware/base.rb', line 53

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

#content_type_for(format) ⇒ Object



45
46
47
# File 'lib/grape/middleware/base.rb', line 45

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

#content_typesObject



49
50
51
# File 'lib/grape/middleware/base.rb', line 49

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

#default_optionsObject



14
15
16
# File 'lib/grape/middleware/base.rb', line 14

def default_options
  {}
end

#mime_typesObject



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

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

#responseObject



40
41
42
43
# File 'lib/grape/middleware/base.rb', line 40

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