Class: Brauser::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/brauser/parser.rb

Overview

A parser for the HTTP headers.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.disambiguate(subject, positive_matcher, negative_matcher) ⇒ Boolean

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

Parameters:

  • subject (String)

    The subject to match.

  • positive_matcher (Regexp)

    The expression to match.

  • negative_matcher (Regexp)

    The expression NOT to match.

Returns:

  • (Boolean)

    true if matching succeeded, false otherwise.



15
16
17
# File 'lib/brauser/parser.rb', line 15

def self.disambiguate(subject, positive_matcher, negative_matcher)
  subject =~ positive_matcher && subject !~ negative_matcher
end

Instance Method Details

#parse_accept_language(header) ⇒ Hash

Parses a Accept-Language header.

Parameters:

  • header (String)

    The Accept-Language header.

Returns:

  • (Hash)

    The list of accepted languages with their priorities.



39
40
41
42
43
44
45
# File 'lib/brauser/parser.rb', line 39

def parse_accept_language(header)
  header.ensure_string.tokenize.reduce({}) { |rv, token|
    code, priority = token.split(";q=")
    rv[code.downcase.gsub("_", "-").to_sym] = priority.to_float if code && priority
    rv
  }
end

#parse_agent(header) ⇒ Array|NilClass

Parses the User-Agent header.

Parameters:

  • header (String)

    The User-Agent header.

Returns:

  • (Array|NilClass)

    An array of engine, version and platform if the match succeeded, nil otherwise.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/brauser/parser.rb', line 23

def parse_agent(header)
  # First of all match the agent and the version
  catch(:result) {
    Brauser::Definitions.browsers.each do |_, definition|
      result = definition.match(header)
      throw(:result, result) if result
    end

    nil
  }
end