Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/user_agent_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
require 'user_agent_parser/version'
require 'user_agent_parser/operating_system'
require 'user_agent_parser/device'
require 'user_agent_parser/cache'

module UserAgentParser
DefaultPatternsPath = File.join(File.dirname(__FILE__), "../vendor/ua-parser/regexes.yaml")
DefaultCache = Cache.new

# Parse the given +user_agent_string+, returning a +UserAgent+
def self.parse(user_agent_string, options={})
Parser.new(options).parse(user_agent_string)
DefaultCache.fetch(options).parse(user_agent_string)
end
end
17 changes: 17 additions & 0 deletions lib/user_agent_parser/cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module UserAgentParser
class Cache
def initialize
@registry = {}
end

def fetch(options)
@registry.fetch(options.hash) do
add(options)
end
end

def add(options)
@registry[options.hash] = Parser.new(options)
end
end
end
2 changes: 1 addition & 1 deletion lib/user_agent_parser/parser.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'yaml'

module UserAgentParser

class Parser
attr_reader :patterns_path

Expand Down
19 changes: 19 additions & 0 deletions spec/cache_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe UserAgentParser::Cache do
def custom_patterns_path
File.join(File.dirname(__FILE__), "custom_regexes.yaml")
end

it 'stores and returns the parser based on options' do
cache = UserAgentParser::Cache.new
cached_path = cache.fetch(:patterns_path => UserAgentParser::DefaultPatternsPath).patterns_path
cached_path.must_equal UserAgentParser::DefaultPatternsPath
end

it 'defaults to the default parser' do
cache = UserAgentParser::Cache.new
cached_path = cache.fetch(:patterns_path => custom_patterns_path).patterns_path
cached_path.must_equal custom_patterns_path
end
end
2 changes: 1 addition & 1 deletion spec/custom_regexes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ os_parsers:

device_parsers:
- regex: '.*'
device_replacement: 'Custom device'
device_replacement: 'Custom device'
1 change: 1 addition & 0 deletions spec/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def custom_patterns_path
ua = UserAgentParser.parse("Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/418.8 (KHTML, like Gecko) Safari/419.3")
ua.family.must_equal("Safari")
end

it "accepts a custom patterns path" do
ua = UserAgentParser.parse("Any user agent string", :patterns_path => custom_patterns_path)
ua.family.must_equal("Custom browser")
Expand Down