class Sparrow::Steward

Parses the http_mesage and provides information if it should be processed, i.e. if the content type or accept header is applicable

@todo this class is requested to be removed by issue #7 in the feature and its internal will be moved to its counterparts such as HttpMessage itself. @version 0.0.16

Attributes

allowed_accepts[R]

@return [Array<String>] @see Sparrow::Configuration#allowed_accepts

allowed_content_types[R]

@return [Array<String>] @see Sparrow::Configuration#allowed_content_types

excluded_routes[R]

@return [Array<String,Regexp>] @see Sparrow::Configuration#excluded_routes

http_message[R]

@return [HttpMessage] the wrapped http message object to be checked

ignored_response_codes[R]

@return [Array<Integer>] @see Configuration#ignored_response_codes

route_parser[R]

@return [RouteParser] the route parser for the excluded_routes @see RouteParser @see exluded_routes

Public Class Methods

new(http_message, options = {}) click to toggle source

Initialize the Steward with the HTTP message to check and the specific check options.

@param options [Hash] @option options [HttpMessage] :http_message the message to be checked @option options [Array<String>] :allowed_accepts ([]) List of HTTP Accept

Header options

@option options [Array<String>] :allowed_content_types ([])

List of HTTP Content Type Header options

@option options [Array<String,Regexp>] :excluded_routes ([]) List of routes

(paths) to not process
(see Sparrow::Configuration#excluded_routes)

@option options [Array<Integer>] ignored_response_codes ([])

(see Sparrow::Configuration#ignored_response_codes)

@see RouteParser#excluded_routes @see Configuration

# File lib/sparrow/steward.rb, line 56
def initialize(http_message, options = {})
  @http_message           = http_message
  @allowed_accepts        = options.fetch(:allowed_accepts, [])
  @allowed_content_types  = options.fetch(:allowed_content_types, [])
  @excluded_routes        = options.fetch(:excluded_routes, [])
  @ignored_response_codes = options.fetch(:ignored_response_codes, [])
  @route_parser           = RouteParser.new(excluded_routes)
end

Public Instance Methods

has_processable_http_message?() click to toggle source

Checks the http_message against any given criteria specified by the options within the constructor

@return [Boolean] processable message

# File lib/sparrow/steward.rb, line 70
def has_processable_http_message?
  includes_route? &&
      allowed_content_type? &&
      allowed_accept_header?
end

Private Instance Methods

accept_type_matches?(accepted_headers, type) click to toggle source

@private

# File lib/sparrow/steward.rb, line 119
def accept_type_matches?(accepted_headers, type)
  accepted_headers.detect do |accept|
    type.include?(accept)
  end
end
allowed_accept_header?() click to toggle source

@private

# File lib/sparrow/steward.rb, line 93
def allowed_accept_header?
  accept_header = http_message.accept

  allowed_accepts.include?(nil) ||
      accept_type_matches?(allowed_accepts, accept_header)
end
allowed_content_type?() click to toggle source

@private

# File lib/sparrow/steward.rb, line 86
def allowed_content_type?
  content_type = http_message.content_type
  content_type_equals?(content_type) || content_type_matches?(content_type)
end
content_type_equals?(type) click to toggle source

@private

# File lib/sparrow/steward.rb, line 102
def content_type_equals?(type)
  allowed_content_types.include?(type)
end
content_type_matches?(type) click to toggle source

@private

# File lib/sparrow/steward.rb, line 108
def content_type_matches?(type)
  matches = allowed_content_types.map do |acceptable_content_type|
    (acceptable_content_type &&
        type.to_s.starts_with?(acceptable_content_type.to_s))
  end

  matches.any?
end
includes_route?() click to toggle source

@private

# File lib/sparrow/steward.rb, line 80
def includes_route?
  route_parser.allow?(http_message.path)
end