Class: Rack::ReadOnly

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/read_only.rb,
lib/rack/read_only/version.rb

Constant Summary collapse

READ_METHODS =
%w(HEAD GET OPTIONS)
WRITE_METHODS =
%w(POST PUT DELETE PATCH)
ALL_METHODS =
READ_METHODS + WRITE_METHODS
VERSION =
"2.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ ReadOnly

Returns a new instance of ReadOnly.



11
12
13
14
# File 'lib/rack/read_only.rb', line 11

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

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



9
10
11
# File 'lib/rack/read_only.rb', line 9

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/rack/read_only.rb', line 9

def options
  @options
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/rack/read_only.rb', line 16

def active?
  options[:active]
end

#call(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rack/read_only.rb', line 34

def call(env)
  if active? && WRITE_METHODS.include?(env['REQUEST_METHOD'])
    response = Rack::Response.new(
      response_body,
      response_status,
      response_headers
    )

    return response
  end

  app.call(env)
end

#response_bodyObject



20
21
22
# File 'lib/rack/read_only.rb', line 20

def response_body
  options[:response_body]
end

#response_headersObject



28
29
30
31
32
# File 'lib/rack/read_only.rb', line 28

def response_headers
  options.fetch(:response_headers, {
    'Content-Type' => 'application/json'
  })
end

#response_statusObject



24
25
26
# File 'lib/rack/read_only.rb', line 24

def response_status
  options.fetch(:response_status, 503)
end