schmobile 0.4.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ Gemfile.lock
2
+ pkg/
3
+ # rcov generated
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ before_install:
2
+ - gem update --system
3
+ rvm:
4
+ - 1.8.7
5
+ - 1.9.3
6
+ - jruby
7
+ - ree
data/Gemfile CHANGED
@@ -1,14 +1,3 @@
1
1
  source "http://rubygems.org"
2
- # Add dependencies required to use your gem here.
3
- # Example:
4
- # gem "activesupport", ">= 2.3.5"
5
2
 
6
- # Add dependencies to develop your gem here.
7
- # Include everything needed to run rake, tests, features, etc.
8
- group :development do
9
- gem "rack", "~> 1.1.0"
10
- gem "mocha", "0.9.9"
11
- gem "shoulda", ">= 0"
12
- gem "bundler", "~> 1.0.0"
13
- gem "jeweler", "~> 1.5.2"
14
- end
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Schmobile [![Build Status](https://secure.travis-ci.org/morten/schmobile.png)](http://travis-ci.org/morten/schmobile)
2
+
3
+ A mobile user agent detection Rack middleware. It provides `Rack::Request#is_mobile?`
4
+
5
+ ## Forcing mobile mode
6
+
7
+ You can force toggle mobile mode by sending +is_mobile#true+ and +is_mobile#false+ as
8
+ parameters in the request URL. This setting will be stored in the session for subsequent requests.
9
+ This entirely overrides the user agent detection.
10
+
11
+ ## User agent detection
12
+
13
+ You can add/remove user agents like so:
14
+
15
+ ```ruby
16
+ Schmobile::UserAgents.add_user_agent_pattern("wibble")
17
+ Schmobile::UserAgents.remove_user_agent_pattern("ipad")
18
+ ```
19
+
20
+ ## Filters
21
+
22
+ The outcome of the +request.is_mobile?+ call is the product of a series of filters getting evaluated
23
+ against the request. You can manipulate the Schmobile::Filters::CHAIN array to alter if a
24
+ request is deemed mobile or not. Add your own filter to bypass the check depending on e.g. location
25
+ or request format.
26
+
27
+ ## Redirecting
28
+
29
+ It can be configured to return the user to an explicit destination:
30
+
31
+ ```ruby
32
+ use Schmobile, :redirect_to => "/mobile"
33
+ ```
34
+
35
+ It supports string interpolation for dynamic destinations:
36
+
37
+ ```ruby
38
+ use Schmobile, :redirect_to => "/mobile/#!/{{path}}"
39
+ ```
40
+
41
+ Finally the middleware provides a request level method to determine if the client is a mobile device
42
+
43
+ ```ruby
44
+ Rack::Request#is_mobile?
45
+ ```
46
+
47
+ ## Rolling out
48
+
49
+ ```ruby
50
+ use Schmobile, :redirect_to => "/mobile", :if => Proc.new { |request| request.host =~ /staging/ }
51
+ ```
52
+
53
+ ## License
54
+
55
+ Released under the Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.html
data/Rakefile CHANGED
@@ -1,46 +1,12 @@
1
- require 'rubygems'
2
- require 'bundler'
3
- begin
4
- Bundler.setup(:default, :development)
5
- rescue Bundler::BundlerError => e
6
- $stderr.puts e.message
7
- $stderr.puts "Run `bundle install` to install missing gems"
8
- exit e.status_code
9
- end
10
- require 'rake'
11
-
12
- require 'jeweler'
13
- Jeweler::Tasks.new do |gem|
14
- # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
- gem.name = "schmobile"
16
- gem.homepage = "http://github.com/morten/schmobile"
17
- gem.license = "MIT"
18
- gem.summary = %Q{A Rack middleware for detecting mobile user agents}
19
- gem.description = %Q{Used to determine if a request is from a mobile client, and possibly redirect it if that's the case}
20
- gem.email = "morten@zendesk.com"
21
- gem.authors = ["Morten Primdahl"]
22
- # Include your dependencies below. Runtime dependencies are required when using your gem,
23
- # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
- # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
- # gem.add_development_dependency 'rspec', '> 1.2.3'
26
- end
27
- Jeweler::RubygemsDotOrgTasks.new
28
-
1
+ require 'bundler/gem_tasks'
29
2
  require 'rake/testtask'
30
- Rake::TestTask.new(:test) do |test|
3
+
4
+ Rake::TestTask.new do |test|
31
5
  test.libs << 'lib' << 'test'
32
6
  test.pattern = 'test/**/test_*.rb'
33
7
  test.verbose = true
34
8
  end
35
9
 
36
- task :default => :test
37
-
38
- require 'rake/rdoctask'
39
- Rake::RDocTask.new do |rdoc|
40
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
-
42
- rdoc.rdoc_dir = 'rdoc'
43
- rdoc.title = "schmobile #{version}"
44
- rdoc.rdoc_files.include('README*')
45
- rdoc.rdoc_files.include('lib/**/*.rb')
10
+ task :default do
11
+ sh "bundle exec rake test"
46
12
  end
@@ -0,0 +1,13 @@
1
+ module Schmobile
2
+ module Filters
3
+ module IsMobileParam
4
+ def self.call(request)
5
+ if request.params.key?(Schmobile::IS_MOBILE)
6
+ request.session[Schmobile::IS_MOBILE] = (request.params[Schmobile::IS_MOBILE] == "true")
7
+ end
8
+
9
+ nil
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module Schmobile
2
+ module Filters
3
+ module MobileSession
4
+ def self.call(request)
5
+ request.session[Schmobile::IS_MOBILE]
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ require 'schmobile/user_agents'
2
+
3
+ module Schmobile
4
+ module Filters
5
+ module MobileUserAgent
6
+ def self.call(request)
7
+ request.session[Schmobile::IS_MOBILE] = Schmobile::UserAgents.is_mobile_agent?(request.user_agent)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,30 @@
1
+ require 'schmobile/filters/mobile_session'
2
+ require 'schmobile/filters/is_mobile_param'
3
+ require 'schmobile/filters/mobile_user_agent'
4
+
5
+ module Schmobile
6
+ # Filters are tests that get run against a request to determine if it's a mobile request or not.
7
+ # A filter can return true, false or nil. The first non-nil value of the filter chain is the
8
+ # one that gets used and this also gets used to mark the session as a "mobile session"
9
+ #
10
+ # You can manipulate the chain to add new conditions that check on e.g. request format.
11
+ module Filters
12
+ CHAIN = [
13
+ Schmobile::Filters::IsMobileParam, # Must come before session check
14
+ Schmobile::Filters::MobileSession,
15
+ Schmobile::Filters::MobileUserAgent # Always returns either true or false
16
+ ]
17
+
18
+ def self.apply(request)
19
+ Schmobile::Filters::CHAIN.each do |filter|
20
+ result = filter.call(request)
21
+ unless result.nil?
22
+ request.session[Schmobile::IS_MOBILE] = result
23
+ break
24
+ end
25
+ end
26
+
27
+ request.session[Schmobile::IS_MOBILE] ||= false
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,59 @@
1
+ module Schmobile
2
+ class Middleware
3
+
4
+ def initialize(app, options = {})
5
+ @app = app
6
+ @options = options
7
+ end
8
+
9
+ def call(env)
10
+ request = Rack::Request.new(env)
11
+
12
+ if request.is_mobile? && redirect?(request)
13
+ [ 301, { "Location" => redirect_location(request) }, [] ]
14
+ else
15
+ @app.call(env)
16
+ end
17
+ end
18
+
19
+ # Returns true if this middleware has been configured with a redirect_to and the requested path
20
+ # is not already below the configured redirect_to
21
+ def redirect?(request)
22
+ redirecting = true
23
+
24
+ if @options.key?(:redirect_if)
25
+ redirecting = @options[:redirect_if].call(request)
26
+ end
27
+
28
+ if @options.key?(:redirect_to)
29
+ redirecting &&= request.path !~ /^#{@options[:redirect_to]}/
30
+ else
31
+ redirecting = false
32
+ end
33
+
34
+ redirecting ? redirect_location(request) : nil
35
+ end
36
+
37
+ def redirect_location(request)
38
+ "#{@options[:redirect_to]}#{redirect_with(request)}"
39
+ end
40
+
41
+ def redirect_with(request)
42
+ build_path(@options[:redirect_with].to_s, request)
43
+ end
44
+
45
+ private
46
+
47
+ def build_path(destination, request)
48
+ final_destination = destination.dup
49
+ destination.scan(/\{\{\w+\}\}/) do |call|
50
+ func = call.scan(/\w+/).first.to_s
51
+ if request.respond_to?(func)
52
+ final_destination.sub!(/\{\{#{func}\}\}/, request.send(func))
53
+ end
54
+ end
55
+ final_destination
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,24 @@
1
+ module Schmobile
2
+ module Request
3
+ def is_mobile?
4
+ if @is_mobile.nil?
5
+ @is_mobile = Schmobile::Filters.apply(self)
6
+ end
7
+
8
+ @is_mobile
9
+ end
10
+
11
+ def toggle_mobile_session!
12
+ session[Schmobile::IS_MOBILE] = !is_mobile?
13
+ @is_mobile = nil
14
+ end
15
+
16
+ def is_device?(identifier)
17
+ user_agent =~ /#{identifier}/i
18
+ end
19
+ end
20
+ end
21
+
22
+ Rack::Request.class_eval do
23
+ include Schmobile::Request
24
+ end
@@ -0,0 +1,58 @@
1
+ module Schmobile
2
+ class UserAgents
3
+
4
+ MOBILE_USER_AGENTS = %w(
5
+ alcatel amoi android astel audiovox blackberry cdm chtml danger docomo ericsson htc_touch
6
+ iphone ipod j2me kddi midp minimo mmp mobi mobile mobileexplorer mot- motorola netfront nokia
7
+ novarra palm pdxgw phone plucker pocket portable portalmmm sagem samsung sgh sie- softbank
8
+ sprint symbian telit ucweb up.b upg1 vodafone webos windows\ ce x240 x320 xiino
9
+ )
10
+
11
+ NON_MOBILE_USER_AGENTS = %w(
12
+ ipad
13
+ )
14
+
15
+ def self.remove_user_agent_pattern(pattern)
16
+ MOBILE_USER_AGENTS.delete(pattern)
17
+ @mobile_agent_matcher = nil
18
+ end
19
+
20
+ def self.add_user_agent_pattern(pattern)
21
+ MOBILE_USER_AGENTS.push(*pattern)
22
+ @mobile_agent_matcher = nil
23
+ end
24
+
25
+ def self.remove_non_mobile_user_agent_pattern(pattern)
26
+ NON_MOBILE_USER_AGENTS.delete(pattern)
27
+ @non_mobile_agent_matcher = nil
28
+ end
29
+
30
+ def self.add_non_mobile_user_agent_pattern(pattern)
31
+ NON_MOBILE_USER_AGENTS.push(*pattern)
32
+ @non_mobile_agent_matcher = nil
33
+ end
34
+
35
+ def self.is_mobile_agent?(user_agent)
36
+ agent = user_agent.to_s.downcase
37
+ mobile = !(agent =~ mobile_agent_matcher).nil?
38
+ mobile = mobile && agent !~ non_mobile_agent_matcher unless NON_MOBILE_USER_AGENTS.empty?
39
+ mobile
40
+ end
41
+
42
+ def self.mobile_agent_matcher
43
+ @mobile_agent_matcher ||= Regexp.new(MOBILE_USER_AGENTS.uniq.compact.map { |v| Regexp.escape(v) }.join("|"))
44
+ end
45
+
46
+ def self.non_mobile_agent_matcher
47
+ @non_mobile_agent_matcher ||= Regexp.new(NON_MOBILE_USER_AGENTS.uniq.compact.map { |v| Regexp.escape(v) }.join("|"))
48
+ end
49
+
50
+ def self.matched_by?(user_agent)
51
+ MOBILE_USER_AGENTS.each do |agent|
52
+ return agent if user_agent =~ /#{agent}/
53
+ end
54
+ nil
55
+ end
56
+
57
+ end
58
+ end
data/lib/schmobile.rb CHANGED
@@ -1 +1,10 @@
1
- require 'rack/schmobile'
1
+ require 'rack'
2
+
3
+ require 'schmobile/middleware'
4
+ require 'schmobile/request_extension'
5
+ require 'schmobile/filters'
6
+
7
+ module Schmobile
8
+ VERSION = "0.5.0"
9
+ IS_MOBILE = "is_mobile"
10
+ end
data/schmobile.gemspec CHANGED
@@ -1,80 +1,24 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
1
+ Gem::Specification.new "Schmobile", "1.0.0" do |s|
2
+ s.name = 'schmobile'
3
+ s.date = '2012-06-14'
4
+ s.files = `git ls-files`.split("\n")
5
+ s.license = "Apache License Version 2.0"
5
6
 
6
- Gem::Specification.new do |s|
7
- s.name = %q{schmobile}
8
- s.version = "0.4.1"
7
+ s.summary = "A mobile user agent detection Rack middleware."
8
+ s.description = "A mobile user agent detection Rack middleware. See the README."
9
9
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Morten Primdahl"]
12
- s.date = %q{2011-11-03}
13
- s.description = %q{Used to determine if a request is from a mobile client, and possibly redirect it if that's the case}
14
- s.email = %q{morten@zendesk.com}
15
- s.extra_rdoc_files = [
16
- "LICENSE.txt",
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- ".document",
21
- "Gemfile",
22
- "Gemfile.lock",
23
- "LICENSE.txt",
24
- "README.rdoc",
25
- "Rakefile",
26
- "VERSION",
27
- "lib/rack/schmobile.rb",
28
- "lib/rack/schmobile/filters.rb",
29
- "lib/rack/schmobile/filters/is_mobile_param.rb",
30
- "lib/rack/schmobile/filters/mobile_session.rb",
31
- "lib/rack/schmobile/filters/mobile_user_agent.rb",
32
- "lib/rack/schmobile/middleware.rb",
33
- "lib/rack/schmobile/request_extension.rb",
34
- "lib/rack/schmobile/user_agents.rb",
35
- "lib/schmobile.rb",
36
- "schmobile.gemspec",
37
- "test/helper.rb",
38
- "test/test_filters.rb",
39
- "test/test_middleware.rb",
40
- "test/test_rack_request.rb",
41
- "test/test_user_agents.rb"
42
- ]
43
- s.homepage = %q{http://github.com/morten/schmobile}
44
- s.licenses = ["MIT"]
45
- s.require_paths = ["lib"]
46
- s.rubygems_version = %q{1.5.3}
47
- s.summary = %q{A Rack middleware for detecting mobile user agents}
48
- s.test_files = [
49
- "test/helper.rb",
50
- "test/test_filters.rb",
51
- "test/test_middleware.rb",
52
- "test/test_rack_request.rb",
53
- "test/test_user_agents.rb"
54
- ]
10
+ s.authors = ["Morten Primdahl"]
11
+ s.email = 'primdahl@me.com'
12
+ s.homepage = 'http://github.com/zendesk/schmobile'
55
13
 
56
- if s.respond_to? :specification_version then
57
- s.specification_version = 3
14
+ s.require_paths = %w[lib]
58
15
 
59
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
60
- s.add_development_dependency(%q<rack>, ["~> 1.1.0"])
61
- s.add_development_dependency(%q<mocha>, ["= 0.9.9"])
62
- s.add_development_dependency(%q<shoulda>, [">= 0"])
63
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
64
- s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
65
- else
66
- s.add_dependency(%q<rack>, ["~> 1.1.0"])
67
- s.add_dependency(%q<mocha>, ["= 0.9.9"])
68
- s.add_dependency(%q<shoulda>, [">= 0"])
69
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
70
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
71
- end
72
- else
73
- s.add_dependency(%q<rack>, ["~> 1.1.0"])
74
- s.add_dependency(%q<mocha>, ["= 0.9.9"])
75
- s.add_dependency(%q<shoulda>, [">= 0"])
76
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
77
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
78
- end
79
- end
16
+ s.add_runtime_dependency("rack")
17
+
18
+ s.add_development_dependency('rake')
19
+ s.add_development_dependency('bundler')
20
+ s.add_development_dependency('shoulda')
21
+ s.add_development_dependency('mocha')
80
22
 
23
+ s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
24
+ end
data/test/helper.rb CHANGED
@@ -6,9 +6,10 @@ require 'mocha'
6
6
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
7
  $LOAD_PATH.unshift(File.dirname(__FILE__))
8
8
 
9
- require 'rack/schmobile'
9
+ require 'schmobile'
10
10
 
11
11
  class Test::Unit::TestCase
12
+
12
13
  def ipod
13
14
  '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'
14
15
  end
data/test/test_filters.rb CHANGED
@@ -2,7 +2,7 @@ require 'helper'
2
2
 
3
3
  class TestFukters < Test::Unit::TestCase
4
4
 
5
- context "Rack::Schmobile::Filters" do
5
+ context "Schmobile::Filters" do
6
6
  context "#apply" do
7
7
  setup do
8
8
  @request = stub(:user_agent => msie8)
@@ -13,48 +13,48 @@ class TestFukters < Test::Unit::TestCase
13
13
 
14
14
  context "on a pristine request" do
15
15
  should "return true if the session is already mobile" do
16
- Rack::Schmobile::UserAgents.expects(:is_mobile_agent?).never
17
- @session[Rack::Schmobile::IS_MOBILE] = true
18
- assert Rack::Schmobile::Filters.apply(@request)
16
+ Schmobile::UserAgents.expects(:is_mobile_agent?).never
17
+ @session[Schmobile::IS_MOBILE] = true
18
+ assert Schmobile::Filters.apply(@request)
19
19
  end
20
20
 
21
21
  should "return true if there's an is_mobile parameter with value true" do
22
- Rack::Schmobile::UserAgents.expects(:is_mobile_agent?).never
22
+ Schmobile::UserAgents.expects(:is_mobile_agent?).never
23
23
  @request.stubs(:params).returns({ "is_mobile" => "true" })
24
- assert Rack::Schmobile::Filters.apply(@request)
24
+ assert Schmobile::Filters.apply(@request)
25
25
  end
26
26
 
27
27
  should "return false if there's an is_mobile parameter with value false" do
28
- Rack::Schmobile::UserAgents.expects(:is_mobile_agent?).never
28
+ Schmobile::UserAgents.expects(:is_mobile_agent?).never
29
29
  @request.stubs(:params).returns({ "is_mobile" => "false" })
30
- assert !Rack::Schmobile::Filters.apply(@request)
30
+ assert !Schmobile::Filters.apply(@request)
31
31
  end
32
32
 
33
33
  should "defer to user agent check if there's no session or parameter" do
34
- Rack::Schmobile::UserAgents.expects(:is_mobile_agent?).once
35
- assert !Rack::Schmobile::Filters.apply(@request)
34
+ Schmobile::UserAgents.expects(:is_mobile_agent?).once
35
+ assert !Schmobile::Filters.apply(@request)
36
36
  end
37
37
  end
38
38
 
39
39
  context "on a request with mobile session set to false" do
40
40
  should "allow to change the session value with a parameter" do
41
- Rack::Schmobile::UserAgents.expects(:is_mobile_agent?).never
42
- @request.session[Rack::Schmobile::IS_MOBILE] = false
43
- assert !Rack::Schmobile::Filters.apply(@request)
41
+ Schmobile::UserAgents.expects(:is_mobile_agent?).never
42
+ @request.session[Schmobile::IS_MOBILE] = false
43
+ assert !Schmobile::Filters.apply(@request)
44
44
  @request.stubs(:params).returns({ "is_mobile" => "true" })
45
- assert Rack::Schmobile::Filters.apply(@request)
46
- assert @request.session[Rack::Schmobile::IS_MOBILE]
45
+ assert Schmobile::Filters.apply(@request)
46
+ assert @request.session[Schmobile::IS_MOBILE]
47
47
  end
48
48
  end
49
49
 
50
50
  context "on a request with mobile session set to true" do
51
51
  should "allow to change the session value with a parameter" do
52
- Rack::Schmobile::UserAgents.expects(:is_mobile_agent?).never
53
- @request.session[Rack::Schmobile::IS_MOBILE] = true
54
- assert Rack::Schmobile::Filters.apply(@request)
52
+ Schmobile::UserAgents.expects(:is_mobile_agent?).never
53
+ @request.session[Schmobile::IS_MOBILE] = true
54
+ assert Schmobile::Filters.apply(@request)
55
55
  @request.stubs(:params).returns({ "is_mobile" => "false" })
56
- assert !Rack::Schmobile::Filters.apply(@request)
57
- assert !@request.session[Rack::Schmobile::IS_MOBILE]
56
+ assert !Schmobile::Filters.apply(@request)
57
+ assert !@request.session[Schmobile::IS_MOBILE]
58
58
  end
59
59
  end
60
60
  end
@@ -2,14 +2,14 @@ require 'helper'
2
2
 
3
3
  class TestMiddleware < Test::Unit::TestCase
4
4
 
5
- context "Rack::Schmobile::Middleware" do
5
+ context "Schmobile::Middleware" do
6
6
  setup do
7
7
  @app = Class.new { def call(app); true; end }.new
8
- @rack = Rack::Schmobile::Middleware.new(@app)
8
+ @rack = Schmobile::Middleware.new(@app)
9
9
  end
10
10
 
11
11
  should "return an HTTP permanent redirect when given a redirect path and used by a mobile client" do
12
- @rack = Rack::Schmobile::Middleware.new(@app, :redirect_to => "/hello")
12
+ @rack = Schmobile::Middleware.new(@app, :redirect_to => "/hello")
13
13
  Rack::Request.any_instance.expects(:is_mobile?).returns(true)
14
14
 
15
15
  assert_equal [301, { "Location"=>"/hello" }, []], @rack.call(environment)
@@ -30,7 +30,7 @@ class TestMiddleware < Test::Unit::TestCase
30
30
 
31
31
  context "with redirect_to" do
32
32
  setup do
33
- @rack = Rack::Schmobile::Middleware.new(@app, :redirect_to => "/wonderland")
33
+ @rack = Schmobile::Middleware.new(@app, :redirect_to => "/wonderland")
34
34
  end
35
35
 
36
36
  context "#redirect?" do
@@ -48,19 +48,19 @@ class TestMiddleware < Test::Unit::TestCase
48
48
  end
49
49
 
50
50
  should "return false when :if resolves to false" do
51
- @rack = Rack::Schmobile::Middleware.new(@app, :redirect_to => "/wonderland", :redirect_if => Proc.new { |request| false })
51
+ @rack = Schmobile::Middleware.new(@app, :redirect_to => "/wonderland", :redirect_if => Proc.new { |request| false })
52
52
  assert !@rack.redirect?(request("PATH_INFO" => "/somewhere"))
53
53
  end
54
54
  end
55
55
 
56
56
  context "#redirect" do
57
- should "interpolate the argument string" do
58
- @rack = Rack::Schmobile::Middleware.new(@app, :redirect_to => "/wonderland", :redirect_with => "/{{path}}")
57
+ should "interpolate the argument string xxxx" do
58
+ @rack = Schmobile::Middleware.new(@app, :redirect_to => "/wonderland", :redirect_with => "/{{path}}")
59
59
  assert_equal "/wonderland/wiffle", @rack.redirect_location(request("PATH_INFO" => "wiffle"))
60
60
  end
61
61
 
62
62
  should "interpolate a multipart argument string" do
63
- @rack = Rack::Schmobile::Middleware.new(@app, :redirect_to => "/wonderland/", :redirect_with => "{{path}}/lemurs/{{path}}")
63
+ @rack = Schmobile::Middleware.new(@app, :redirect_to => "/wonderland/", :redirect_with => "{{path}}/lemurs/{{path}}")
64
64
  assert_equal "/wonderland/wiffle/lemurs/wiffle", @rack.redirect_location(request("PATH_INFO" => "wiffle"))
65
65
  end
66
66
  end
@@ -16,13 +16,13 @@ class TestRackRequest < Test::Unit::TestCase
16
16
 
17
17
  context "#is_mobile?" do
18
18
  should "only call the filter chain once" do
19
- Rack::Schmobile::Filters.expects(:apply).once.returns(false)
19
+ Schmobile::Filters.expects(:apply).once.returns(false)
20
20
  one_request = request
21
21
  3.times { one_request.is_mobile? }
22
22
  end
23
23
 
24
24
  should "re-call the filter chain once reset" do
25
- Rack::Schmobile::Filters.expects(:apply).twice.returns(false)
25
+ Schmobile::Filters.expects(:apply).twice.returns(false)
26
26
  one_request = request
27
27
  3.times { one_request.is_mobile? }
28
28
  one_request.toggle_mobile_session!
@@ -56,24 +56,24 @@ class TestRackRequest < Test::Unit::TestCase
56
56
  end
57
57
 
58
58
  should "return false when forced in the session" do
59
- Rack::Request.any_instance.stubs(:session).returns({ Rack::Schmobile::IS_MOBILE => false })
59
+ Rack::Request.any_instance.stubs(:session).returns({ Schmobile::IS_MOBILE => false })
60
60
  assert !request("HTTP_USER_AGENT" => iphone).is_mobile?
61
61
  end
62
62
 
63
63
  should "return true when forced in the session" do
64
- Rack::Request.any_instance.stubs(:session).returns({ Rack::Schmobile::IS_MOBILE => true })
64
+ Rack::Request.any_instance.stubs(:session).returns({ Schmobile::IS_MOBILE => true })
65
65
  assert request.is_mobile?
66
66
  end
67
67
  end
68
68
 
69
69
  context "with params" do
70
70
  should "return false when forced via a request parameter" do
71
- Rack::Request.any_instance.stubs(:params).returns({ Rack::Schmobile::IS_MOBILE => "false" })
71
+ Rack::Request.any_instance.stubs(:params).returns({ Schmobile::IS_MOBILE => "false" })
72
72
  assert !request("HTTP_USER_AGENT" => iphone).is_mobile?
73
73
  end
74
74
 
75
75
  should "return true when forced via a request parameter" do
76
- Rack::Request.any_instance.stubs(:params).returns({ Rack::Schmobile::IS_MOBILE => "true" })
76
+ Rack::Request.any_instance.stubs(:params).returns({ Schmobile::IS_MOBILE => "true" })
77
77
  assert request.is_mobile?
78
78
  end
79
79
  end