Sha256: 4d7ca83e55d9679b3544bcec31fe24bb5de20339ea3ae74f9f40edb2a7ef11f3

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module Uaid
  def Uaid.supported_agents
    @supported ||= [
      /safari [345]/,
      /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
    attr_reader :agent, :engine, :product, :version, :supported
    
    def initialize(agent, extractor = Uaid.extractor)
      @agent, @extractor = agent, extractor
      @engine, @product, @version = @extractor.extract(@agent ? @agent.strip : '')
    end
    
    def identifier
      [engine, product, product + version].join(' ')
    end
    
    def 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+)\?$/
        engine == $1 || product == $1
      else
        super
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
uaid-0.1.3 lib/uaid/user_agent.rb