我有一个rails应用程序,我正在使用机架-移动检测为手机提供不同的响应。现在我试图缓存这些响应,问题是机架移动检测标签、平板电脑以及手机,所以我不能点击机架移动检测头。在我看来,最好的做法是修改机架-移动检测,使它只检测手机,但我有困难找出它。它不需要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所以我的问题是:怎样才是最好的方法来修改它,使它只检测手机,而不是平板电脑?有谁知道有这样一种判决是有效的吗?任何帮助都是非常感谢的。
发布于 2014-03-13 18:59:30
有一个创业板可能对这个https://github.com/talison/rack-mobile-detect很有帮助,我还建议考虑一下这个gemgithub.com/jistr/mob何时能检测到设备的类型(移动、平板、桌面)。
https://stackoverflow.com/questions/22387904
复制相似问题