class Restfulness::Request

Simple, indpendent, request interface for dealing with the incoming information in a request.

Currently wraps around the information provided in a Rack Request object.

Attributes

action[RW]

The HTTP action being handled

app[R]

Who does this request belong to?

body[RW]

Raw HTTP body, for POST and PUT requests.

env[RW]

Expose rack env to interact with rack middleware

headers[RW]

Hash of HTTP headers. Keys always normalized to lower-case symbols with underscore.

remote_ip[RW]

Additional useful fields

uri[R]

Ruby URI object

user_agent[RW]

Additional useful fields

Public Class Methods

new(app) click to toggle source
# File lib/restfulness/request.rb, line 31
def initialize(app)
  @app = app

  # Prepare basics
  self.action  = nil
  self.headers = {}
  self.body    = nil
end

Public Instance Methods

accept() click to toggle source
# File lib/restfulness/request.rb, line 61
def accept
  if headers[:accept]
    @accept ||= Headers::Accept.new(headers[:accept])
  end
end
content_type() click to toggle source
# File lib/restfulness/request.rb, line 67
def content_type
  if headers[:content_type]
    @content_type ||= Headers::MediaType.new(headers[:content_type])
  end
end
http_accept_language() click to toggle source

Provide a wrapper for the http_accept_language parser

# File lib/restfulness/request.rb, line 97
def http_accept_language
  @http_accept_language = HttpAcceptLanguage::Parser.new(headers[:accept_language])
end
params() click to toggle source
# File lib/restfulness/request.rb, line 73
def params
  @params ||= begin
    data = body_to_string || ""
    if data.length > 0
      if content_type && content_type.json?
        params_from_json(data)
      elsif content_type && content_type.form?
        params_from_form(data)
      else
        # Body provided with no or invalid content type
        raise HTTPException.new(406)
      end
    else
      {}
    end
  end
end
path() click to toggle source
# File lib/restfulness/request.rb, line 44
def path
  @path ||= (route ? route.build_path(uri.path) : nil)
end
query() click to toggle source
# File lib/restfulness/request.rb, line 53
def query
  @query ||= ::Rack::Utils.parse_nested_query(uri.query).with_indifferent_access
end
route() click to toggle source
# File lib/restfulness/request.rb, line 48
def route
  # Determine the route from the uri
  @route ||= app.router.route_for(uri.path)
end
sanitized_params() click to toggle source
# File lib/restfulness/request.rb, line 91
def sanitized_params
  # Note: this returns nil if #params has not been called
  @sanitized_params ||= @params ? Sanitizer.sanitize_hash(@params) : nil
end
sanitized_query_string() click to toggle source
# File lib/restfulness/request.rb, line 57
def sanitized_query_string
  @sanitized_query ||= uri.query ? Sanitizer.sanitize_query_string(uri.query) : ''
end
uri=(uri) click to toggle source
# File lib/restfulness/request.rb, line 40
def uri=(uri)
  @uri = URI(uri)
end

Protected Instance Methods

body_to_string() click to toggle source
# File lib/restfulness/request.rb, line 109
def body_to_string
  unless body.nil?
    # Sometimes the body can be a StringIO, Tempfile, or some other freakish IO.
    if body.respond_to?(:read)
      read_body = body.read
      body.rewind if body.respond_to?(:rewind)
      read_body
    else
      body
    end
  else
    ""
  end
end
params_from_form(data) click to toggle source
# File lib/restfulness/request.rb, line 130
def params_from_form(data)
  Rack::Utils.parse_query(data)
end
params_from_json(data) click to toggle source
# File lib/restfulness/request.rb, line 124
def params_from_json(data)
  MultiJson.decode(data)
rescue MultiJson::LoadError
  raise HTTPException.new(400, "Invalid JSON in request body")
end