【发布时间】:2014-03-13 18:37:07
【问题描述】:
我有一个 Rails 应用程序,我正在使用 rack-mobile-detect 为手机提供不同的响应。现在我正在尝试缓存这些响应,问题是 rack-mobile-detect 标记平板电脑和手机,所以我无法键入 rack-mobile-detect 标头。在我看来,最好的办法是修改 rack-mobile-detect 以便它只检测手机,但我无法弄清楚。它不需要是 100%,如果一些不起眼的手机滑过,只要没有常规浏览器被标记为手机,这没什么大不了的。以下是相关代码:
def initialize(app, options = {})
@app = app
@regex_ua_targeted = options[:targeted] || /iphone|android|ipod|ipad/i
@regex_accept = /vnd\.wap/i
@regex_ua_catchall = options[:catchall] ||
Regexp.new('palm|blackberry|nokia|phone|midp|mobi|symbian|chtml|ericsson|minimo|' +
'audiovox|motorola|samsung|telit|upg1|windows ce|ucweb|astel|plucker|' +
'x320|x240|j2me|sgh|portable|sprint|docomo|kddi|softbank|android|mmp|' +
'pdxgw|netfront|xiino|vodafone|portalmmm|sagem|mot-|sie-|ipod|up\\.b|' +
'webos|amoi|novarra|cdm|alcatel|pocket|ipad|iphone|mobileexplorer|' +
'mobile', true)
end
def call(env)
device = nil
user_agent = env.fetch('HTTP_USER_AGENT', '')
device = Regexp.new(@regex_ua_targeted).match(user_agent)
device ||= env.keys.detect { |k| k.match(/^HTTP(.*)_PROFILE$/) } != nil
device ||= Regexp.new(@regex_accept).match(env.fetch('HTTP_ACCEPT','')) != nil
device ||= Regexp.new(@regex_ua_catchall).match(user_agent) != nil
if device
device = device.to_s
env[X_HEADER] = device
end
@app.call(env)
end
所以我的问题是:修改它以使其仅检测手机而不检测平板电脑的最佳方法是什么?有谁知道对此有效的正则表达式?非常感谢任何帮助。
【问题讨论】:
标签: ruby-on-rails regex mobile http-headers rack