File Coverage

blib/lib/Plack/Middleware/SocketIO/Resource.pm
Criterion Covered Total %
statement 33 76 43.4
branch 0 16 0.0
condition 0 6 0.0
subroutine 11 20 55.0
pod 5 6 83.3
total 49 124 39.5


line stmt bran cond sub pod time code
1             package Plack::Middleware::SocketIO::Resource;
2              
3 1     1   5 use strict;
  1         2  
  1         31  
4 1     1   6 use warnings;
  1         2  
  1         25  
5              
6 1     1   831 use Plack::Request;
  1         68271  
  1         37  
7 1     1   832 use Plack::Middleware::SocketIO::Connection;
  1         3  
  1         32  
8 1     1   658 use Plack::Middleware::SocketIO::Handle;
  1         5  
  1         44  
9              
10 1     1   838 use Plack::Middleware::SocketIO::JSONPPolling;
  1         3  
  1         26  
11 1     1   552 use Plack::Middleware::SocketIO::WebSocket;
  1         4  
  1         31  
12 1     1   561 use Plack::Middleware::SocketIO::XHRMultipart;
  1         3  
  1         21  
13 1     1   518 use Plack::Middleware::SocketIO::XHRPolling;
  1         3  
  1         23  
14 1     1   510 use Plack::Middleware::SocketIO::Htmlfile;
  1         2  
  1         29  
15              
16             sub instance {
17 0     0 1   my $class = shift;
18              
19 1     1   6 no strict;
  1         2  
  1         473  
20              
21 0   0       ${"$class\::_instance"} ||= $class->_new_instance(@_);
  0            
22              
23 0           return ${"$class\::_instance"};
  0            
24             }
25              
26             sub connection {
27 0     0 1   my $self = shift;
28 0           my ($id) = @_;
29              
30 0           return $self->{connections}->{$id};
31             }
32              
33             sub connections {
34 0     0 0   my $self = shift;
35              
36 0           return values %{$self->{connections}};
  0            
37             }
38              
39             sub add_connection {
40 0     0 1   my $self = shift;
41              
42 0           my $conn = $self->_build_connection(@_);
43              
44 0           $self->{connections}->{$conn->id} = $conn;
45              
46 0           return $conn;
47             }
48              
49             sub remove_connection {
50 0     0 1   my $self = shift;
51 0           my ($id) = @_;
52              
53 0           delete $self->{connections}->{$id};
54             }
55              
56             sub finalize {
57 0     0 1   my $self = shift;
58 0           my ($env, $cb) = @_;
59              
60 0           my ($resource, $type) = $env->{PATH_INFO} =~ m{^/([^\/]+)/([^\/]+)/?};
61 0 0 0       return unless $resource && $type;
62              
63 0           my $transport =
64             $self->_build_transport($type, env => $env, resource => $resource);
65 0 0         return unless $transport;
66              
67 0           return $transport->finalize($cb);
68             }
69              
70             sub _new_instance {
71 0     0     my $class = shift;
72              
73 0           my $self = bless {@_}, $class;
74              
75 0           $self->{connections} = {};
76              
77 0           return $self;
78             }
79              
80             sub _build_transport {
81 0     0     my $self = shift;
82 0           my ($type, @args) = @_;
83              
84 0           my $class;
85 0 0         if ($type eq 'xhr-multipart') {
    0          
    0          
    0          
    0          
86 0           $class = 'XHRMultipart';
87             }
88             elsif ($type eq 'xhr-polling') {
89 0           $class = 'XHRPolling';
90             }
91             elsif ($type eq 'jsonp-polling') {
92 0           $class = 'JSONPPolling';
93             }
94             elsif ($type =~ m/^(?:flash|web)socket$/) {
95 0           $class = 'WebSocket';
96             }
97             elsif ($type =~ m/^htmlfile$/) {
98 0           $class = 'Htmlfile';
99             }
100              
101 0 0         return unless $class;
102              
103 0           $class = "Plack::Middleware::SocketIO::$class";
104              
105 0           return $class->new(@args);
106             }
107              
108             sub _build_connection {
109 0     0     my $self = shift;
110              
111 0           return Plack::Middleware::SocketIO::Connection->new(@_);
112             }
113              
114             1;
115             __END__