Class: Praxis::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Request

Returns a new instance of Request.



8
9
10
11
12
13
# File 'lib/praxis/request.rb', line 8

def initialize(env)
  @env = env
  @query = Rack::Utils.parse_nested_query(env['QUERY_STRING'.freeze])
  @route_params = {}
  load_version
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



5
6
7
# File 'lib/praxis/request.rb', line 5

def action
  @action
end

#envObject (readonly)

Returns the value of attribute env.



4
5
6
# File 'lib/praxis/request.rb', line 4

def env
  @env
end

#headersObject

Returns the value of attribute headers.



33
34
35
# File 'lib/praxis/request.rb', line 33

def headers
  @headers
end

#paramsObject

Returns the value of attribute params.



33
34
35
# File 'lib/praxis/request.rb', line 33

def params
  @params
end

#payloadObject

Returns the value of attribute payload.



33
34
35
# File 'lib/praxis/request.rb', line 33

def payload
  @payload
end

#queryObject (readonly)

Returns the value of attribute query.



4
5
6
# File 'lib/praxis/request.rb', line 4

def query
  @query
end

#route_paramsObject

Returns the value of attribute route_params.



5
6
7
# File 'lib/praxis/request.rb', line 5

def route_params
  @route_params
end

#versionObject (readonly)

Returns the value of attribute version.



4
5
6
# File 'lib/praxis/request.rb', line 4

def version
  @version
end

Instance Method Details

#coalesce_inputs!Object



64
65
66
67
# File 'lib/praxis/request.rb', line 64

def coalesce_inputs!
  self.raw_params
  self.raw_payload
end

#content_typeObject



15
16
17
# File 'lib/praxis/request.rb', line 15

def content_type
  @env['CONTENT_TYPE'.freeze]
end

#load_headers(context) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/praxis/request.rb', line 74

def load_headers(context)
  return unless action.headers
  defined_headers = action.precomputed_header_keys_for_rack.each_with_object(Hash.new) do |(upper,original), hash|
    hash[original] = self.env[upper] if self.env.has_key? upper
  end
  self.headers = action.headers.load(defined_headers, context)
end

#load_params(context) ⇒ Object



82
83
84
85
# File 'lib/praxis/request.rb', line 82

def load_params(context)
  return unless action.params
  self.params = action.params.load(self.raw_params, context)
end

#load_payload(context) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/praxis/request.rb', line 87

def load_payload(context)
  return unless action.payload
  raw = case content_type
    when %r|^application/x-www-form-urlencoded|i
      Rack::Utils.parse_nested_query(self.raw_payload)
    when nil
      {}
    else
      self.raw_payload
    end

  self.payload = action.payload.load(raw, context, content_type: content_type)
end

#load_versionObject



69
70
71
72
# File 'lib/praxis/request.rb', line 69

def load_version
  @version = env.fetch("HTTP_X_API_VERSION".freeze,
                       @query.fetch('api_version'.freeze, 'n/a'.freeze))
end

#media_typeObject

The media type (type/subtype) portion of the CONTENT_TYPE header without any media type parameters. e.g., when CONTENT_TYPE is “text/plain;charset=utf-8”, the media-type is “text/plain”.

For more information on the use of media types in HTTP, see: www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7



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

def media_type
  content_type && content_type.split(/\s*[;,]\s*/, 2).first.downcase
end

#params_hashObject



35
36
37
38
39
40
41
# File 'lib/praxis/request.rb', line 35

def params_hash
  return {} if params.nil?

  params.attributes.each_with_object({}) do |(k,v),hash|
    hash[k] = v
  end
end

#pathObject



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

def path
  @env['PATH_INFO'.freeze]
end

#raw_paramsObject



47
48
49
50
51
52
53
# File 'lib/praxis/request.rb', line 47

def raw_params
  @raw_params ||= begin
    params = query.merge(route_params)
    params.delete('api_version'.freeze)
    params
  end
end

#raw_payloadObject



55
56
57
58
59
60
61
62
# File 'lib/praxis/request.rb', line 55

def raw_payload
  @raw_payload ||= begin
    if (input = env['rack.input'.freeze].read)
      env['rack.input'.freeze].rewind
      input
    end
  end
end

#unmatched_versionsObject

versions that matched all the conditions of the request (except its version)



120
121
122
# File 'lib/praxis/request.rb', line 120

def unmatched_versions
  @unmatched_versions ||= Set.new
end

#validate_headers(context) ⇒ Object



101
102
103
104
105
# File 'lib/praxis/request.rb', line 101

def validate_headers(context)
  return [] unless action.headers

  action.headers.validate(self.headers, context)
end

#validate_params(context) ⇒ Object



107
108
109
110
111
# File 'lib/praxis/request.rb', line 107

def validate_params(context)
  return [] unless action.params

  action.params.validate(self.params, context)
end

#validate_payload(context) ⇒ Object



113
114
115
116
117
# File 'lib/praxis/request.rb', line 113

def validate_payload(context)
  return [] unless action.payload

  action.payload.validate(self.payload, context)
end

#verbObject



43
44
45
# File 'lib/praxis/request.rb', line 43

def verb
  @env['REQUEST_METHOD'.freeze]
end