Sha256: afab09755b163607204027175b19ca2219e6ee5217e9a4ac33651585f753cd25

Contents?: true

Size: 918 Bytes

Versions: 2

Compression:

Stored size: 918 Bytes

Contents

require "rack/read_only/version"

module Rack
  class ReadOnly
    READ_METHODS = %w(HEAD GET OPTIONS)
    WRITE_METHODS = %w(POST PUT DELETE PATCH)
    ALL_METHODS = READ_METHODS + WRITE_METHODS

    attr_reader :app, :options

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

    def active?
      options[:active]
    end

    def response_body
      options[:response_body]
    end

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

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

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-read_only-2.0.0 lib/rack/read_only.rb
rack-read_only-1.0.1 lib/rack/read_only.rb