Skip to content

Instantly share code, notes, and snippets.

@zhuravel
Created September 9, 2013 18:35
Show Gist options
  • Save zhuravel/6499666 to your computer and use it in GitHub Desktop.
Save zhuravel/6499666 to your computer and use it in GitHub Desktop.
#----------------------------------------------------------------------+
# Port of Device.js library |
# https://github.com/matthewhudson/device.js/blob/master/lib/device.js |
#----------------------------------------------------------------------+
class Device
def initialize(user_agent)
@user_agent = user_agent
end
def mobile?
iphone? || ipod? || android_phone? || windows_phone? || blackberry_phone?
end
def tablet?
ipad? || android_tablet? || blackberry_tablet? || windows_tablet?
end
def desktop?
!mobile? && !tablet?
end
%w(iphone ipod ipad android windows).each do |device|
define_method(device + '?') { find device }
end
def ios?
iphone? || ipod? || ipad?
end
def android_phone?
android? && find('mobile')
end
def android_tablet?
android? && !find('mobile')
end
def blackberry?
find('blackberry') || find('bb10') || find('rim')
end
def blackberry_phone?
blackberry? && !find('tablet')
end
def blackberry_tablet?
blackberry? && find('tablet')
end
def windows_phone?
windows? && find('phone')
end
def windows_tablet?
windows? && find('touch')
end
private
def find(needle)
@user_agent.downcase.include? needle.downcase
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment