blib/lib/Plack/Middleware/WURFL/ScientiaMobile.pm

filter: Statementstmt 78.0% 39 / 50 Branchbran 40.0% 4 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 12 / 14 Podpod 75.0% 3 / 4 totaltotal 74.3% 58 / 78 CC 7 SCAR 9.7
CC
7
Cov
72%
__ANON__
6.0
get_from_env
6.0
call
3.7
CRAP 2.6
↑ Summary
1 package Plack::Middleware::WURFL::ScientiaMobile;
2  
3 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Net::WURFL::ScientiaMobile;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use URI::Escape qw(uri_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 extends 'Plack::Middleware';
11  
12 3 our $VERSION = '0.01';
13 sub PSGI_KEY () { q{plack.middleware.wurfl.scientiamobile} }
14 sub CLIENT_CLASS () { q{Net::WURFL::ScientiaMobile} }
15  
16 has 'config' => (
17 is => 'ro',
18 required => 1,
19 3 ▶ isa => sub { die "config must be a hashref!" unless ref $_[0] eq 'HASH' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 3 3
20 3 );
21  
22 3 has 'client' => (
23 is => 'rw',
24 );
25  
26 3 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
27 3 my $self = shift;
28 3 $self->client(CLIENT_CLASS->new($self->config));
29 }
30  
31 1 ▶sub call {
Subroutine call: called CC=3 SCAR=13.1
Pod documented
32 1 my $self = shift;
33 1 my ($env) = @_;
34
35 # if we're using a cookie-based cache provider, pass env to it
36 1 my $cookie_cache = $self->client->cache->isa('Net::WURFL::ScientiaMobile::Cache::Cookie');
37 1 ▶ $self->client->cache->env($env) if $cookie_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $cookie_cache 0 1 1
38
39 # query the ScientiaMobile webservice
40 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=1.2
41 1 $self->client->detectDevice($env);
42 0 $env->{+PSGI_KEY} = $self->client;
43 1 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
44 1 $env->{+PSGI_KEY} = $_;
45 1 };
46
47 1 my $res = $self->app->($env);
48 1 ▶ if ($cookie_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cookie_cache) { } 0 1 1
49 0 ▶ $self->response_cb($res, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
50 0 my $res = shift;
51 0 foreach my $cookie_name (%{ $self->client->cache->cookies }) {
52 0 push @{$res->[1]}, 'Set-Cookie' => sprintf '%s=%s',
53 uri_escape($cookie_name),
54 0 uri_escape($self->client->cache->cookies->{$cookie_name});
55 }
56 0 });
57 } else {
58 1 return $res;
59 }
60 }
61  
62 0 ▶sub get_from_env {
Subroutine get_from_env: not called CC=2 SCAR=17.9
Pod documented
63 0 my $self_or_class = shift;
64 0 my ($env) = @_;
65
66 0 my $obj = $env->{+PSGI_KEY};
67 0 ▶ return (ref $obj eq CLIENT_CLASS) ? $obj : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $obj eq 'Net::WURFL::ScientiaMobile' ? : 0 0 0
68 }
69  
70 1 ▶sub get_error_from_env {
Subroutine get_error_from_env: called CC=2 SCAR=7.0
Pod documented
71 1 my $self_or_class = shift;
72 1 my ($env) = @_;
73
74 1 my $obj = $env->{+PSGI_KEY};
75 1 ▶ return (ref $obj eq CLIENT_CLASS) ? undef : $obj;
Branch: 1 branch
BranchTrueFalseTotal
ref $obj eq 'Net::WURFL::ScientiaMobile' ? : 0 1 1
76 }
77  
78 =head1 NAME
79  
80 Plack::Middleware::WURFL::ScientiaMobile - Query the ScientiaMobile webservice in middleware
81  
82 =head1 SYNOPSIS
83  
84 use Plack::Builder;
85 builder {
86 enable 'WURFL::ScientiaMobile', config => {
87 api_key => '...',
88 };
89 $app;
90 };
91  
92 =head1 DESCRIPTION
93  
94 This middleware is intended to act as a bridge between the WURFL ScientiaMobile webservice
95 and PSGI-based web applications. It does two things: it processes each incoming HTTP request
96 through the C<detectDevice()> method of L<Net::WURFL::ScientiaMobile> and it places the
97 pre-populated ScientiaMobile object inside the C<$env> structure that is passed to your web
98 application.
99 You can easily access it from your web framework of choice and apply your device-specific logic.
100  
101 If you configure the ScientiaMobile object with a C<Cache> cache provider, the middleware
102 will be smart enough to interact with it for reading and writing cookies.
103  
104 use Plack::Builder;
105 builder {
106 enable 'WURFL::ScientiaMobile', config => {
107 api_key => '...',
108 cache => Net::WURFL::ScientiaMobile::Cache::Cookie->new,
109 };
110 $app;
111 };
112  
113 =head1 ARGUMENTS
114  
115 This middleware accepts the following arguments.
116  
117 =head2 config
118  
119 This argument is required. It must be a hashref containing the configuration options for the
120 L<Net::WURFL::ScientiaMobile> client object. The only required option is C<api_key>, but check
121 the documentation for L<Net::WURFL::ScientiaMobile> to learn about all possible options.
122  
123 =head1 SUBROUTINES
124  
125 =head2 PSGI_KEY
126  
127 Returns the PSGI C<$env> key under which you'd expect to find either an instance of
128 L<Net::WURFL::ScientiaMobile> (pre-populated with the device capabilities) or an exception
129 object.
130  
131 =head2 get_from_env
132  
133 Given a L<Plack> C<$env>, returns the L<Net::WURFL::ScientiaMobile> object containing the device
134 capabilities. If the call to C<detectDevice()> threw an exception instead of succeeding, this method
135 returns undef.
136  
137 For example, in your web application:
138  
139 use Plack::Middleware::WURFL::ScientiaMobile;
140
141 sub my_handler {
142 ...
143 my $env = ...; # your web framework provides this
144 my $scientiamobile = Plack::Middleware::WURFL::ScientiaMobile->get_from_env($env);
145 if (!$scientiamobile) {
146 my $error = Plack::Middleware::WURFL::ScientiaMobile->get_error_from_env($env);
147 ....
148 }
149 ....
150 }
151  
152 Refer to the documentation of your web framework to learn how to access C<$env>. For example,
153 L<Catalyst> provides it in C<$ctx-E<gt>request-E<gt>env>, Dancer provides it in C<request-E<gt>env>,
154 L<Mojo> provides it in C<$tx-E<gt>req-E<gt>env>.
155  
156 =head2 get_error_from_env
157  
158 Given a L<Plack> C<$env>, returns the L<Exception::Class> object representing the failure. If
159 no exception was caught, undef is returned.
160  
161 Refer to the documentation of L<Net::WURFL::ScientiaMobile> for an explanation of possible
162 exceptions.
163  
164 =head1 SEE ALSO
165  
166 L<Plack>, L<Plack::Middleware>, L<Net::WURFL::ScientiaMobile>
167  
168 =head1 AUTHOR
169  
170 Alessandro Ranellucci C<< <aar@cpan.org> >>
171  
172 =head1 COPYRIGHT & LICENSE
173  
174 Copyright 2012, ScientiaMobile, Inc.
175  
176 This program is free software; you can redistribute it and/or modify
177 it under the same terms as Perl itself.
178  
179 =cut
180  
181 3 1;
↑ Summary