Class: UseragentParser::UserAgentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/useragent_parser/parsers/user_agent_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, family_replacement = nil, v1_replacement = nil) ⇒ UserAgentParser

Returns a new instance of UserAgentParser.



7
8
9
10
11
12
# File 'lib/useragent_parser/parsers/user_agent_parser.rb', line 7

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

Instance Attribute Details

#family_replacementObject

Returns the value of attribute family_replacement.



5
6
7
# File 'lib/useragent_parser/parsers/user_agent_parser.rb', line 5

def family_replacement
  @family_replacement
end

#patternObject

Returns the value of attribute pattern.



5
6
7
# File 'lib/useragent_parser/parsers/user_agent_parser.rb', line 5

def pattern
  @pattern
end

#user_agent_reObject

Returns the value of attribute user_agent_re.



5
6
7
# File 'lib/useragent_parser/parsers/user_agent_parser.rb', line 5

def user_agent_re
  @user_agent_re
end

#v1_replacementObject

Returns the value of attribute v1_replacement.



5
6
7
# File 'lib/useragent_parser/parsers/user_agent_parser.rb', line 5

def v1_replacement
  @v1_replacement
end

Instance Method Details

#match_spans(user_agent_string) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/useragent_parser/parsers/user_agent_parser.rb', line 14

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

#parse(user_agent_string) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/useragent_parser/parsers/user_agent_parser.rb', line 22

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