lib/uaid/user_agent.rb in fivepointssolutions-uaid-0.0.1 vs lib/uaid/user_agent.rb in fivepointssolutions-uaid-0.1.0
- old
+ new
@@ -1,49 +1,62 @@
module Uaid
+ def Uaid.supported_agents
+ @supported ||= [
+ /safari [34]/,
+ /chrome/,
+ /firefox [23]/,
+ /ie [678]/,
+ /android/,
+ /bot/
+ ]
+ end
+
+ # Define the set of supported agents. You may also concat to the existing
+ # list.
+ #
+ def Uaid.supported_agents=(s)
+ @supported = s
+ end
+
class UserAgent
- SUPPORTED, UNSUPPORTED = true, false
+ attr_reader :agent, :engine, :product, :version, :supported
- attr_reader :agent, :name, :version
+ def initialize(agent, extractor = Uaid.extractor)
+ @agent, @extractor = agent, extractor
+ @engine, @product, @version = @extractor.extract(@agent ? @agent.strip : '')
+ end
- def initialize(agent)
- @agent = agent
- @name, @version, @supported = extract(@agent ? @agent.strip : '')
+ def identifier
+ [engine, product, product + version].join(' ')
end
def supported?
- @supported
+ !!Uaid.supported_agents.detect do |agent_match|
+ case agent_match
+ when Array: agent_match == [engine, product, version]
+ when Regexp: [engine, product, version].join(' ') =~ agent_match
+ end
+ end
end
+ def unknown?
+ engine == 'unknown' || product == 'unknown' || version == 'x'
+ end
+
def unsupported?
!supported?
end
+ def version?(e)
+ version == e
+ end
+
def method_missing(method, *args)
- if method.to_s =~ /^(\w+)\?$/ && EXTRACTIONS.find {|patterns, values| values.first == $1}
- name == $1
+ if method.to_s =~ /^(\w+)\?$/
+ return true if (engine == $1 || product == $1)
+ @extractor.engines.include?($1) || @extractor.products.include?($1)
else
super
end
end
-
- protected
- EXTRACTIONS = [
- [[/MSIE/, /MSIE 7/], ['ie', '7', SUPPORTED]],
- [[/MSIE/, /MSIE 8/], ['ie', '8', SUPPORTED]],
- [[/MSIE/, /MSIE 6/], ['ie', '6', UNSUPPORTED]],
- [[/iPhone/, /Version\/3/], ['mobilesafari', '3', SUPPORTED]],
- [[/Safari/, /Version\/3/], ['webkit', '3', SUPPORTED]],
- [[/AppleWebKit/, /525/], ['webkit', '3', SUPPORTED]],
- [[/AppleWebKit/, /528/], ['webkit', '4', SUPPORTED]],
- [[/Gecko/, /Firefox\/3/], ['gecko', '3', SUPPORTED]],
- [[/Gecko/, /Firefox\/2|BonEcho\/2/], ['gecko', '2', SUPPORTED]],
- [[/Gecko/, /Shiretoko/], ['gecko', '3.5', SUPPORTED]],
- [[/googlebot|msnbot|ia_archiver/i], ['bot', 'x', SUPPORTED]],
- [[/^$/], ['noagent', 'x', UNSUPPORTED]],
- [[/.*/], ['unknown', 'x', UNSUPPORTED]]
- ]
-
- def extract(agent)
- EXTRACTIONS.detect {|patterns,| patterns.inject(true) {|m,pattern| m && agent =~ pattern} }.last
- end
end
end
\ No newline at end of file