Class: Grape::Middleware::Base

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

Direct Known Subclasses

Auth::Base, Auth::OAuth2, Error, Filter, Formatter, Globals, Versioner::AcceptVersionHeader, Versioner::Header, Versioner::Param, Versioner::Path

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Base) initialize(app, options = {})

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.



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

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

Instance Attribute Details

- (Object) app (readonly)

Returns the value of attribute app



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

def app
  @app
end

- (Object) env (readonly)

Returns the value of attribute env



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

def env
  @env
end

- (Object) options (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

- (Response?) after

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.



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

def after
end

- (Object) before

This method is abstract.

Called before the application is called in the middleware lifecycle.



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

def before
end

- (Object) call(env)



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

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

- (Object) call!(env)



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

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

- (Object) content_type



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

def content_type
  content_type_for(env['api.format'] || options[:format]) || 'text/html'
end

- (Object) content_type_for(format)



43
44
45
# File 'lib/grape/middleware/base.rb', line 43

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

- (Object) content_types



47
48
49
# File 'lib/grape/middleware/base.rb', line 47

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

- (Object) default_options



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

def default_options
  {}
end

- (Object) mime_types



55
56
57
58
59
# File 'lib/grape/middleware/base.rb', line 55

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

- (Object) response



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

def response
  Rack::Response.new(@app_response)
end