pax_global_header00006660000000000000000000000064121216003010014474gustar00rootroot0000000000000052 comment=406888d721aae963f2cfd1b3459029918c053e3c ruby-rack-mobile-detect-0.4.0/000077500000000000000000000000001212160030100161075ustar00rootroot00000000000000ruby-rack-mobile-detect-0.4.0/.document000066400000000000000000000000741212160030100177270ustar00rootroot00000000000000README.rdoc lib/**/*.rb bin/* features/**/*.feature LICENSE ruby-rack-mobile-detect-0.4.0/CHANGELOG.md000066400000000000000000000010721212160030100177200ustar00rootroot00000000000000### 0.4.0: Jun 7, 2012 ### * Added iPad as a targeted device, thank you sansari (https://github.com/sansari) ### 0.3.0: Mar 12, 2011 ### * Modified catch-all based: https://github.com/brendanlim/mobile-fu ### 0.2.0: Mar 14, 2010 ### * Added redirect_to option from github.com/joren * Added redirect_map option * Added dependency on rack * Added more test for redirect options * Updated dir structure: lib/rack-mobile-detect.rb -> lib/rack/mobile-detect.rb * Added note on require syntax: require 'rack/mobile-detect' * Updated tests, echo_env.rb to reference new file ruby-rack-mobile-detect-0.4.0/LICENSE000066400000000000000000000020571212160030100171200ustar00rootroot00000000000000The MIT License Copyright (c) 2009 Tom Alison 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. ruby-rack-mobile-detect-0.4.0/README.md000066400000000000000000000102201212160030100173610ustar00rootroot00000000000000Install ======= sudo gem install rack-mobile-detect In your code: require 'rack/mobile-detect' Overview ======== `Rack::MobileDetect` is Rack middleware for ruby webapps that detects mobile devices. It adds an `X_MOBILE_DEVICE` header to the request if a device is detected. The strategy for detecting a mobile device is as follows: ### Targeted Detection ### Search for a 'targeted' mobile device. A targeted mobile device is a device you may want to provide special content to because it has advanced capabilities - for example and iPhone or Android phone. Targeted mobile devices are detected via a `Regexp` applied against the HTTP User-Agent header. By default, the targeted devices are iPhone, iPad, Android and iPod. If a targeted device is detected, the token match from the regular expression will be the value passed in the `X_MOBILE_DEVICE` header, i.e.: `X_MOBILE_DEVICE: iPhone` ### UAProf Detection ### Search for a UAProf device. More about UAProf detection can be found [here](http://www.developershome.com/wap/detection/detection.asp?page=profileHeader). If a UAProf device is detected, it will have `X_MOBILE_DEVICE: true` ### Accept Header Detection ### Look at the HTTP Accept header to see if the device accepts WAP content. More information about this form of detection is found [here](http://www.developershome.com/wap/detection/detection.asp?page=httpHeaders). Any device detected using this method will have `X_MOBILE_DEVICE: true` ### Catchall Detection ### Use a 'catch-all' regex. The current catch-all regex was taken from the [mobile-fu project](http://github.com/brendanlim/mobile-fu) Any device detected using this method will have `X_MOBILE_DEVICE: true` Notes ===== If none of the detection methods detect a mobile device, the `X_MOBILE_DEVICE` header will be _absent_. Note that `Rack::MobileDetect::X_HEADER` holds the string 'X\_MOBILE\_DEVICE' that is inserted into the request headers. Usage ===== use Rack::MobileDetect This allows you to do mobile device detection with the defaults. use Rack::MobileDetect, :targeted => /SCH-\w*$|[Bb]lack[Bb]erry\w*/ In this usage you can set the value of the regular expression used to target particular devices. This regular expression captures Blackberry and Samsung SCH-* model phones. For example, if a phone with the user-agent: 'BlackBerry9000/4.6.0.167 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/102' connects, the value of `X_MOBILE_DEVICE` will be set to 'BlackBerry9000' use Rack::MobileDetect, :catchall => /mydevice/i This allows you to limit the catchall expression to only the device list you choose. Redirects ========= use Rack::MobileDetect, :redirect_to => 'http://m.example.com/' This allows you to choose a custom redirect path any time a mobile device is detected. use Rack::MobileDetect, :targeted => /BlackBerry|iPhone/, :redirect_map => { 'BlackBerry' => 'http://m.example.com/blackberry', 'iPhone' => 'http://m.example.com/iphone' } This allows you to map specific redirect URLs to targeted devices. The key in the redirect_map should be the value of the matched pattern. use Rack::MobileDetect, :targeted => /BlackBerry|iPhone/, :redirect_map => { 'BlackBerry' => 'http://m.example.com/blackberry', 'iPhone' => 'http://m.example.com/iphone' }, :redirect_to => 'http://m.example.com/' This allows you to map targeted devices to specific URLs. Non-targeted mobile devices will redirect to the url specified by redirect_to. In the example above, BlackBerrys and iPhones get redirected to device-specific URLs. All other mobile devices get redirected to 'http://m.example.com'. Utils ===== A Sinatra app called echo_env.rb is available in the [util/](http://github.com/talison/rack-mobile-detect/tree/master/util/) directory. Hit this app with a mobile device to see the various HTTP headers and whether or not the `X_MOBILE_DEVICE` header is added by `Rack::MobileDetect`. See the [unit test source code](http://github.com/talison/rack-mobile-detect/tree/master/test/) for more info. ruby-rack-mobile-detect-0.4.0/Rakefile000066400000000000000000000032751212160030100175630ustar00rootroot00000000000000require 'rubygems' require 'rake' begin require 'jeweler' Jeweler::Tasks.new do |gem| gem.name = "rack-mobile-detect" gem.summary = %Q{Rack middleware for ruby webapps to detect mobile devices.} gem.description = %Q{Rack::MobileDetect detects mobile devices and adds an X_MOBILE_DEVICE header to the request if a mobile device is detected. Specific devices can be targeted with custom Regexps and redirect support is available.} gem.email = "accounts@majortom.fastmail.us" gem.homepage = "http://github.com/talison/rack-mobile-detect" gem.authors = ["Tom Alison"] gem.add_development_dependency("shoulda", ">= 0") gem.add_dependency("rack") # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings end Jeweler::GemcutterTasks.new rescue LoadError puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler" end require 'rake/testtask' Rake::TestTask.new(:test) do |test| test.libs << 'lib' << 'test' test.pattern = 'test/**/test_*.rb' test.verbose = true end begin require 'rcov/rcovtask' Rcov::RcovTask.new do |test| test.libs << 'test' test.pattern = 'test/**/test_*.rb' test.verbose = true end rescue LoadError task :rcov do abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov" end end task :test => :check_dependencies task :default => :test require 'rake/rdoctask' Rake::RDocTask.new do |rdoc| version = File.exist?('VERSION') ? File.read('VERSION') : "" rdoc.rdoc_dir = 'rdoc' rdoc.title = "rack-mobile-detect #{version}" rdoc.rdoc_files.include('README*') rdoc.rdoc_files.include('lib/**/*.rb') end ruby-rack-mobile-detect-0.4.0/TODO000066400000000000000000000001001212160030100165660ustar00rootroot00000000000000* Allow options to disable UAProf detection, Accept detection ruby-rack-mobile-detect-0.4.0/VERSION.yml000066400000000000000000000000541212160030100177560ustar00rootroot00000000000000--- :patch: 0 :major: 0 :build: :minor: 4 ruby-rack-mobile-detect-0.4.0/lib/000077500000000000000000000000001212160030100166555ustar00rootroot00000000000000ruby-rack-mobile-detect-0.4.0/lib/rack/000077500000000000000000000000001212160030100175755ustar00rootroot00000000000000ruby-rack-mobile-detect-0.4.0/lib/rack/mobile-detect.rb000066400000000000000000000161451212160030100226460ustar00rootroot00000000000000# The MIT License # # Copyright (c) 2009 Tom Alison # # 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. require 'rack' module Rack # # Full project at http://github.com/talison/rack-mobile-detect # # Rack::MobileDetect detects mobile devices and adds an # X_MOBILE_DEVICE header to the request if a mobile device is # detected. The strategy for detecting a mobile device is as # follows: # # 1. Search for a 'targeted' mobile device. A targeted mobile device # is a mobile device you may want to provide special content to # because it has advanced capabilities - for example an iPad, iPhone or # Android device. Targeted mobile devices are detected via a Regexp # applied against the HTTP User-Agent header. # # By default, the targeted devices are iPhone, Android, iPad and iPod. If # a targeted device is detected, the token match from the regular # expression will be the value passed in the X_MOBILE_DEVICE header, # i.e.: X_MOBILE_DEVICE: iPhone # # 2. Search for a UAProf device. More about UAProf detection can be # found here: # http://www.developershome.com/wap/detection/detection.asp?page=profileHeader # # If a UAProf device is detected, the value of X_MOBILE_DEVICE is # simply set to 'true'. # # 3. Look at the HTTP Accept header to see if the device accepts WAP # content. More information about this form of detection is found # here: # http://www.developershome.com/wap/detection/detection.asp?page=httpHeaders # # Any device detected using this method will have X_MOBILE_DEVICE # set to 'true'. # # 4. Use a 'catch-all' regex. The current catch-all regex was taken # from the mobile-fu project. See: # http://github.com/brendanlim/mobile-fu # # Any device detected using this method will have X_MOBILE_DEVICE # set to 'true'. # # If none of the detection methods detected a mobile device, the # X_MOBILE_DEVICE header will be absent. # # Note that Rack::MobileDetect::X_HEADER holds the string # 'X_MOBILE_DEVICE' that is inserted into the request headers. # # Usage: # use Rack::MobileDetect # # This allows you to do mobile device detection with the defaults. # # use Rack::MobileDetect, :targeted => /SCH-\w*$|[Bb]lack[Bb]erry\w*/ # # In this usage you can set the value of the regular expression used # to target particular devices. This regular expression captures # Blackberry and Samsung SCH-* model phones. For example, if a phone # with the user-agent: 'BlackBerry9000/4.6.0.167 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/102' # connects, the value of X_MOBILE_DEVICE will be set to 'BlackBerry9000' # # use Rack::MobileDetect, :catchall => /mydevice/i # # This allows you to limit the catchall expression to only the # device list you choose. # # See the unit test source code for more info. # # Author: Tom Alison (tom.alison at gmail.com) # License: MIT # class MobileDetect X_HEADER = 'X_MOBILE_DEVICE' # Users can pass in a :targeted option, which should be a Regexp # specifying which user-agent agent tokens should be specifically # captured and passed along in the X_MOBILE_DEVICE variable. # # The :catchall option allows specifying a Regexp to catch mobile # devices that fall through the other tests. def initialize(app, options = {}) @app = app # @ua_targeted holds a list of user-agent tokens that are # captured. Captured tokens are passed through in the # environment variable. These are special mobile devices that # may have special rendering capabilities for you to target. @regex_ua_targeted = options[:targeted] || /iphone|android|ipod|ipad/i # Match mobile content in Accept header: # http://www.developershome.com/wap/detection/detection.asp?page=httpHeaders @regex_accept = /vnd\.wap/i # From mobile-fu: http://github.com/brendanlim/mobile-fu @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) # A URL that specifies a single redirect-url for any device @redirect_to = options[:redirect_to] # A mapping of devices to redirect URLs, for targeted devices @redirect_map = options[:redirect_map] end # Because the web app may be multithreaded, this method must # create new Regexp instances to ensure thread safety. def call(env) device = nil user_agent = env.fetch('HTTP_USER_AGENT', '') # First check for targeted devices and store the device token device = Regexp.new(@regex_ua_targeted).match(user_agent) # Fall-back on UAProf detection # http://www.developershome.com/wap/detection/detection.asp?page=profileHeader device ||= env.keys.detect { |k| k.match(/^HTTP(.*)_PROFILE$/) } != nil # Fall back to Accept header detection device ||= Regexp.new(@regex_accept).match(env.fetch('HTTP_ACCEPT','')) != nil # Fall back on catch-all User-Agent regex device ||= Regexp.new(@regex_ua_catchall).match(user_agent) != nil if device device = device.to_s env[X_HEADER] = device redirect = check_for_redirect(device) if redirect path = Rack::Utils.unescape(env['PATH_INFO']) return [301, {'Location' => redirect}, []] if redirect && path !~ /^#{redirect}/ end end @app.call(env) end # Checks to see if any redirect options were passed in # and returns the appropriate redirect or nil (if no redirect requested) def check_for_redirect(device) # Find the device-specific redirect in the map, if exists return @redirect_map[device] if @redirect_map && @redirect_map.has_key?(device) # Return global redirect, or nil return @redirect_to end end end ruby-rack-mobile-detect-0.4.0/metadata.yml000066400000000000000000000041761212160030100204220ustar00rootroot00000000000000--- !ruby/object:Gem::Specification name: rack-mobile-detect version: !ruby/object:Gem::Version prerelease: false segments: - 0 - 4 - 0 version: 0.4.0 platform: ruby authors: - Tom Alison autorequire: bindir: bin cert_chain: [] date: 2012-06-07 00:00:00 -07:00 default_executable: dependencies: - !ruby/object:Gem::Dependency name: shoulda prerelease: false requirement: &id001 !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version segments: - 0 version: "0" type: :development version_requirements: *id001 - !ruby/object:Gem::Dependency name: rack prerelease: false requirement: &id002 !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version segments: - 0 version: "0" type: :runtime version_requirements: *id002 description: |- Rack::MobileDetect detects mobile devices and adds an X_MOBILE_DEVICE header to the request if a mobile device is detected. Specific devices can be targeted with custom Regexps and redirect support is available. email: accounts@majortom.fastmail.us executables: [] extensions: [] extra_rdoc_files: - LICENSE - README.md - TODO files: - .document - CHANGELOG.md - LICENSE - README.md - Rakefile - TODO - VERSION.yml - lib/rack/mobile-detect.rb - rack-mobile-detect.gemspec - test/helper.rb - test/test_rack-mobile-detect.rb - util/echo_env.rb has_rdoc: true homepage: http://github.com/talison/rack-mobile-detect licenses: [] post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version segments: - 0 version: "0" required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - ">=" - !ruby/object:Gem::Version segments: - 0 version: "0" requirements: [] rubyforge_project: rubygems_version: 1.3.6 signing_key: specification_version: 3 summary: Rack middleware for ruby webapps to detect mobile devices. test_files: - test/helper.rb - test/test_rack-mobile-detect.rb ruby-rack-mobile-detect-0.4.0/rack-mobile-detect.gemspec000066400000000000000000000035271212160030100231160ustar00rootroot00000000000000# Generated by jeweler # DO NOT EDIT THIS FILE DIRECTLY # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' # -*- encoding: utf-8 -*- Gem::Specification.new do |s| s.name = %q{rack-mobile-detect} s.version = "0.4.0" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Tom Alison"] s.date = %q{2012-06-07} s.description = %q{Rack::MobileDetect detects mobile devices and adds an X_MOBILE_DEVICE header to the request if a mobile device is detected. Specific devices can be targeted with custom Regexps and redirect support is available.} s.email = %q{accounts@majortom.fastmail.us} s.extra_rdoc_files = [ "LICENSE", "README.md", "TODO" ] s.files = [ ".document", "CHANGELOG.md", "LICENSE", "README.md", "Rakefile", "TODO", "VERSION.yml", "lib/rack/mobile-detect.rb", "rack-mobile-detect.gemspec", "test/helper.rb", "test/test_rack-mobile-detect.rb", "util/echo_env.rb" ] s.homepage = %q{http://github.com/talison/rack-mobile-detect} s.require_paths = ["lib"] s.rubygems_version = %q{1.3.6} s.summary = %q{Rack middleware for ruby webapps to detect mobile devices.} s.test_files = [ "test/helper.rb", "test/test_rack-mobile-detect.rb" ] if s.respond_to? :specification_version then current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION s.specification_version = 3 if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then s.add_development_dependency(%q, [">= 0"]) s.add_runtime_dependency(%q, [">= 0"]) else s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) end else s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) end end ruby-rack-mobile-detect-0.4.0/test/000077500000000000000000000000001212160030100170665ustar00rootroot00000000000000ruby-rack-mobile-detect-0.4.0/test/helper.rb000066400000000000000000000003451212160030100206740ustar00rootroot00000000000000require 'rubygems' require 'test/unit' require 'shoulda' $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(File.dirname(__FILE__)) require 'rack/mobile-detect' class Test::Unit::TestCase end ruby-rack-mobile-detect-0.4.0/test/test_rack-mobile-detect.rb000066400000000000000000000230321212160030100241050ustar00rootroot00000000000000require 'helper' class TestRackMobileDetect < Test::Unit::TestCase context "An app with mobile-device defaults" do setup do @app = test_app @rack = Rack::MobileDetect.new(@app) end should "not detect a non-mobile device" do env = test_env @rack.call(env) assert !env.key?(x_mobile) end should "detect all default targeted devices" do env = test_env({ 'HTTP_USER_AGENT' => ipod }) @rack.call(env) assert_equal 'iPod', env[x_mobile] env = test_env({ 'HTTP_USER_AGENT' => iphone }) @rack.call(env) assert_equal 'iPhone', env[x_mobile] env = test_env({ 'HTTP_USER_AGENT' => android }) @rack.call(env) assert_equal 'Android', env[x_mobile] env = test_env({ 'HTTP_USER_AGENT' => ipad }) @rack.call(env) assert_equal 'iPad', env[x_mobile] end should "detect UAProf device" do env = test_env({ 'HTTP_X_WAP_PROFILE' => '"http://www.blackberry.net/go/mobile/profiles/uaprof/9000_80211g/4.6.0.rdf"' }) @rack.call(env) assert_equal "true", env[x_mobile] env = test_env({ 'HTTP_PROFILE' => 'http://www.blackberry.net/go/mobile/profiles/uaprof/9000_80211g/4.6.0.rdf' }) @rack.call(env) assert_equal "true", env[x_mobile] # See http://www.developershome.com/wap/detection/detection.asp?page=uaprof env = test_env({ 'HTTP_80_PROFILE' => 'http://wap.sonyericsson.com/UAprof/T68R502.xml' }) @rack.call(env) assert_equal "true", env[x_mobile] end should "not detect spurious profile header match" do env = test_env({ 'HTTP_X_PROFILE_FOO' => 'bar' }) @rack.call(env) assert !env.key?(x_mobile) end should "detect wap in Accept header" do env = test_env({ 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/vnd.wap.xhtml+xml,*/*;q=0.5' }) @rack.call(env) assert_equal "true", env[x_mobile] env = test_env({ 'HTTP_ACCEPT' => 'application/vnd.wap.wmlscriptc;q=0.7,text/vnd.wap.wml;q=0.7,*/*;q=0.5' }) @rack.call(env) assert_equal "true", env[x_mobile] end should "detect additional devices in catchall" do env = test_env({ 'HTTP_USER_AGENT' => blackberry }) @rack.call(env) assert_equal "true", env[x_mobile] env = test_env({ 'HTTP_USER_AGENT' => samsung }) @rack.call(env) assert_equal "true", env[x_mobile] env = test_env({ 'HTTP_USER_AGENT' => webos }) @rack.call(env) assert_equal "true", env[x_mobile] env = test_env({ 'HTTP_USER_AGENT' => 'opera' }) @rack.call(env) assert !env.key?(x_mobile) end end context "An app with a custom targeted option" do setup do @app = test_app # Target Samsung SCH and Blackberries. Note case-sensitivity. @rack = Rack::MobileDetect.new(@app, :targeted => /SCH-\w*$|[Bb]lack[Bb]erry\w*/) end should "capture the targeted token" do env = test_env({ 'HTTP_USER_AGENT' => samsung }) @rack.call(env) assert_equal 'SCH-U960', env[x_mobile] env = test_env({ 'HTTP_USER_AGENT' => "Samsung SCH-I760" }) @rack.call(env) assert_equal 'SCH-I760', env[x_mobile] env = test_env({ 'HTTP_USER_AGENT' => blackberry }) @rack.call(env) assert_equal 'BlackBerry9000', env[x_mobile] # An iPhone will be detected, but the token won't be captured env = test_env({ 'HTTP_USER_AGENT' => iphone }) @rack.call(env) assert_equal "true", env[x_mobile] end end context "An app with a custom catchall option" do setup do @app = test_app # Custom catchall regex @rack = Rack::MobileDetect.new(@app, :catchall => /mysupermobiledevice/i) end should "catch only the specified devices" do env = test_env({ 'HTTP_USER_AGENT' => "MySuperMobileDevice v1.0" }) @rack.call(env) assert_equal "true", env[x_mobile] env = test_env({ 'HTTP_USER_AGENT' => samsung }) @rack.call(env) assert !env.key?(x_mobile) end end context "An app with a custom redirect" do setup do @app = test_app # Custom redirect @rack = Rack::MobileDetect.new(@app, :redirect_to => 'http://m.example.com/') end should "redirect to mobile website" do env = test_env({ 'HTTP_USER_AGENT' => iphone }) status, headers, body = @rack.call(env) assert_equal 'iPhone', env[x_mobile] assert_equal(301, status) assert_equal({'Location' => "http://m.example.com/"}, headers) end end context "An app with a custom redirect map" do setup do @app = test_app redirects = { "myphone" => "http://m.example.com/myphone", "yourphone" => "http://m.example.com/yourphone" } # Target fake devices @rack = Rack::MobileDetect.new(@app, :targeted => /myphone|yourphone/, :redirect_map => redirects) end should "redirect to the custom url of the targeted devices" do env = test_env({ 'HTTP_USER_AGENT' => 'myphone rocks' }) status, headers, body = @rack.call(env) assert_equal 'myphone', env[x_mobile] assert_equal(301, status) assert_equal({'Location' => "http://m.example.com/myphone"}, headers) env = test_env({ 'HTTP_USER_AGENT' => 'yourphone sucks' }) status, headers, body = @rack.call(env) assert_equal 'yourphone', env[x_mobile] assert_equal(301, status) assert_equal({'Location' => "http://m.example.com/yourphone"}, headers) end should "not redirect a non-targeted device" do env = test_env({ 'HTTP_USER_AGENT' => 'some wap phone' }) status, headers, body = @rack.call(env) assert_equal 'true', env[x_mobile] assert_not_equal(301, status) end end context "An app with a custom redirect map and redirect_to option" do setup do @app = test_app redirects = { "myphone" => "http://m.example.com/myphone", "yourphone" => "http://m.example.com/yourphone" } # Target fake devices @rack = Rack::MobileDetect.new(@app, :targeted => /myphone|yourphone/, :redirect_map => redirects, :redirect_to => 'http://m.example.com/genericdevice') end should "use the redirect value in the redirect map when targeted" do env = test_env({ 'HTTP_USER_AGENT' => 'myphone rocks' }) status, headers, body = @rack.call(env) assert_equal 'myphone', env[x_mobile] assert_equal(301, status) assert_equal({'Location' => "http://m.example.com/myphone"}, headers) end should "use redirect_to to redirect a device not in the map" do env = test_env({ 'HTTP_USER_AGENT' => 'some wap phone' }) status, headers, body = @rack.call(env) assert_equal 'true', env[x_mobile] assert_equal(301, status) assert_equal({'Location' => "http://m.example.com/genericdevice"}, headers) end end # Expected x_header def x_mobile Rack::MobileDetect::X_HEADER end # User agents for testing def 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/7B334b Safari/531.21.10' end def ipod 'Mozilla/5.0 (iPod; U; CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20' end def iphone 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7C144 Safari/528.16' end def android 'Mozilla/5.0 (Linux; U; Android 2.0; ld-us; sdk Build/ECLAIR) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17' end def blackberry 'BlackBerry9000/4.6.0.167 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/102' end def samsung 'Mozilla/4.0 (compatible; MSIE 6.0; BREW 3.1.5; en )/800x480 Samsung SCH-U960' end def webos 'Mozilla/5.0 (webOS/1.4.0; U; en-US) AppleWebKit/532.2 (KHTML, like Gecko) Version/1.0 Safari/532.2 Pre/1.1' end # Our test web app. Doesn't do anything. def test_app() Class.new { def call(app); true; end }.new end # Test environment variables def test_env(overwrite = {}) { 'GATEWAY_INTERFACE'=> 'CGI/1.2', 'HTTP_ACCEPT'=> 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'HTTP_ACCEPT_CHARSET'=> 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', 'HTTP_ACCEPT_ENCODING'=> 'gzip,deflate', 'HTTP_ACCEPT_LANGUAGE'=> 'en-us,en;q=0.5', 'HTTP_CONNECTION'=> 'keep-alive', 'HTTP_HOST'=> 'localhost:4567', 'HTTP_KEEP_ALIVE'=> 300, 'HTTP_USER_AGENT'=> 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.3) Gecko/20090920 Firefox/3.5.3 (Swiftfox)', 'HTTP_VERSION'=> 'HTTP/1.1', 'PATH_INFO'=> '/', 'QUERY_STRING'=> '', 'REMOTE_ADDR'=> '127.0.0.1', 'REQUEST_METHOD'=> 'GET', 'REQUEST_PATH'=> '/', 'REQUEST_URI'=> '/', 'SCRIPT_NAME'=> '', 'SERVER_NAME'=> 'localhost', 'SERVER_PORT'=> '4567', 'SERVER_PROTOCOL'=> 'HTTP/1.1', 'SERVER_SOFTWARE'=> 'Mongrel 1.1.5', 'rack.multiprocess'=> false, 'rack.multithread'=> true, 'rack.request.form_hash'=> '', 'rack.request.form_vars'=> '', 'rack.request.query_hash'=> '', 'rack.request.query_string'=> '', 'rack.run_once'=> false, 'rack.url_scheme'=> 'http', 'rack.version'=> '1: 0' }.merge(overwrite) end end ruby-rack-mobile-detect-0.4.0/util/000077500000000000000000000000001212160030100170645ustar00rootroot00000000000000ruby-rack-mobile-detect-0.4.0/util/echo_env.rb000066400000000000000000000010451212160030100211770ustar00rootroot00000000000000require 'rubygems' require 'sinatra' $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) require 'rack/mobile-detect' use Rack::MobileDetect # Very simple sinatra app that allows debugging of the headers with # Rack::MobileDetect. Also useful for looking at various mobile phone # headers. get '/' do content_type 'text/plain' env_string = env.sort.map{ |v| v.join(': ') }.join("\n") + "\n" # Print to log if debug is passed, i.e.: # http://localhost:4567/?debug puts env_string if params.key?('debug') env_string end