File Coverage

blib/lib/WWW/Connpass/Session.pm
Criterion Covered Total %
statement 45 205 21.9
branch 0 22 0.0
condition n/a
subroutine 15 38 39.4
pod 0 19 0.0
total 60 284 21.1


line stmt bran cond sub pod time code
1             package WWW::Connpass::Session;
2 1     1   3 use strict;
  1         0  
  1         20  
3 1     1   3 use warnings;
  1         0  
  1         18  
4              
5 1     1   3 use Carp qw/croak/;
  1         1  
  1         39  
6 1     1   437 use Web::Query qw/wq/;
  1         81759  
  1         51  
7 1     1   711 use Text::CSV_XS;
  1         6865  
  1         45  
8 1     1   5 use JSON 2;
  1         15  
  1         6  
9 1     1   106 use URI;
  1         2  
  1         19  
10              
11 1     1   349 use WWW::Connpass::Agent;
  1         3  
  1         31  
12 1     1   341 use WWW::Connpass::Event;
  1         1  
  1         21  
13 1     1   308 use WWW::Connpass::Event::Questionnaire;
  1         1  
  1         20  
14 1     1   331 use WWW::Connpass::Event::Participants;
  1         2  
  1         18  
15 1     1   282 use WWW::Connpass::Group;
  1         2  
  1         20  
16 1     1   283 use WWW::Connpass::Place;
  1         2  
  1         18  
17 1     1   289 use WWW::Connpass::User;
  1         1  
  1         25  
18              
19 1     1   3 use constant DEBUG => $ENV{WWW_CONNPASS_DEBUG};
  1         1  
  1         1562  
20              
21             my $_JSON = JSON->new->utf8;
22              
23             sub new {
24 0     0 0   my ($class, $user, $pass, $opt) = @_;
25              
26 0           my $mech = WWW::Connpass::Agent->new(%$opt, cookie_jar => {});
27 0           $mech->get('https://connpass.com/login/');
28 0           $mech->form_id('login_form');
29 0           $mech->set_fields(username => $user, password => $pass);
30 0           my $res = $mech->submit();
31 0           _check_response_error_or_throw($res);
32              
33 0     0     my $error = wq($res->decoded_content)->find('.errorlist > li')->map(sub { $_->text });
  0            
34 0 0         if (@$error) {
35 0           my $message = join "\n", @$error;
36 0           croak "Failed to login by user: $user. error: $message";
37             }
38              
39 0           return bless {
40             mech => $mech,
41             user => $user,
42             } => $class;
43             }
44              
45 0     0 0   sub user { shift->{user} }
46              
47             sub _check_response_error_or_throw {
48 0     0     my $res = shift;
49 0 0         unless ($res->is_success) {
50 0           my $message = sprintf '[ERROR] %d %s: %s', $res->code, $res->message, $res->decoded_content;
51 0           $message = "=REQUEST\n".$res->request->as_string."\nRESPONSE=\n".$res->as_string if DEBUG;
52 0           local $Carp::CarpLevel = $Carp::CarpLevel + 1;
53 0           croak $message;
54             }
55 0           return $res;
56             }
57              
58             sub new_event {
59 0     0 0   my ($self, $title) = @_;
60              
61 0           my $res = $self->{mech}->request_like_xhr(POST => 'http://connpass.com/api/event/', {
62             title => $title,
63             place => undef,
64             });
65 0           _check_response_error_or_throw($res);
66              
67 0           my $data = $_JSON->decode($res->decoded_content);
68 0           return WWW::Connpass::Event->new(session => $self, event => $data);
69             }
70              
71             sub fetch_event_by_id {
72 0     0 0   my ($self, $event_id) = @_;
73 0           my $uri = sprintf 'http://connpass.com/api/event/%d', $event_id;
74              
75 0           my $res = $self->{mech}->get($uri);
76 0 0         return if $res->code == 404;
77 0           _check_response_error_or_throw($res);
78              
79 0           my $data = $_JSON->decode($res->decoded_content);
80 0           return WWW::Connpass::Event->new(session => $self, event => $data);
81             }
82              
83             sub refetch_event {
84 0     0 0   my ($self, $event) = @_;
85 0           return $self->fetch_event_by_id($event->id);
86             }
87              
88             sub update_event {
89 0     0 0   my ($self, $event, $diff) = @_;
90 0           my $uri = sprintf 'http://connpass.com/api/event/%d', $event->id;
91              
92             my $res = $self->{mech}->request_like_xhr(PUT => $uri, {
93 0           %{ $event->raw_data },
94             $event->place ? (
95             place => $event->place->{id},
96 0 0         ) : (),
97             %$diff,
98             });
99 0           _check_response_error_or_throw($res);
100              
101 0           $event = $_JSON->decode($res->decoded_content);
102 0           return WWW::Connpass::Event->new(session => $self, event => $event);
103             }
104              
105             sub update_waitlist_count {
106 0     0 0   my ($self, $event, @waitlist_count) = @_;
107 0           my %update = map { $_->id => $_ } grep { !$_->is_new } @waitlist_count;
  0            
  0            
108 0 0         my @update = map { $_->raw_data } map { delete $update{$_->id} || $_ } $event->waitlist_count();
  0            
  0            
109 0           push @update => map { $_->raw_data } grep { $_->is_new } @waitlist_count;
  0            
  0            
110              
111 0           my $uri = sprintf 'http://connpass.com/api/event/%d/participation_type/', $event->id;
112 0           my $res = $self->{mech}->request_like_xhr(PUT => $uri, \@update);
113 0           _check_response_error_or_throw($res);
114              
115 0           return $self->refetch_event($event);
116             }
117              
118             sub fetch_questionnaire_by_event {
119 0     0 0   my ($self, $event) = @_;
120 0           my $uri = sprintf 'http://connpass.com/api/question/%d', $event->id;
121 0           my $res = $self->{mech}->get($uri);
122             # HTTP::Response
123 0 0         if ($res->code == 404) {
124 0           return WWW::Connpass::Event::Questionnaire->new(
125             session => $self,
126             questionnaire => {
127             id => undef,
128             questions => [],
129             event => $event->id,
130             },
131             );
132             }
133 0           _check_response_error_or_throw($res);
134              
135 0           my $data = $_JSON->decode($res->decoded_content);
136 0           return WWW::Connpass::Event::Questionnaire->new(session => $self, questionnaire => $data);
137             }
138              
139             sub update_questionnaire {
140 0     0 0   my ($self, $questionnaire, @question) = @_;
141              
142 0 0         my $method = $questionnaire->is_new ? 'POST' : 'PUT';
143 0           my $uri = sprintf 'http://connpass.com/api/question/%d', $questionnaire->event;
144             my $res = $self->{mech}->request_like_xhr($method => $uri, {
145 0           %{ $questionnaire->raw_data },
146 0           questions => [map { $_->raw_data } @question],
  0            
147             });
148 0           _check_response_error_or_throw($res);
149              
150 0           my $data = $_JSON->decode($res->decoded_content);
151 0           return WWW::Connpass::Event::Questionnaire->new(session => $self, questionnaire => $data);
152             }
153              
154             sub register_place {
155 0     0 0   my ($self, %data) = @_;
156              
157 0           my $res = $self->{mech}->request_like_xhr(POST => 'http://connpass.com/api/place/', \%data);
158 0           _check_response_error_or_throw($res);
159              
160 0           my $data = $_JSON->decode($res->decoded_content);
161 0           return WWW::Connpass::Place->new(session => $self, place => $data);
162             }
163              
164             sub add_owner_to_event {
165 0     0 0   my ($self, $event, $user) = @_;
166 0           my $uri = sprintf 'http://connpass.com/api/event/%d/owner/%d', $event->id, $user->id;
167 0           my $res = $self->{mech}->request_like_xhr(POST => $uri, { id => $user->id });
168 0           _check_response_error_or_throw($res);
169              
170 0           my $data = $_JSON->decode($res->decoded_content);
171 0           return WWW::Connpass::User->new(user => $data);
172             }
173              
174             sub update_place {
175 0     0 0   my ($self, $place, %data) = @_;
176 0           my $uri = sprintf 'http://connpass.com/api/place/%d', $place->id;
177             my $res = $self->{mech}->request_like_xhr(PUT => $uri, {
178 0           %{ $place->raw_data },
  0            
179             %data,
180             });
181 0           _check_response_error_or_throw($res);
182              
183 0           my $data = $_JSON->decode($res->decoded_content);
184 0           return WWW::Connpass::Place->new(session => $self, place => $data);
185             }
186              
187             sub fetch_all_places {
188 0     0 0   my $self = shift;
189              
190 0           my $res = $self->{mech}->get('http://connpass.com/api/place/');
191 0           _check_response_error_or_throw($res);
192              
193 0           my $data = $_JSON->decode($res->decoded_content);
194 0           return map { WWW::Connpass::Place->new(session => $self, place => $_) } @$data;
  0            
195             }
196              
197             sub fetch_place_by_id {
198 0     0 0   my ($self, $place_id) = @_;
199 0           my $uri = sprintf 'http://connpass.com/api/place/%d', $place_id;
200              
201 0           my $res = $self->{mech}->get($uri);
202 0 0         return if $res->code == 404;
203 0           _check_response_error_or_throw($res);
204              
205 0           my $data = $_JSON->decode($res->decoded_content);
206 0           return WWW::Connpass::Place->new(session => $self, place => $data);
207             }
208              
209             sub refetch_place {
210 0     0 0   my ($self, $place) = @_;
211 0           return $self->fetch_place_by_id($place->id);
212             }
213              
214             sub search_users_by_name {
215 0     0 0   my ($self, $name) = @_;
216 0           my $uri = URI->new('http://connpass.com/api/user/');
217 0           $uri->query_form(q => $name);
218              
219 0           my $res = $self->{mech}->get($uri);
220 0           _check_response_error_or_throw($res);
221              
222 0           my $data = $_JSON->decode($res->decoded_content);
223 0           return map { WWW::Connpass::User->new(user => $_) } @$data;
  0            
224             }
225              
226             sub fetch_managed_events {
227 0     0 0   my $self = shift;
228 0           my $res = $self->{mech}->get('http://connpass.com/editmanage/');
229 0           _check_response_error_or_throw($res);
230 0           return map { WWW::Connpass::Event->new(session => $self, event => $_) }
231 0           map { $_JSON->decode($_) } @{
232 0     0     wq($res->decoded_content)->find('#EventManageTable .event_list > table')->map(sub { $_->data('obj') })
  0            
233 0           };
234             }
235              
236             sub fetch_organized_groups {
237 0     0 0   my $self = shift;
238 0           my $res = $self->{mech}->get('http://connpass.com/group/');
239 0           _check_response_error_or_throw($res);
240              
241             my $groups = wq($res->decoded_content)->find('.series_lists_area .series_list .title a')->map(sub {
242 0     0     my $title = $_->text;
243 0           my $url = $_->attr('href');
244 0           my ($id) = wq(_check_response_error_or_throw($self->{mech}->get($url))->decoded_content)->find('.icon_gray_edit')->parent()->attr('href') =~ m{/series/([^/]+)/edit/$};
245 0           my ($name) = $url =~ m{^https?://([^.]+)\.connpass\.com/};
246 0 0         return unless $id;
247             return {
248 0           id => $id,
249             name => $name,
250             title => $title,
251             url => $url,
252             };
253 0           });
254              
255 0           return map { WWW::Connpass::Group->new(session => $self, group => $_) } @$groups;
  0            
256             }
257              
258             sub fetch_participants_info {
259 0     0 0   my ($self, $event) = @_;
260 0           my $uri = sprintf 'http://connpass.com/event/%d/participants_csv/', $event->id;
261              
262 0           my $res = $self->{mech}->get($uri);
263 0           _check_response_error_or_throw($res);
264              
265             # HTTP::Response
266 0           my $content = $res->decoded_content;
267              
268 0           my $csv = Text::CSV_XS->new({ binary => 1, decode_utf8 => 0, eol => "\r\n", auto_diag => 1 });
269              
270 0           my @questions = $event->questionnaire->questions;
271 0           my @params = qw/waitlist_name username nickname comment registration attendance/;
272 0           push @params => map { 'answer_'.$_ } keys @questions;
  0            
273 0           push @params => qw/updated_at receipt_id/;
274              
275 0           my @lines = split /\r\n/, $content;
276 0           my %label; @label{@params} = do {
  0            
277 0           my $header = shift @lines;
278 0           my $success = $csv->parse($header);
279 0 0         die "Invalid CSV syntax: $header" unless $success;
280 0           $csv->fields;
281             };
282              
283 0           my @rows;
284 0           for my $line (@lines) {
285 0           my $success = $csv->parse($line);
286 0 0         die "Invalid CSV syntax: $line" unless $success;
287              
288 0           my %row;
289 0           @row{@params} = $csv->fields;
290 0           push @rows => \%row;
291             }
292              
293 0           return WWW::Connpass::Event::Participants->new(
294             label => \%label,
295             rows => \@rows,
296             );
297             }
298              
299             1;
300             __END__