Module: DoubleAgent
- Defined in:
- lib/double_agent/double_agent.rb,
lib/double_agent/parser.rb,
lib/double_agent/version.rb,
lib/double_agent/resource.rb,
lib/double_agent/user_agent.rb
Overview
A browser user agent string parser. Use DoubleAgent.parse(user_agent) to return a DoubleAgent::UserAgent object, which you can query with methods like “browser_name” or “os”. The DoubleAgent module itself responds to those same method - just pass the user agent string as an argument.
Defined Under Namespace
Modules: Resource Classes: BrowserParser, OSParser, UserAgent
Constant Summary collapse
- VERSION =
Version number
'1.1.0'
- BROWSER_DATA =
A hash of browser data, the basis for browser parsing. You may edit this data and call load_browsers! to customize parsing.
Psych.load_file(File.('../../../data/browsers.yml', __FILE__))
- OS_DATA =
A hash of OS hashes, the basis for OS parsing. You may edit this data and call load_oses! to customize parsing.
Psych.load_file(File.('../../../data/oses.yml', __FILE__))
Class Method Summary collapse
-
.load_browsers! ⇒ Object
Transforms BROWSER_DATA into a case statement inside of the _browser_sym method.
-
.load_oses! ⇒ Object
Transforms OS_DATA into a case statement inside of the _os_sym method.
-
.method_missing(method, *args, &block) ⇒ Object
Forwards calls to a UserAgent object.
-
.parse(user_agent_string) ⇒ Object
Returns a new UserAgent object.
-
.resource(klass, &user_agent_block) ⇒ Object
Mix DoubleAgent::Resource into klass.
Class Method Details
.load_browsers! ⇒ Object
Transforms BROWSER_DATA into a case statement inside of the _browser_sym method
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/double_agent/double_agent.rb', line 33 def self.load_browsers! BROWSER_PARSERS.clear.merge! Hash[BROWSER_DATA.map { |sym, data| [sym, BrowserParser.new(sym, data)] }] module_eval <<-CODEZ def self._browser_sym(user_agent) case user_agent.to_s #{BROWSER_DATA.map { |sym, data| "when %r{#{data[:regex]}}i then :\"#{sym}\"" }.join("\n")} end end CODEZ end |
.load_oses! ⇒ Object
Transforms OS_DATA into a case statement inside of the _os_sym method
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/double_agent/double_agent.rb', line 46 def self.load_oses! OS_PARSERS.clear.merge! Hash[OS_DATA.map { |sym, data| [sym, OSParser.new(sym, data)] }] module_eval <<-CODEZ def self._os_sym(user_agent) case user_agent.to_s #{OS_DATA.map { |sym, data| "when %r{#{data[:regex]}}i then :\"#{sym}\"" }.join("\n")} end end CODEZ end |
.method_missing(method, *args, &block) ⇒ Object
Forwards calls to a UserAgent object
28 29 30 |
# File 'lib/double_agent/double_agent.rb', line 28 def self.method_missing(method, *args, &block) parse(args.first).send(method) end |
.parse(user_agent_string) ⇒ Object
Returns a new UserAgent object
14 15 16 |
# File 'lib/double_agent/double_agent.rb', line 14 def self.parse(user_agent_string) UserAgent.new(user_agent_string) end |
.resource(klass, &user_agent_block) ⇒ Object
Mix DoubleAgent::Resource into klass. If klass doesn’t already have a user_agent method, you may pass a block which will be used to define one.
20 21 22 23 24 25 |
# File 'lib/double_agent/double_agent.rb', line 20 def self.resource(klass, &user_agent_block) klass.class_eval do define_method :user_agent, &user_agent_block end if user_agent_block klass.send :include, DoubleAgent::Resource end |