Class: Grape::Middleware::Base

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

Constant Summary collapse

CONTENT_TYPES =

Content types are listed in order of preference.

ActiveSupport::OrderedHash[
  :xml,  'application/xml',
  :serializable_hash, 'application/json',
  :json, 'application/json',
  :atom, 'application/atom+xml',
  :rss,  'application/rss+xml',
  :txt,  'text/plain',
]

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.



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

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

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



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

def app
  @app
end

#envObject (readonly)

Returns the value of attribute env.



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

def env
  @env
end

#optionsObject (readonly)

Returns the value of attribute options.



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

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.



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

def after; end

#beforeObject

This method is abstract.

Called before the application is called in the middleware lifecycle.



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

def before; end

#call(env) ⇒ Object



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

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

#call!(env) ⇒ Object



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

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

#content_typeObject



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

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

#content_type_for(format) ⇒ Object



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

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

#content_typesObject



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

def content_types
  options[:content_types] || CONTENT_TYPES
end

#default_optionsObject



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

def default_options
  {}
end

#mime_typesObject



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

def mime_types
  content_types.invert
end

#requestObject



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

def request
  Rack::Request.new(self.env)
end

#responseObject



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

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