class Brauser::Parser

A parser for the HTTP headers.

Public Class Methods

disambiguate(subject, positive_matcher, negative_matcher) click to toggle source

Makes sure a subject matches a pattern AND NOT another pattern.

@param subject [String] The subject to match. @param positive_matcher [Regexp] The expression to match. @param negative_matcher [Regexp] The expression NOT to match. @return [Boolean] `true` if matching succeeded, `false otherwise`.

# File lib/brauser/parser.rb, line 15
def self.disambiguate(subject, positive_matcher, negative_matcher)
  subject =~ positive_matcher && subject !~ negative_matcher
end

Public Instance Methods

parse_accept_language(header) click to toggle source

Parses a Accept-Language header.

@param header [String] The Accept-Language header. @return [Hash] The list of accepted languages with their priorities.

# File lib/brauser/parser.rb, line 39
def parse_accept_language(header)
  header.ensure_string.tokenize.reduce({}) do |rv, token|
    code, priority = token.split(";q=")
    rv[code.downcase.gsub("_", "-").to_sym] = priority.to_float if code && priority
    rv
  end
end
parse_agent(header) click to toggle source

Parses the User-Agent header.

@param header [String] The User-Agent header. @return [Array|NilClass] An array of engine, version and platform if the match succeeded, `nil` otherwise.

# File lib/brauser/parser.rb, line 23
def parse_agent(header)
  # First of all match the agent and the version
  catch(:result) do
    Brauser::Definitions.browsers.each do |_, definition|
      result = definition.match(header)
      throw(:result, result) if result
    end

    nil
  end
end