diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..959a8c91 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +pkg +coverage +doc \ No newline at end of file diff --git a/README.rdoc b/README.rdoc new file mode 100644 index 00000000..069aa515 --- /dev/null +++ b/README.rdoc @@ -0,0 +1,69 @@ += Browser + +Do some browser detection with Ruby. Includes ActionController integration. + +== Installation + + gem install browser + +== Usage + + require "rubygems" + require "browser" + + browser = Browser.new(:ua => "some string", :accept_language => "en-us") + browser.safari? + browser.opera? + browser.mobile? + browser.firefox? + browser.ie? + browser.ie6? # this goes up to 9 + browser.capable? # supports some CSS 3 + browser.platform # return :mac, :windows, :linux or :other + browser.mac? + browser.windows? + browser.linux? + browser.meta # an array with several attributes + browser.to_s # the meta info joined by space + +See the tests for more examples. + +=== Rails integration + +Just add it to the Gemfile or `environment.rb`, depending of your Rails version. + + gem "browser" #=> Rails 3 + config.gem "browser" #=> Rails 2 + +This adds a helper method called `browser`, that inspects your current user agent. + + <% if browser.ie6? %> +

Your're running an older IE version. Please update it!

+ <% end %> + +== Maintainer + +* Nando Vieira - http://nandovieira.com.br + +== License + +(The MIT License) + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..7d430a8a --- /dev/null +++ b/Rakefile @@ -0,0 +1,48 @@ +require "rcov/rcovtask" +require "rake/testtask" +require "rake/rdoctask" +require "lib/browser/version" + +Rcov::RcovTask.new do |t| + t.test_files = FileList["test/**/*_test.rb"] + t.rcov_opts = ["--sort coverage", "--exclude .gem"] + + t.output_dir = "coverage" + t.libs << "test" + t.verbose = true +end + +Rake::TestTask.new do |t| + t.libs << "lib" + t.libs << "test" + t.test_files = FileList["test/**/*_test.rb"] + t.verbose = true + t.ruby_opts = %w[-rubygems] +end + +Rake::RDocTask.new do |rdoc| + rdoc.main = "README.rdoc" + rdoc.rdoc_dir = "doc" + rdoc.title = "Browser API" + rdoc.options += %w[ --line-numbers --inline-source --charset utf-8 ] + rdoc.rdoc_files.include("README.rdoc") + rdoc.rdoc_files.include("lib/**/*.rb") +end + +begin + require "jeweler" + + Jeweler::Tasks.new do |gem| + gem.name = "browser" + gem.email = "fnando.vieira@gmail.com" + gem.homepage = "http://github.com/fnando/browser" + gem.authors = ["Nando Vieira"] + gem.version = Browser::Version::STRING + gem.summary = "Do some browser detection with Ruby." + gem.files = FileList["README.rdoc", "{lib,test}/**/*", "Rakefile"] + end + + Jeweler::GemcutterTasks.new +rescue LoadError => e + puts "You need to install jeweler to build this gem." +end diff --git a/browser.gemspec b/browser.gemspec new file mode 100644 index 00000000..5948e713 --- /dev/null +++ b/browser.gemspec @@ -0,0 +1,41 @@ +# Generated by jeweler +# DO NOT EDIT THIS FILE DIRECTLY +# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command +# -*- encoding: utf-8 -*- + +Gem::Specification.new do |s| + s.name = %q{browser} + s.version = "0.1.0" + + s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= + s.authors = ["Nando Vieira"] + s.date = %q{2010-07-16} + s.email = %q{fnando.vieira@gmail.com} + s.extra_rdoc_files = [ + "README.rdoc" + ] + s.files = [ + "README.rdoc", + "Rakefile", + "lib/browser.rb", + "lib/browser/action_controller.rb", + "lib/browser/version.rb", + "test/browser_test.rb" + ] + s.homepage = %q{http://github.com/fnando/browser} + s.rdoc_options = ["--charset=UTF-8"] + s.require_paths = ["lib"] + s.rubygems_version = %q{1.3.7} + s.summary = %q{Do some browser detection with Ruby.} + + if s.respond_to? :specification_version then + current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION + s.specification_version = 3 + + if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then + else + end + else + end +end + diff --git a/lib/browser.rb b/lib/browser.rb new file mode 100644 index 00000000..6147c114 --- /dev/null +++ b/lib/browser.rb @@ -0,0 +1,319 @@ +class Browser + # Add Rails helper if ActionController::Base is available + require "browser/action_controller" if defined?(ActionController::Base) + + # Set browser's UA string. + attr_accessor :user_agent + + # Set browser's preferred language + attr_writer :accept_language + + alias :ua :user_agent + alias :ua= :user_agent= + + NAMES = { + :android => "Android", + :blackberry => "BlackBerry", + :chrome => "Chrome", + :firefox => "Firefox", + :ie => "Internet Explorer", + :ipad => "iPad", + :iphone => "iPhone", + :ipod => "iPod Touch", + :opera => "Opera", + :other => "Other" + } + + LANGUAGES = { + "af" => "Afrikaans", + "sq" => "Albanian", + "eu" => "Basque", + "bg" => "Bulgarian", + "be" => "Byelorussian", + "ca" => "Catalan", + "zh" => "Chinese", + "zh-cn" => "Chinese/China", + "zh-tw" => "Chinese/Taiwan", + "zh-hk" => "Chinese/Hong Kong", + "zh-sg" => "Chinese/singapore", + "hr" => "Croatian", + "cs" => "Czech", + "da" => "Danish", + "nl" => "Dutch", + "nl-nl" => "Dutch/Netherlands", + "nl-be" => "Dutch/Belgium", + "en" => "English", + "en-gb" => "English/United Kingdom", + "en-us" => "English/United States", + "en-au" => "English/Australian", + "en-ca" => "English/Canada", + "en-nz" => "English/New Zealand", + "en-ie" => "English/Ireland", + "en-za" => "English/South Africa", + "en-jm" => "English/Jamaica", + "en-bz" => "English/Belize", + "en-tt" => "English/Trinidad", + "et" => "Estonian", + "fo" => "Faeroese", + "fa" => "Farsi", + "fi" => "Finnish", + "fr" => "French", + "fr-be" => "French/Belgium", + "fr-fr" => "French/France", + "fr-ch" => "French/Switzerland", + "fr-ca" => "French/Canada", + "fr-lu" => "French/Luxembourg", + "gd" => "Gaelic", + "gl" => "Galician", + "de" => "German", + "de-at" => "German/Austria", + "de-de" => "German/Germany", + "de-ch" => "German/Switzerland", + "de-lu" => "German/Luxembourg", + "de-li" => "German/Liechtenstein", + "el" => "Greek", + "he" => "Hebrew", + "he-il" => "Hebrew/Israel", + "hi" => "Hindi", + "hu" => "Hungarian", + "ie-ee" => "Internet Explorer/Easter Egg", + "is" => "Icelandic", + "id" => "Indonesian", + "in" => "Indonesian", + "ga" => "Irish", + "it" => "Italian", + "it-ch" => "Italian/ Switzerland", + "ja" => "Japanese", + "km" => "Khmer", + "km-kh" => "Khmer/Cambodia", + "ko" => "Korean", + "lv" => "Latvian", + "lt" => "Lithuanian", + "mk" => "Macedonian", + "ms" => "Malaysian", + "mt" => "Maltese", + "no" => "Norwegian", + "pl" => "Polish", + "pt" => "Portuguese", + "pt-br" => "Portuguese/Brazil", + "rm" => "Rhaeto-Romanic", + "ro" => "Romanian", + "ro-mo" => "Romanian/Moldavia", + "ru" => "Russian", + "ru-mo" => "Russian /Moldavia", + "gd" => "Scots Gaelic", + "sr" => "Serbian", + "sk" => "Slovack", + "sl" => "Slovenian", + "sb" => "Sorbian", + "es" => "Spanish", + "es-do" => "Spanish", + "es-ar" => "Spanish/Argentina", + "es-co" => "Spanish/Colombia", + "es-mx" => "Spanish/Mexico", + "es-es" => "Spanish/Spain", + "es-gt" => "Spanish/Guatemala", + "es-cr" => "Spanish/Costa Rica", + "es-pa" => "Spanish/Panama", + "es-ve" => "Spanish/Venezuela", + "es-pe" => "Spanish/Peru", + "es-ec" => "Spanish/Ecuador", + "es-cl" => "Spanish/Chile", + "es-uy" => "Spanish/Uruguay", + "es-py" => "Spanish/Paraguay", + "es-bo" => "Spanish/Bolivia", + "es-sv" => "Spanish/El salvador", + "es-hn" => "Spanish/Honduras", + "es-ni" => "Spanish/Nicaragua", + "es-pr" => "Spanish/Puerto Rico", + "sx" => "Sutu", + "sv" => "Swedish", + "sv-se" => "Swedish/Sweden", + "sv-fi" => "Swedish/Finland", + "ts" => "Thai", + "tn" => "Tswana", + "tr" => "Turkish", + "uk" => "Ukrainian", + "ur" => "Urdu", + "vi" => "Vietnamese", + "xh" => "Xshosa", + "ji" => "Yiddish", + "zu" => "Zulu" + } + + # Create a new browser instance and set + # the UA and Accept-Language headers. + # + # browser = Browser.new({ + # :ua => "Safari", + # :accept_language => "pt-br" + # }) + # + def initialize(options = {}, &block) + @user_agent = (options[:user_agent] || options[:ua]).to_s + @accept_language = options[:accept_language].to_s + + yield self if block_given? + end + + # Get readable browser name. + def name + NAMES[id] + end + + # Return a symbol that identifies the browser. + def id + case + when chrome? then :chrome + when iphone? then :iphone + when ipad? then :ipad + when ipod? then :ipod + when ie? then :ie + when opera? then :opera + when firefox? then :firefox + when android? then :android + when blackberry? then :blackberry + else + :other + end + end + + # Return an array with all preferred languages that this browser accepts. + def accept_language + @accept_language.gsub(/;q=[\d.]+/, "").split(",").collect {|l| l.downcase.gsub(/\s/m, "")} + end + + # Return major version. + def version + full_version.to_s.split(".").first + end + + # Return the full version. + def full_version + _, v = *ua.match(/(?:Version|MSIE|Opera|Firefox|Chrome|BlackBerry[^\/]+)[\/ ]([\d.]+)/) + v || "0.0" + end + + # Return true if browser supports some CSS 3 (Safari, Firefox, Opera & IE7+). + def capable? + safari? || firefox? || opera? || (ie? && version >= "7") + end + + # Detect if browser is mobile. + def mobile? + !!(ua =~ /(Mobile|Symbian|MIDP|Windows CE)/) || blackberry? + end + + # Detect if browser is BlackBerry + def blackberry? + !!(ua =~ /BlackBerry/) + end + + # Detect if browser is Android. + def android? + !!(ua =~ /Android/) + end + + # Detect if browser is iPhone. + def iphone? + !!(ua =~ /iPhone/) + end + + # Detect if browser is iPad. + def ipad? + !!(ua =~ /iPad/) + end + + # Detect if browser is iPod. + def ipod? + !!(ua =~ /iPod/) + end + + # Detect if browser is Safari. + def safari? + !!(ua =~ /Safari/) + end + + # Detect if browser is Firefox. + def firefox? + !!(ua =~ /Firefox/) + end + + # Detect if browser is Chrome. + def chrome? + !!(ua =~ /Chrome/) + end + + # Detect if browser is Internet Explorer. + def ie? + !!(ua =~ /MSIE/ && ua !~ /Opera/) + end + + # Detect if browser is Internet Explorer 6. + def ie6? + ie? && version == "6" + end + + # Detect if browser is Internet Explorer 7. + def ie7? + ie? && version == "7" + end + + # Detect if browser is Internet Explorer 8. + def ie8? + ie? && version == "8" + end + + # Detect if browser is Internet Explorer 9. + def ie9? + ie? && version == "9" + end + + # Detect if browser is Opera. + def opera? + !!(ua =~ /Opera/) + end + + # Detect if current platform is Macintosh. + def mac? + !!(ua =~ /Mac OS X/) + end + + # Detect if current platform is Windows. + def windows? + !!(ua =~ /Windows/) + end + + # Detect if current platform is Linux flavor. + def linux? + !!(ua =~ /Linux/) + end + + # Return the platform. + def platform + case + when linux? then :linux + when mac? then :mac + when windows? then :windows + else + :other + end + end + + # Return a meta info about this browser. + def meta + Array.new.tap do |m| + m << id + m << "safari safari#{version}" if safari? + m << "#{id}#{version}" unless safari? + m << platform + m << "capable" if capable? + m << "mobile" if mobile? + end + end + + # Return meta representation as string. + def to_s + meta.join(" ") + end +end diff --git a/lib/browser/action_controller.rb b/lib/browser/action_controller.rb new file mode 100644 index 00000000..1e29894c --- /dev/null +++ b/lib/browser/action_controller.rb @@ -0,0 +1,17 @@ +class Browser + module ActionController # :nodoc: all + def self.included(base) + base.send :helper_method, :browser + end + + private + def browser + @browser ||= Browser.new( + :accept_language => request.headers["ACCEPT_LANGUAGE"], + :ua => request.headers["USER_AGENT"] + ) + end + end +end + +ActionController::Base.send :include, Browser::ActionController diff --git a/lib/browser/version.rb b/lib/browser/version.rb new file mode 100644 index 00000000..8f24b053 --- /dev/null +++ b/lib/browser/version.rb @@ -0,0 +1,8 @@ +class Browser + module Version + MAJOR = 0 + MINOR = 1 + PATCH = 0 + STRING = "#{MAJOR}.#{MINOR}.#{PATCH}" + end +end \ No newline at end of file diff --git a/test/browser_test.rb b/test/browser_test.rb new file mode 100644 index 00000000..e387723d --- /dev/null +++ b/test/browser_test.rb @@ -0,0 +1,272 @@ +require "test/unit" + +begin + require "action_controller" +rescue LoadError => e + require "rubygems" + require "action_controller" +end + +require "browser" + + +class BrowserTest < Test::Unit::TestCase + IPHONE = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/1A542a Safari/419.3" + IPOD = "Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3A100a Safari/419.3" + IPAD = "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10" + IE6 = "Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)" + IE7 = "Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)" + IE8 = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.2; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)" + IE9 = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)" + OPERA = "Opera/9.99 (Windows NT 5.1; U; pl) Presto/9.9.9" + FIREFOX = "Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.2) Gecko/20121223 Ubuntu/9.25 (jaunty) Firefox/3.8" + CHROME = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4" + ANDROID = "Android SDK 1.5r3: Mozilla/5.0 (Linux; U; Android 1.5; de-; sdk Build/CUPCAKE) AppleWebkit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1" + BLACKBERRY = "BlackBerry7100i/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/103" + + def setup + @browser = Browser.new + end + + def test_yield_self_when_block_is_given + browser = nil + Browser.new {|b| browser = b } + assert_kind_of Browser, browser + end + + def test_respond_to_ua_methods + assert @browser.respond_to?(:ua) + assert @browser.respond_to?(:ua=) + end + + def test_delegate_ua_methods + @browser.user_agent = "Safari" + assert_equal "Safari", @browser.ua + + @browser.ua = "Mozilla" + assert_equal "Mozilla", @browser.user_agent + end + + def test_set_accept_language_while_instantiating_object + @browser = Browser.new(:accept_language => "pt-br") + assert_equal ["pt-br"], @browser.accept_language + end + + def test_set_user_agent_while_instantianting_object + @browser = Browser.new(:ua => "Safari") + assert_equal "Safari", @browser.ua + + @browser = Browser.new(:user_agent => "Chrome") + assert_equal "Chrome", @browser.ua + end + + def test_detect_iphone + @browser.ua = IPHONE + + assert_equal "iPhone", @browser.name + assert @browser.iphone? + assert @browser.safari? + assert @browser.mobile? + assert @browser.capable? + assert_equal "3.0", @browser.full_version + assert_equal "3", @browser.version + end + + def test_detect_ipod + @browser.ua = IPOD + + assert_equal "iPod Touch", @browser.name + assert @browser.ipod? + assert @browser.safari? + assert @browser.mobile? + assert @browser.capable? + assert_equal "3.0", @browser.full_version + assert_equal "3", @browser.version + end + + def test_detect_ipad + @browser.ua = IPAD + + assert_equal "iPad", @browser.name + assert @browser.ipad? + assert @browser.safari? + assert @browser.capable? + assert_equal "4.0.4", @browser.full_version + assert_equal "4", @browser.version + end + + def test_detect_ie6 + @browser.ua = IE6 + + assert_equal "Internet Explorer", @browser.name + assert @browser.ie? + assert @browser.ie6? + assert @browser.capable? == false + assert_equal "6.0", @browser.full_version + assert_equal "6", @browser.version + end + + def test_detect_ie7 + @browser.ua = IE7 + + assert_equal "Internet Explorer", @browser.name + assert @browser.ie? + assert @browser.ie7? + assert @browser.capable? + assert_equal "7.0", @browser.full_version + assert_equal "7", @browser.version + end + + def test_detect_ie8 + @browser.ua = IE8 + + assert_equal "Internet Explorer", @browser.name + assert @browser.ie? + assert @browser.ie8? + assert @browser.capable? + assert_equal "8.0", @browser.full_version + assert_equal "8", @browser.version + end + + def test_detect_ie9 + @browser.ua = IE9 + + assert_equal "Internet Explorer", @browser.name + assert @browser.ie? + assert @browser.ie9? + assert @browser.capable? + assert_equal "9.0", @browser.full_version + assert_equal "9", @browser.version + end + + def test_detect_opera + @browser.ua = OPERA + + assert_equal "Opera", @browser.name + assert @browser.opera? + assert @browser.capable? + assert_equal "9.99", @browser.full_version + assert_equal "9", @browser.version + end + + def test_detect_firefox + @browser.ua = FIREFOX + + assert_equal "Firefox", @browser.name + assert @browser.firefox? + assert @browser.capable? + assert_equal "3.8", @browser.full_version + assert_equal "3", @browser.version + end + + def test_detect_chrome + @browser.ua = CHROME + + assert_equal "Chrome", @browser.name + assert @browser.chrome? + assert @browser.safari? + assert @browser.capable? + assert_equal "5.0.375.99", @browser.full_version + assert_equal "5", @browser.version + end + + def test_detect_android + @browser.ua = ANDROID + + assert_equal "Android", @browser.name + assert @browser.android? + assert @browser.safari? + assert @browser.mobile? + assert @browser.capable? + assert_equal "3.1.2", @browser.full_version + assert_equal "3", @browser.version + end + + def test_detect_blackberry + @browser.ua = BLACKBERRY + + assert_equal "BlackBerry", @browser.name + assert @browser.blackberry? + assert @browser.mobile? + assert @browser.capable? == false + assert_equal "4.1.0", @browser.full_version + assert_equal "4", @browser.version + end + + def test_detect_other_mobiles + @browser.ua = "Symbian OS" + assert @browser.mobile? + + @browser.ua = "MIDP-2.0" + assert @browser.mobile? + + @browser.ua = "Windows CE" + assert @browser.mobile? + end + + def test_return_a_zero_version + @browser.ua = "Bot" + assert_equal "0.0", @browser.full_version + assert_equal "0", @browser.version + end + + def test_meta + @browser.ua = CHROME + assert_kind_of Array, @browser.meta + end + + def test_return_string_representation + @browser.ua = CHROME + assert_equal "chrome safari safari5 mac capable", @browser.to_s + end + + def test_return_string_representation_for_mobile + @browser.ua = IPHONE + assert_equal "iphone safari safari3 mac capable mobile", @browser.to_s + end + + def test_return_string_representation_for_handcap + @browser.ua = IE6 + assert_equal "ie ie6 windows", @browser.to_s + end + + def test_detect_unknown_id + @browser.ua = "Unknown" + assert_equal :other, @browser.id + end + + def test_detect_unknown_name + @browser.ua = "Unknown" + assert_equal "Other", @browser.name + end + + def test_detect_mac_platform + @browser.ua = "Mac OS X" + assert_equal :mac, @browser.platform + end + + def test_detect_windows_platform + @browser.ua = "Windows" + assert_equal :windows, @browser.platform + end + + def test_detect_linux_platform + @browser.ua = "Linux" + assert_equal :linux, @browser.platform + end + + def test_detect_unknown_platform + @browser.ua = "Unknown" + assert_equal :other, @browser.platform + end + + def test_return_all_known_languages + @browser.accept_language = "en-us,en;q=0.8,pt-br;q=0.5,pt;q=0.3" + assert_equal ["en-us", "en", "pt-br", "pt"], @browser.accept_language + end + + def test_pimp_action_controller + methods = ActionController::Base.private_instance_methods.collect {|m| m.to_sym} + assert methods.include?(:browser) + end +end