class Praxis::Request

Constants

API_NO_VERSION_NAME
API_VERSION_HEADER_NAME
API_VERSION_PARAM_NAME
CONTENT_TYPE_NAME
PATH_INFO_NAME
PATH_VERSION_MATCHER

DEPRECATED: remove with ResourceDefinition.version using: :path

PATH_VERSION_PREFIX
QUERY_STRING_NAME
REQUEST_METHOD_NAME
VERSION_USING_DEFAULTS

Attributes

action[RW]
env[R]
headers[RW]
params[RW]
payload[RW]
query[R]
route_params[RW]

Public Class Methods

new(env) click to toggle source
# File lib/praxis/request.rb, line 17
def initialize(env)
  @env = env
  @query = Rack::Utils.parse_nested_query(env[QUERY_STRING_NAME])
  @route_params = {}
end
path_version_prefix() click to toggle source
# File lib/praxis/request.rb, line 81
def self.path_version_prefix
  PATH_VERSION_PREFIX
end

Public Instance Methods

coalesce_inputs!() click to toggle source
# File lib/praxis/request.rb, line 76
def coalesce_inputs!
  self.raw_params
  self.raw_payload
end
content_type() click to toggle source

Determine the content type of this request as indicated by the Content-Type header.

@return [nil,MediaTypeIdentifier] nil if the header is missing, else a media-type identifier

# File lib/praxis/request.rb, line 26
def content_type
  header = @env[CONTENT_TYPE_NAME]
  @content_type ||= (header && MediaTypeIdentifier.load(header)).freeze
end
load_headers(context) click to toggle source
# File lib/praxis/request.rb, line 115
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) click to toggle source
# File lib/praxis/request.rb, line 123
def load_params(context)
  return unless action.params
  self.params = action.params.load(self.raw_params, context)
end
load_payload(context) click to toggle source
# File lib/praxis/request.rb, line 128
def load_payload(context)
  return unless action.payload
  return if content_type.nil?

  raw = if (handler = Praxis::Application.instance.handlers[content_type.handler_name])
    handler.parse(self.raw_payload)
  else
    # TODO is this a good default?
    self.raw_payload
  end

  self.payload = action.payload.load(raw, context, content_type: content_type.to_s)
end
media_type() click to toggle source

The media type (type/subtype+suffix) 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

@return [String] @see MediaTypeIdentifier#without_parameters

# File lib/praxis/request.rb, line 40
def media_type
  content_type.without_parameters.to_s
end
params_hash() click to toggle source
# File lib/praxis/request.rb, line 50
def params_hash
  return {} if params.nil?
  params.attributes
end
path() click to toggle source
# File lib/praxis/request.rb, line 44
def path
  @env[PATH_INFO_NAME]
end
path_version_matcher() click to toggle source
# File lib/praxis/request.rb, line 88
def path_version_matcher
  if Application.instance.versioning_scheme == :path
    matcher = Mustermann.new(ApiDefinition.instance.info.base_path + '*')
    matcher.params(self.path)[API_VERSION_PARAM_NAME]
  else
    PATH_VERSION_MATCHER.match(self.path)[:version]
  end
end
raw_params() click to toggle source
# File lib/praxis/request.rb, line 59
def raw_params
  @raw_params ||= begin
    params = query.merge(route_params)
    params.delete(API_VERSION_PARAM_NAME)
    params
  end
end
raw_payload() click to toggle source
# File lib/praxis/request.rb, line 67
def raw_payload
  @raw_payload ||= begin
    if (input = env['rack.input'.freeze].read)
      env['rack.input'.freeze].rewind
      input
    end
  end
end
unmatched_versions() click to toggle source

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

# File lib/praxis/request.rb, line 161
def unmatched_versions
  @unmatched_versions ||= Set.new
end
validate_headers(context) click to toggle source
# File lib/praxis/request.rb, line 142
def validate_headers(context)
  return [] unless action.headers

  action.headers.validate(self.headers, context)
end
validate_params(context) click to toggle source
# File lib/praxis/request.rb, line 148
def validate_params(context)
  return [] unless action.params

  action.params.validate(self.params, context)
end
validate_payload(context) click to toggle source
# File lib/praxis/request.rb, line 154
def validate_payload(context)
  return [] unless action.payload

  action.payload.validate(self.payload, context)
end
verb() click to toggle source
# File lib/praxis/request.rb, line 55
def verb
  @env[REQUEST_METHOD_NAME]
end
version() click to toggle source
# File lib/praxis/request.rb, line 97
def version
  result = nil

  Array(Application.instance.versioning_scheme).find do |mode|
    case mode
    when :header;
      result = env[API_VERSION_HEADER_NAME]
    when :params;
      result = @query[API_VERSION_PARAM_NAME]
    when :path;
      result = self.path_version_matcher
    else
      raise "Unknown method for retrieving the API version: #{mode}"
    end
  end
  return result || API_NO_VERSION_NAME
end