Class: Brauser::Parser
- Inherits:
-
Object
- Object
- Brauser::Parser
- Defined in:
- lib/brauser/parser.rb
Overview
A parser for the HTTP headers.
Class Method Summary collapse
-
.disambiguate(subject, positive_matcher, negative_matcher) ⇒ Boolean
Makes sure a subject matches a pattern AND NOT another pattern.
Instance Method Summary collapse
-
#parse_accept_language(header) ⇒ Hash
Parses a Accept-Language header.
-
#parse_agent(header) ⇒ Array|NilClass
Parses the User-Agent header.
Class Method Details
.disambiguate(subject, positive_matcher, negative_matcher) ⇒ Boolean
Makes sure a subject matches a pattern AND NOT another pattern.
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.
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.
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 |