Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
mobileesp strategy accepts symbol for predefined detection procs
Browse files Browse the repository at this point in the history
  • Loading branch information
jistr committed Apr 18, 2012
1 parent 48cddfe commit 5c9c79d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
8 changes: 6 additions & 2 deletions lib/mobvious/strategies/mobileesp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ class MobileESP
# @param detection_procedure
# A lambda function that gets one parameter (`MobileESPConverted::UserAgentInfo` instance)
# and returns device type symbol or nil.
def initialize(detection_procedure = DEVICE_TYPES_MOBILE_DESKTOP)
@detection_procedure = detection_procedure
def initialize(detection_procedure = :mobile_desktop)
if detection_procedure.is_a? Symbol
@detection_procedure = eval("DEVICE_TYPES_#{detection_procedure.to_s.upcase}")
else
@detection_procedure = detection_procedure
end
end

# Gets device type using user-agent sniffing. Can return nil if the used
Expand Down
37 changes: 24 additions & 13 deletions spec/mobvious/strategies/mobileesp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
module Mobvious::Strategies
class MobileESPSpec < MiniTest::Spec
describe MobileESP do
describe "using mobile_desktop strategy" do
before do
@request = mock 'request'
@env = mock 'env'

@request.stubs(:env).returns(@env)
@env.stubs('[]').with('HTTP_ACCEPT').returns('text/html')
end

describe "using default (mobile_desktop) strategy" do
before do
@strategy = Mobvious::Strategies::MobileESP.new
@request = mock 'request'
@env = mock 'env'

@request.stubs(:env).returns(@env)
@env.stubs('[]').with('HTTP_ACCEPT').returns('text/html')
end

it "categorizes iPhone as :mobile" do
Expand All @@ -31,13 +34,7 @@ class MobileESPSpec < MiniTest::Spec

describe "using mobile_tablet_desktop strategy" do
before do
@strategy = Mobvious::Strategies::MobileESP.new(
Mobvious::Strategies::MobileESP::DEVICE_TYPES_MOBILE_TABLET_DESKTOP)
@request = mock 'request'
@env = mock 'env'

@request.stubs(:env).returns(@env)
@env.stubs('[]').with('HTTP_ACCEPT').returns('text/html')
@strategy = Mobvious::Strategies::MobileESP.new(:mobile_tablet_desktop)
end

it "categorizes iPhone as :mobile" do
Expand All @@ -55,6 +52,20 @@ class MobileESPSpec < MiniTest::Spec
@strategy.get_device_type(@request).must_equal :desktop
end
end

describe "custom strategy" do
before do
procedure = lambda {|mobileesp|
return :test
}
@strategy = Mobvious::Strategies::MobileESP.new(procedure)
end

it "categorizes anything as :test" do
@request.stubs(:user_agent).returns("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.46 Safari/535.11")
@strategy.get_device_type(@request).must_equal :test
end
end
end
end
end
Expand Down

0 comments on commit 5c9c79d

Please sign in to comment.