Sha256: b301836a4303e0a11c9587e600a0620d0f88224e7b8d7d44286a60bfa80676d7

Contents?: true

Size: 1.19 KB

Versions: 6

Compression:

Stored size: 1.19 KB

Contents

# encoding: utf-8

module UseragentParser
  class UserAgentParser
    attr_accessor :pattern, :user_agent_re, :family_replacement, :v1_replacement

    def initialize(pattern, family_replacement = nil, v1_replacement = nil)
      @pattern = pattern
      @user_agent_re = Regexp.compile(pattern)
      @family_replacement = family_replacement
      @v1_replacement = v1_replacement
    end

    def match_spans(user_agent_string)
      match_spans = []
      match = @user_agent_re.match(user_agent_string)
      if match
        # Return the offsets
      end
    end

    def parse(user_agent_string)
      family, v1, v2, v3 = nil, nil, nil, nil
      match = @user_agent_re.match(user_agent_string)
      if match
        family = match[1]
        if @family_replacement
          if %r'\$1'.match @family_replacement
            family = @family_replacement.gsub(%r'\$1', match[1])
          else
            family = @family_replacement
          end
        end

        if @v1_replacement
          v1 = @v1_replacement
        else
          v1 = match[2]
        end

        v2 = match[3] if match.size >= 4
        v3 = match[4] if match.size >= 5
      end
      return family, v1, v2, v3
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
useragent_parser-0.2.3 lib/useragent_parser/parsers/user_agent_parser.rb
useragent_parser-0.2.2 lib/useragent_parser/parsers/user_agent_parser.rb
useragent_parser-0.2.1 lib/useragent_parser/parsers/user_agent_parser.rb
useragent_parser-0.2.0 lib/useragent_parser/parsers/user_agent_parser.rb
useragent_parser-0.1.1 lib/useragent_parser/parsers/user_agent_parser.rb
useragent_parser-0.1.0 lib/useragent_parser/parsers/user_agent_parser.rb