class Agent212::Parser

Constants

CHAR

quoted-pair = “" CHAR CHAR = <any US-ASCII character (octets 0 - 127)>

CTEXT

ctext = <any TEXT excluding “(” and “)”>

LEFT_BRACKET
LWS

LWS = [CRLF] 1*( SP | HT )

PRODUCT
QUOTED_PAIR
RIGHT_BRACKET
SEPARATORS

tools.ietf.org/html/rfc2616#section-3.8

SLASH
TOKEN

token = 1*<any CHAR except CTLs or separators>

Public Class Methods

new(input) click to toggle source
# File lib/agent212/parser.rb, line 31
def initialize(input)
  @input = StringScanner.new(input.to_s.strip)
end
parse(input) click to toggle source
# File lib/agent212/parser.rb, line 35
def self.parse(input)
  new(input).parse
end

Public Instance Methods

parse() click to toggle source

returns an array with the parts from the user_agent string in @input

# File lib/agent212/parser.rb, line 40
def parse
  parse_user_agent
end
parse_comment() click to toggle source

comment = “(” *( ctext | quoted-pair | comment ) “)” returns a string with the comment including the brackets

# File lib/agent212/parser.rb, line 94
def parse_comment
  comment = @input.scan(LEFT_BRACKET) or return nil
  comment << parse_star_comment_contents
  comment << @input.scan(RIGHT_BRACKET) or raise ParseError, "expected ) to end the comment #{comment.inspect}"
rescue => error
  if @input.eos?
    raise ParseError, "unexpected end of string (original error: ##{error}"
  end
end
parse_ctext() click to toggle source

ctext = <any TEXT excluding “(” and “)”>

# File lib/agent212/parser.rb, line 118
def parse_ctext
  @input.scan(CTEXT)
end
parse_ctext_or_quotedpair_or_comment() click to toggle source
# File lib/agent212/parser.rb, line 113
def parse_ctext_or_quotedpair_or_comment
  parse_ctext || parse_quotedpair || parse_comment
end
parse_lws() click to toggle source

returns the scanned whitespace or nil

# File lib/agent212/parser.rb, line 64
def parse_lws
  @input.scan(LWS)
end
parse_optional_product_version() click to toggle source
“/” product-version

returns nil or the version of the product raises an error if ‘/” is followed by a non-token

# File lib/agent212/parser.rb, line 80
def parse_optional_product_version
  return nil unless @input.scan(SLASH)
  version = parse_product_version or raise ParseError, "expected a product-version at character #{@input.pos}"
  version
end
parse_product() click to toggle source

product = token [“/” product-version]

# File lib/agent212/parser.rb, line 69
def parse_product
  token = parse_token
  return nil if token.nil?
  Product.new(token).tap do |product|
    product.version = parse_optional_product_version
  end
end
parse_product_or_comment() click to toggle source

product | comment

# File lib/agent212/parser.rb, line 59
def parse_product_or_comment
  parse_product || parse_comment
end
parse_product_version()
Alias for: parse_token
parse_quotedpair() click to toggle source

quoted-pair = “" CHAR

# File lib/agent212/parser.rb, line 123
def parse_quotedpair
  @input.scan(QUOTED_PAIR)
end
parse_star_comment_contents() click to toggle source

*( ctext | quoted-pair | comment )

# File lib/agent212/parser.rb, line 105
def parse_star_comment_contents
  contents = ""
  while x = parse_ctext_or_quotedpair_or_comment
    contents << x
  end
  contents
end
parse_token() click to toggle source

returns the token or nil

# File lib/agent212/parser.rb, line 87
def parse_token
  @input.scan(TOKEN)
end
Also aliased as: parse_product_version
parse_user_agent() click to toggle source

user agent = 1 * ( product | comment ) meaning AT LEAST ONE product or comment returns an array with all parsed products

# File lib/agent212/parser.rb, line 47
def parse_user_agent
  parts = []
  parts << parse_product_or_comment || raise(ParseError, 'expected at least one ( product | comment )')
  parse_lws or return parts
  while x = parse_product_or_comment
    parts << x
    parse_lws
  end
  parts
end