Sha256: dd5ad50cbd1d9e9e0a4b4e32c53704c1258e6a69fa34066e9fdef25f0c23382f

Contents?: true

Size: 1.22 KB

Versions: 12

Compression:

Stored size: 1.22 KB

Contents

require 'volt'
require 'rack'

module Volt
  # A request object for a HttpController. See Rack::Request for more details
  class HttpRequest < Rack::Request
    # Returns the request format
    # /blub/index.html => html
    # Defaults to the media_type of the request
    def format
      path_format || media_type
    end

    # Returns the path_info without the format
    # /blub/index.html => /blub/index
    def path
      path_format ? path_info[0..path_format.size * -1 - 2] : path_info
    end

    # Returns the request method
    # Allows requests to override the http request method by setting _method
    def method
      if params[:_method]
        params[:_method].to_s.downcase.to_sym
      else
        request_method.downcase.to_sym
      end
    end

    # The request params with symbolized keys
    def params
      super.symbolize_keys
    end

    private

    # Returns the format given in the path_info
    # http://example.com/test.html => html
    # http://example.com/test => nil
    def path_format
      @path_format ||= extract_format_from_path
    end

    # Extract from the path
    def extract_format_from_path
      format = path_info.match(/\.(\w+)$/)
      format.present? ? format[1] : nil
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
volt-0.9.1.pre1 lib/volt/server/rack/http_request.rb
volt-0.9.0 lib/volt/server/rack/http_request.rb
volt-0.9.0.pre7 lib/volt/server/rack/http_request.rb
volt-0.9.0.pre6 lib/volt/server/rack/http_request.rb
volt-0.9.0.pre5 lib/volt/server/rack/http_request.rb
volt-0.9.0.pre4 lib/volt/server/rack/http_request.rb
volt-0.9.0.pre3 lib/volt/server/rack/http_request.rb
volt-0.9.0.pre2 lib/volt/server/rack/http_request.rb
volt-0.9.0.pre1 lib/volt/server/rack/http_request.rb
volt-0.8.27.beta9 lib/volt/server/rack/http_request.rb
volt-0.8.27.beta8 lib/volt/server/rack/http_request.rb
volt-0.8.27.beta7 lib/volt/server/rack/http_request.rb