Parent

Rackamole::Utils::AgentDetect

Public Class Methods

browsers() click to toggle source

browsers...

   # File lib/rackamole/utils/agent_detect.rb, line 8
8:     def self.browsers() %w[Opera Firefox Chrome Safari MSIE]; end
parse( agent ) click to toggle source

Parses user agent to extract browser and machine info

    # File lib/rackamole/utils/agent_detect.rb, line 11
11:     def self.parse( agent )
12:       browsers.each do |browser|
13:         return extract( agent, browser ) if check?( agent, browser )
14:       end
15:       defaults
16:     end

Private Class Methods

check?( agent, browser ) click to toggle source

Check for known browsers

    # File lib/rackamole/utils/agent_detect.rb, line 22
22:       def self.check?( agent, browser )
23:         agent.match( /#{browser}/ )
24:       end
defaults() click to toggle source

Pre populate info hash

    # File lib/rackamole/utils/agent_detect.rb, line 27
27:       def self.defaults
28:         info = { :browser => {}, :machine => {} }
29:         ]name version].each { |t| info[:browser][t.to_sym] = "N/A" }
30:         ]platform os version local].each { |t| info[:machine][t.to_sym] = "N/A" }
31:         info
32:       end
extract( agent, browser ) click to toggle source

Extract machine and browser info

    # File lib/rackamole/utils/agent_detect.rb, line 35
35:       def self.extract( agent, browser )
36:         @info = defaults
37:         begin
38:           extract_browser( agent, browser )
39:           extract_platform( agent )    
40:         rescue => boom
41:           $stderr.puts "Unable to parse user agent `#{agent}"
42:           $stderr.puts boom
43:           boom.backtrace.each { |l| $stderr.puts l }
44:         end
45:         @info
46:       end
extract_browser( agent, browser ) click to toggle source

Extract browser and version

    # File lib/rackamole/utils/agent_detect.rb, line 49
49:       def self.extract_browser( agent, browser )
50:         @info[:browser][:name] = browser
51:         match = agent.match( /#{browser}[\/|\s]([\d|\.?]+)/ )        
52:         @info[:browser][:version] = match[1] if match and match[1]
53:       end
extract_platform( agent ) click to toggle source

Extracts machine info

    # File lib/rackamole/utils/agent_detect.rb, line 56
56:       def self.extract_platform( agent )
57:         match = agent.match( /\((.*)\)/ )
58:         return unless match and match[1]
59:         
60:         machine_info = match[1]
61:         tokens       = machine_info.split( ";" )
62:         unless tokens.empty?
63:           index    = 1
64:           platform = tokens[0].strip
65:           @info[:machine][:platform] = platform
66:           
67:           index += 1 if tokens[1].match( /[MSIE|U]/ )
68:           os = tokens[index].match( /(.+)\s([\w\d|\.]+)/ )   
69:           if os
70:             @info[:machine][:os]      = os[1].strip if os[1]
71:             @info[:machine][:version] = os[2].strip if os[2]
72:           end
73:         end
74:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.