Class: UserAgentParser::Parser
- Inherits:
-
Object
- Object
- UserAgentParser::Parser
- Extended by:
- Gem::Deprecate
- Defined in:
- lib/user_agent_parser/parser.rb
Instance Attribute Summary collapse
-
#patterns_paths ⇒ Object
readonly
Returns the value of attribute patterns_paths.
Instance Method Summary collapse
-
#initialize(patterns_path: nil, patterns_paths: []) ⇒ Parser
constructor
A new instance of Parser.
- #parse(user_agent) ⇒ Object
- #parse_device(user_agent) ⇒ Object
- #parse_os(user_agent) ⇒ Object
- #parse_ua(user_agent, os = nil, device = nil) ⇒ Object
- #patterns_path ⇒ Object
Constructor Details
#initialize(patterns_path: nil, patterns_paths: []) ⇒ Parser
Returns a new instance of Parser.
29 30 31 32 33 34 |
# File 'lib/user_agent_parser/parser.rb', line 29 def initialize(patterns_path: nil, patterns_paths: []) @patterns_paths = [patterns_path, *patterns_paths].compact @patterns_paths = [UserAgentParser::DefaultPatternsPath] if @patterns_paths.empty? @ua_patterns, @os_patterns, @device_patterns = load_patterns(@patterns_paths) end |
Instance Attribute Details
#patterns_paths ⇒ Object (readonly)
Returns the value of attribute patterns_paths.
27 28 29 |
# File 'lib/user_agent_parser/parser.rb', line 27 def patterns_paths @patterns_paths end |
Instance Method Details
#parse(user_agent) ⇒ Object
36 37 38 39 40 |
# File 'lib/user_agent_parser/parser.rb', line 36 def parse(user_agent) os = parse_os(user_agent) device = parse_device(user_agent) parse_ua(user_agent, os, device) end |
#parse_device(user_agent) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/user_agent_parser/parser.rb', line 52 def parse_device(user_agent) pattern, match = first_pattern_match(@device_patterns, user_agent) if match device_from_pattern_match(pattern, match) else Device.new end end |
#parse_os(user_agent) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/user_agent_parser/parser.rb', line 42 def parse_os(user_agent) pattern, match = first_pattern_match(@os_patterns, user_agent) if match os_from_pattern_match(pattern, match) else OperatingSystem.new end end |
#parse_ua(user_agent, os = nil, device = nil) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/user_agent_parser/parser.rb', line 62 def parse_ua(user_agent, os = nil, device = nil) pattern, match = first_pattern_match(@ua_patterns, user_agent) if match user_agent_from_pattern_match(pattern, match, os, device) else UserAgent.new(nil, nil, os, device) end end |
#patterns_path ⇒ Object
72 73 74 |
# File 'lib/user_agent_parser/parser.rb', line 72 def patterns_path patterns_paths.first end |