Sha256: 05f90c8a1dd2d49fd5edfbae74df69cb3204fe575701b77b4c92b2a68decde8a

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

require 'volt'
require 'rack' if RUBY_PLATFORM != 'opal'

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

4 entries across 4 versions & 1 rubygems

Version Path
volt-0.9.3.pre3 lib/volt/server/rack/http_request.rb
volt-0.9.3.pre2 lib/volt/server/rack/http_request.rb
volt-0.9.3.pre1 lib/volt/server/rack/http_request.rb
volt-0.9.2 lib/volt/server/rack/http_request.rb