def call(env)
      device = nil
      user_agent = env.fetch('HTTP_USER_AGENT', '')

      # First check for targeted devices and store the device token
      device = Regexp.new(@regex_ua_targeted).match(user_agent)

      # Fall-back on UAProf detection
      # http://www.developershome.com/wap/detection/detection.asp?page=profileHeader
      device ||= env.keys.detect { |k| k.match(/^HTTP(.*)_PROFILE$/) } != nil

      # Fall back to Accept header detection
      device ||= Regexp.new(@regex_accept).match(env.fetch('HTTP_ACCEPT','')) != nil

      # Fall back on catch-all User-Agent regex
      device ||= Regexp.new(@regex_ua_catchall).match(user_agent) != nil

      if device
        device = device.to_s

        env[X_HEADER] = device
        redirect = check_for_redirect(device)

        if redirect
          path = Rack::Utils.unescape(env['PATH_INFO'])
          return [301, {'Location' => redirect}, []] if redirect && path !~ /^#{redirect}/
        end
      end

      @app.call(env)
    end
def call(env)
      device = nil
      user_agent = env.fetch('HTTP_USER_AGENT', '')

      # First check for targeted devices and store the device token
      device = Regexp.new(@regex_ua_targeted).match(user_agent)

      # Fall-back on UAProf detection
      # http://www.developershome.com/wap/detection/detection.asp?page=profileHeader
      device ||= env.keys.detect { |k| k.match(/^HTTP(.*)_PROFILE$/) } != nil

      # Fall back to Accept header detection
      device ||= Regexp.new(@regex_accept).match(env.fetch('HTTP_ACCEPT','')) != nil

      # Fall back on catch-all User-Agent regex
      device ||= Regexp.new(@regex_ua_catchall).match(user_agent) != nil

      if device
        device = device.to_s

        env[X_HEADER] = device
        redirect = check_for_redirect(device)

        if redirect
          path = Rack::Utils.unescape(env['PATH_INFO'])
          return [301, {'Location' => redirect}, []] if redirect && path !~ /^#{redirect}/
        end
      end

      @app.call(env)
    end
Users can pass in a :targeted option, which should be a Regexp
 specifying which user-agent agent tokens should be specifically
 captured and passed along in the X_MOBILE_DEVICE variable.

 The :catchall option allows specifying a Regexp to catch mobile
 devices that fall through the other tests.
 Because the web app may be multithreaded, this method must
 create new Regexp instances to ensure thread safety.