Question

I have a rails app and I am using rack-mobile-detect to provide different responses to mobile phones. Now I am trying to cache these responses and the problem is that rack-mobile-detect tags tablets as well as phones so I can't key on the rack-mobile-detect header. It seems to me that the best thing to do is to modify rack-mobile-detect so that it only detects phones but I am having trouble figuring it out. It wouldn't need to be 100%, if a few obscure phones slipped through that wouldn't be a big deal so long as no regular browsers were tagged as phones. Here is the relevant code:

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

so my question is: What would be the best way to modify this so that it only detects phones and not tablets? Does anyone know of a regex which is effective for this purpose? Any help is greatly appreciated.

Was it helpful?

Solution

There is a gem that may be helpful for that https://github.com/talison/rack-mobile-detect Also I suggest considering this gem github.com/jistr/mobvious as it detects type of device(mobile, tablet, desktop)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top