Class: Restfulness::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/restfulness/request.rb

Overview

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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Request

Returns a new instance of Request.



27
28
29
30
31
32
33
34
# File 'lib/restfulness/request.rb', line 27

def initialize(app)
  @app = app

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

Instance Attribute Details

#actionObject

The HTTP action being handled



13
14
15
# File 'lib/restfulness/request.rb', line 13

def action
  @action
end

#appObject (readonly)

Who does this request belong to?



10
11
12
# File 'lib/restfulness/request.rb', line 10

def app
  @app
end

#bodyObject

Raw HTTP body, for POST and PUT requests.



22
23
24
# File 'lib/restfulness/request.rb', line 22

def body
  @body
end

#headersObject

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



16
17
18
# File 'lib/restfulness/request.rb', line 16

def headers
  @headers
end

#remote_ipObject

Additional useful fields



25
26
27
# File 'lib/restfulness/request.rb', line 25

def remote_ip
  @remote_ip
end

#uriObject

Ruby URI object



19
20
21
# File 'lib/restfulness/request.rb', line 19

def uri
  @uri
end

#user_agentObject

Additional useful fields



25
26
27
# File 'lib/restfulness/request.rb', line 25

def user_agent
  @user_agent
end

Instance Method Details

#http_accept_languageObject

Provide a wrapper for the http_accept_language parser



77
78
79
# File 'lib/restfulness/request.rb', line 77

def http_accept_language
  @http_accept_language = HttpAcceptLanguage::Parser.new(headers[:accept_language])
end

#paramsObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/restfulness/request.rb', line 57

def params
  return @params if @params || body.nil?
  case headers[:content_type]
    when /application\/json/
      begin
        @params = MultiJson.decode(body)
      rescue MultiJson::LoadError
        raise HTTPException.new(400)
      end
  else
    raise HTTPException.new(406)
  end
end

#pathObject



40
41
42
# File 'lib/restfulness/request.rb', line 40

def path
  @path ||= (route ? route.build_path(uri.path) : nil)
end

#queryObject



49
50
51
# File 'lib/restfulness/request.rb', line 49

def query
  @query ||= ::Rack::Utils.parse_nested_query(uri.query).with_indifferent_access
end

#routeObject



44
45
46
47
# File 'lib/restfulness/request.rb', line 44

def route
  # Determine the route from the uri
  @route ||= app.router.route_for(uri.path)
end

#sanitized_paramsObject



71
72
73
74
# File 'lib/restfulness/request.rb', line 71

def sanitized_params
  # Note: this returns nil if #params has not been called
  @sanitized_params ||= @params ? Sanitizer.sanitize_hash(@params) : nil
end

#sanitized_query_stringObject



53
54
55
# File 'lib/restfulness/request.rb', line 53

def sanitized_query_string
  @sanitized_query ||= uri.query ? Sanitizer.sanitize_query_string(uri.query) : ''
end