Skip to content

Instantly share code, notes, and snippets.

@jamesdaniels
Created February 13, 2014 03:28
Show Gist options
  • Save jamesdaniels/8969249 to your computer and use it in GitHub Desktop.
Save jamesdaniels/8969249 to your computer and use it in GitHub Desktop.
# Returns either true or false depending on whether or not the user agent of
# the device making the request is matched to a device in our regex.
def is_mobile_device?
request.user_agent.to_s.downcase =~ /silk|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/
end
# Can check for a specific user agent
# e.g., is_device?('iphone') or is_device?('mobileexplorer')
def is_device?(type)
request.user_agent.to_s.downcase.include?(type.to_s.downcase)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment