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 EndpointDefinition.version using: :path

PATH_VERSION_PREFIX
QUERY_STRING_NAME
REQUEST_METHOD_NAME
VERSION_USING_DEFAULTS

Attributes

action[RW]
env[R]
forwarded_from_action[RW]
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 18
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 79
def self.path_version_prefix
  PATH_VERSION_PREFIX
end

Public Instance Methods

coalesce_inputs!() click to toggle source
# File lib/praxis/request.rb, line 74
def coalesce_inputs!
  raw_params
  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 27
def content_type
  header = @env[CONTENT_TYPE_NAME]
  @content_type ||= (header && MediaTypeIdentifier.load(header)).freeze
end
inspect() click to toggle source

Override the inspect instance method of a request, as, by default, the kernel inspect will go nuts traversing the action and app_instance and therefore all associated instance variables reachable through that

# File lib/praxis/request.rb, line 186
def inspect
  "#<#{self.class}##{object_id} @action=#{@action.inspect} @params=#{@params.inspect}>"
end
load_headers(context) click to toggle source
# File lib/praxis/request.rb, line 113
def load_headers(context)
  return unless action.headers

  defined_headers = action.precomputed_header_keys_for_rack.each_with_object({}) do |(upper, original), hash|
    hash[original] = env[upper] if env.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 122
def load_params(context)
  return unless action.params

  self.params = action.params.load(raw_params, context)
  return unless forwarded_from_action && content_type

  # If it is coming from a forwarded action, and has a content type, let's parse it, and merge it to the params as well
  raw = if (handler = Praxis::Application.instance.handlers[content_type.handler_name])
          handler.parse(raw_payload)
        else
          # TODO: is this a good default?
          raw_payload
        end
  loaded_payload = forwarded_from_action.payload.load(raw, context, content_type: content_type.to_s)
  self.params = params.merge(loaded_payload)
end
load_payload(context) click to toggle source
# File lib/praxis/request.rb, line 139
def load_payload(context)
  return unless action.payload
  return if content_type.nil?

  return if action.sister_post_action # Do not load payloads for a special GET action with a sister post one...(cause we've loaded into the params)

  raw = if (handler = Praxis::Application.instance.handlers[content_type.handler_name])
          handler.parse(raw_payload)
        else
          # TODO: is this a good default?
          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 41
def media_type
  content_type.without_parameters.to_s
end
params_hash() click to toggle source
# File lib/praxis/request.rb, line 49
def params_hash
  return {} if params.nil?

  params.attributes
end
path() click to toggle source
# File lib/praxis/request.rb, line 45
def path
  @env[PATH_INFO_NAME]
end
path_version_matcher() click to toggle source
# File lib/praxis/request.rb, line 86
def path_version_matcher
  if Application.instance.versioning_scheme == :path
    matcher = Mustermann.new("#{ApiDefinition.instance.info.base_path}*")
    matcher.params(path)[API_VERSION_PARAM_NAME]
  else
    PATH_VERSION_MATCHER.match(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 ||= if (input = env['rack.input'].read)
                     env['rack.input'].rewind
                     input
                   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 180
def unmatched_versions
  @unmatched_versions ||= Set.new
end
validate_headers(context) click to toggle source
# File lib/praxis/request.rb, line 155
def validate_headers(context)
  return [] unless action.headers

  return ["Attribute #{Attributor.humanize_context(context)} is required."] if action.headers.options[:required] == true && headers.nil?

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

  return ["Attribute #{Attributor.humanize_context(context)} is required."] if action.params.options[:required] == true && params.nil?

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

  return ["Attribute #{Attributor.humanize_context(context)} is required."] if action.payload.options[:required] == true && payload.nil?

  action.payload.validate(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 95
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 = path_version_matcher
    else
      raise "Unknown method for retrieving the API version: #{mode}"
    end
  end
  result || API_NO_VERSION_NAME
end