Class: Praxis::Request

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Request



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

def initialize(env)
  @env = env
  @query = Rack::Utils.parse_nested_query(env['QUERY_STRING'.freeze])
  @route_params = {}
  @path_version_matcher = path_version_matcher
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.



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

def headers
  @headers
end

#paramsObject

Returns the value of attribute params.



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

def params
  @params
end

#payloadObject

Returns the value of attribute payload.



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

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

Class Method Details

.path_version_prefixObject



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

def self.path_version_prefix
  "/v".freeze
end

Instance Method Details

#coalesce_inputs!Object



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

def coalesce_inputs!
  self.raw_params
  self.raw_payload
end

#content_typeObject



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

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

#load_headers(context) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/praxis/request.rb', line 94

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



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

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

#load_payload(context) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/praxis/request.rb', line 107

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

#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



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

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

#params_hashObject



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

def params_hash
  return {} if params.nil?

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

#pathObject



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

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

#path_version_matcherObject



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

def path_version_matcher
  %r{^#{Request.path_version_prefix}(?<version>[^\/]+)\/}.freeze
end

#raw_paramsObject



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

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

#raw_payloadObject



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

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)



140
141
142
# File 'lib/praxis/request.rb', line 140

def unmatched_versions
  @unmatched_versions ||= Set.new
end

#validate_headers(context) ⇒ Object



121
122
123
124
125
# File 'lib/praxis/request.rb', line 121

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

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

#validate_params(context) ⇒ Object



127
128
129
130
131
# File 'lib/praxis/request.rb', line 127

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

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

#validate_payload(context) ⇒ Object



133
134
135
136
137
# File 'lib/praxis/request.rb', line 133

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

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

#verbObject



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

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

#version(using: [:header,:params].freeze) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/praxis/request.rb', line 76

def version(using: [:header,:params].freeze)
  result = nil
  Array(using).find do |mode|
    case mode
    when :header ;
      result = env["HTTP_X_API_VERSION".freeze]
    when :params ;
      result = @query['api_version'.freeze]
    when :path ;
      m = self.path.match(@path_version_matcher) 
      result = m[:version] unless m.nil?
    else
      raise "Unknown method for retrieving the API version: #{mode}"
    end
  end
  return result || 'n/a'.freeze
end