Sha256: 54ca2ef23782ab618bf95ece573a80de1c86f9cd7886d8eb52b33ace5ca2ed58

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

module Uaid
  class UserAgent
    SUPPORTED, UNSUPPORTED = true, false
    
    attr_reader :agent, :name, :version
    
    def initialize(agent)
      @agent = agent
      @name, @version, @supported = extract(@agent ? @agent.strip : '')
    end
    
    def supported?
      @supported
    end
    
    def unsupported?
      !supported?
    end
    
    def method_missing(method, *args)
      if method.to_s =~ /^(\w+)\?$/ && EXTRACTIONS.find {|patterns, values| values.first == $1}
        name == $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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fivepointssolutions-uaid-0.0.0 lib/uaid/user_agent.rb
fivepointssolutions-uaid-0.0.1 lib/uaid/user_agent.rb