Class: Restfulness::Request

Inherits:
Object
  • Object
show all
Includes:
Restfulness::Requests::Authorization
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

Methods included from Restfulness::Requests::Authorization

#authorization

Constructor Details

#initialize(app) ⇒ Request

Returns a new instance of Request.



31
32
33
34
35
36
37
38
# File 'lib/restfulness/request.rb', line 31

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



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

def action
  @action
end

#appObject (readonly)

Who does this request belong to?



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

def app
  @app
end

#bodyObject

Raw HTTP body, for POST and PUT requests.



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

def body
  @body
end

#envObject

Expose rack env to interact with rack middleware



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

def env
  @env
end

#headersObject

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



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

def headers
  @headers
end

#remote_ipObject

Additional useful fields



29
30
31
# File 'lib/restfulness/request.rb', line 29

def remote_ip
  @remote_ip
end

#uriObject

Ruby URI object



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

def uri
  @uri
end

#user_agentObject

Additional useful fields



29
30
31
# File 'lib/restfulness/request.rb', line 29

def user_agent
  @user_agent
end

Instance Method Details

#acceptObject



61
62
63
64
65
# File 'lib/restfulness/request.rb', line 61

def accept
  if headers[:accept]
    @accept ||= Headers::Accept.new(headers[:accept])
  end
end

#content_typeObject



67
68
69
70
71
# 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_languageObject

Provide a wrapper for the http_accept_language parser



97
98
99
# File 'lib/restfulness/request.rb', line 97

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

#paramsObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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

#pathObject



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

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

#queryObject



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

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

#routeObject



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

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

#sanitized_paramsObject



91
92
93
94
# 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_stringObject



57
58
59
# File 'lib/restfulness/request.rb', line 57

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