Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Prepare plugin for Dancer 2 #1

Merged
merged 1 commit into from

2 participants

@yanick
Owner

No description provided.

@dams
Owner

Approved

@yanick yanick merged commit 39e9fb5 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 21, 2012
  1. @yanick

    Prepare plugin for Dancer 2

    yanick authored
This page is out of date. Refresh to see the latest.
View
7 Changes
@@ -1,8 +1,11 @@
Revision history for Dancer-Plugin-MobileDevice
+{{$NEXT}}
+ * Make the plugin Dancer 2-ready.
+
0.03 2011-04-10
- [ Alberto Simões ]
- * Fix access to setting on a test
+ [ Alberto Simões ]
+ * Fix access to setting on a test
* Add support to iPad/iPod
0.02 2010-12-13, Alexis Sukrieh
View
10 lib/Dancer/Plugin/MobileDevice.pm
@@ -13,10 +13,10 @@ register 'is_mobile_device' => sub {
? 1 : 0;
};
-
-before sub {
+hook before => sub {
# If we don't have a mobile layout declared, do nothing.
my $settings = plugin_setting || {};
+
if (my $mobile_layout = $settings->{mobile_layout}) {
# OK, remember the original layout setting (so we can restore it
# after the request), and override it with the specified mobile layout.
@@ -27,19 +27,19 @@ before sub {
}
};
-after sub {
+hook after => sub {
my $settings = plugin_setting || {};
if ( $settings->{mobile_layout} && is_mobile_device() ) {
setting layout => delete vars->{orig_layout};
}
};
-before_template sub {
+hook before_template => sub {
my $tokens = shift;
$tokens->{'is_mobile_device'} = is_mobile_device();
};
-register_plugin;
+register_plugin for_versions => [ 1, 2 ];
1;
__END__
View
18 t/01-is-mobile-device.t
@@ -19,10 +19,22 @@ plan tests => scalar(@mobile_devices) + 1;
use Dancer::Test;
for my $md (@mobile_devices) {
- $ENV{HTTP_USER_AGENT} = $md;
- response_content_is [GET => '/'], 1, "agent $md is a mobile device";
+ $ENV{HTTP_USER_AGENT} = $md; # for Dancer 1.x
+ my $resp = dancer_response GET => '/',
+ undef,
+ { HTTP_USER_AGENT => $md }; # for Dancer 2.x
+
+ my $content = $resp->{content};
+
+ is $content => 1, "agent $md is a mobile device";
}
$ENV{HTTP_USER_AGENT} = 'Mozilla';
-response_content_is [GET => '/'], 0, "Mozilla is not a mobile device";
+ my $resp = dancer_response GET => '/',
+ undef,
+ { HTTP_USER_AGENT => 'Mozilla' }; # for Dancer 2.x
+
+my $content = $resp->{content};
+
+is $content => 0, "Mozilla is not a mobile device";
View
8 t/02-tokens.t
@@ -20,12 +20,16 @@ plan tests => 2;
use Dancer::Test;
$ENV{HTTP_USER_AGENT} = 'Android';
-response_content_is [GET => '/'],
+is dancer_response( GET => '/', undef, {
+ HTTP_USER_AGENT => 'Android'
+ })->{content},
"is_mobile_device: 1\n",
"token is_mobile_device is present and valid for Android";
$ENV{HTTP_USER_AGENT} = 'Mozilla';
-response_content_is [GET => '/'],
+is dancer_response( GET => '/', undef, {
+ HTTP_USER_AGENT => 'Mozilla'
+ })->{content},
"is_mobile_device: 0\n",
"token is_mobile_device is present and valid for Mozilla";
View
49 t/03-dynamic-layout.t
@@ -20,51 +20,56 @@ plan tests => 7;
use Dancer::Test;
-# expose a bug
-set layout => 'main';
+sub resp_for_agent($$$) {
+ my( $agent, $result, $comment ) = @_;
-$ENV{HTTP_USER_AGENT} = 'Mozilla';
-response_content_is [GET => '/'],
- "main\nis_mobile_device: 0\n\n",
- "main layout for non-mobile agents";
+ # for Dancer 1.x
+ $ENV{HTTP_USER_AGENT} = $agent;
-$ENV{HTTP_USER_AGENT} = 'Opera';
-response_content_is [GET => '/'],
- "main\nis_mobile_device: 0\n\n",
- "main layout for non-mobile agents";
+ # for Dancer 2.x
+ is dancer_response( GET => '/', undef,
+ { HTTP_USER_AGENT => $agent } )->{content} => $result, $comment;
+}
+# expose a bug
+set layout => 'main';
+
+resp_for_agent $_, "main\nis_mobile_device: 0\n\n",
+ "main layout for non-mobile agent $_" for qw/ Mozilla Opera /;
# no default layout
set layout => undef;
-$ENV{HTTP_USER_AGENT} = 'Android';
-response_content_is [GET => '/'],
- "is_mobile_device: 1\n",
+resp_for_agent 'Android'
+ => "is_mobile_device: 1\n",
"No layout used unless asked to";
# this is a bit dirty
-my $settings = Dancer::Config::settings();
-$settings->{plugins}{MobileDevice}{mobile_layout} = 'mobile';
+if ( $Dancer::VERSION < 2 ) {
+ my $settings = Dancer::Config::settings();
+ $settings->{plugins}{MobileDevice}{mobile_layout} = 'mobile';
+}
+else {
+ config->{plugins}{MobileDevice}{mobile_layout} = 'mobile';
+}
-response_content_is [GET => '/'],
+resp_for_agent 'Android' =>
"mobile\nis_mobile_device: 1\n\n",
"mobile layout is set for mobile agents when desired";
-$ENV{HTTP_USER_AGENT} = 'Mozilla';
-response_content_is [GET => '/'],
+resp_for_agent 'Mozilla',
"is_mobile_device: 0\n",
"no layout for non-mobile agents";
set layout => 'main';
-$ENV{HTTP_USER_AGENT} = 'Android';
-response_content_is [GET => '/'],
+resp_for_agent 'Android' =>
"mobile\nis_mobile_device: 1\n\n",
"mobile layout is set for mobile agents still";
-$ENV{HTTP_USER_AGENT} = 'Mozilla';
-response_content_is [GET => '/'],
+resp_for_agent 'Mozilla' =>
"main\nis_mobile_device: 0\n\n",
"main layout for non-mobile agents";
+
Something went wrong with that request. Please try again.