blib/lib/WWW/Connpass/Session.pm

filter: Statementstmt 22.7% 47 / 207 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 39.4% 15 / 38 Podpod 0.0% 0 / 19 totaltotal 21.6% 62 / 286 SCAR 45.8
CC
13
Cov
21%
fetch_participants_info
20.0
new
6.0
_check_response_error_or_throw
6.0
CRAP 97.8
↑ Summary blib/lib/WWW/Connpass/Event/Question.pm »
1 package WWW::Connpass::Session;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Web::Query qw/wq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Text::CSV_XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use WWW::Connpass::Agent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use WWW::Connpass::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use WWW::Connpass::Event::Questionnaire;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use WWW::Connpass::Event::Participants;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use WWW::Connpass::Group;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use WWW::Connpass::Place;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use WWW::Connpass::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use constant DEBUG => $ENV{WWW_CONNPASS_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 my $_JSON = JSON->new->utf8;
22  
23 sub new {
24 0 ▶ my ($class, $user, $pass, $opt) = @_;
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
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 ▶ my $error = wq($res->decoded_content)->find('.errorlist > li')->map(sub { $_->text });
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 0 ▶ if (@$error) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$error) 0 0 0
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 ▶sub user { shift->{user} }
Subroutine user: not called CC=1 SCAR=6.9
Pod undocumented
46  
47 sub _check_response_error_or_throw {
48 0 ▶ my $res = shift;
Subroutine _check_response_error_or_throw: not called CC=2 SCAR=17.9
49 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
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 ▶ my ($self, $title) = @_;
Subroutine new_event: not called CC=1 SCAR=6.9
Pod undocumented
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 ▶ my ($self, $event_id) = @_;
Subroutine fetch_event_by_id: not called CC=2 SCAR=17.9
Pod undocumented
73 0 my $uri = sprintf 'http://connpass.com/api/event/%d', $event_id;
74  
75 0 my $res = $self->{mech}->get($uri);
76 0 ▶ return if $res->code == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 404 0 0 0
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 ▶ my ($self, $event) = @_;
Subroutine refetch_event: not called CC=1 SCAR=6.9
Pod undocumented
85 0 return $self->fetch_event_by_id($event->id);
86 }
87  
88 sub update_event {
89 0 ▶ my ($self, $event, $diff) = @_;
Subroutine update_event: not called CC=2 SCAR=17.9
Pod undocumented
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 },
Branch: 1 branch
BranchTrueFalseTotal
$event->place ? : 0 0 0
94 $event->place ? (
95 place => $event->place->{id},
96 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 ▶ my ($self, $event, @waitlist_count) = @_;
Subroutine update_waitlist_count: not called CC=2 SCAR=17.9
Pod undocumented
107 0 my %update = map { $_->id => $_ } grep { !$_->is_new } @waitlist_count;
108 0 ▶ my @update = map { $_->raw_data } map { delete $update{$_->id} || $_ } $event->waitlist_count();
Branch: 1 branch
BranchTrueFalseTotal
delete $update{$_->id} or $_ 0 0 0
109 0 push @update => map { $_->raw_data } grep { $_->is_new } @waitlist_count;
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 ▶ my ($self, $event) = @_;
Subroutine fetch_questionnaire_by_event: not called CC=2 SCAR=17.9
Pod undocumented
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 ▶ if ($res->code == 404) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 404) 0 0 0
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 ▶ my ($self, $questionnaire, @question) = @_;
Subroutine update_questionnaire: not called CC=2 SCAR=17.9
Pod undocumented
141  
142 0 ▶ my $method = $questionnaire->is_new ? 'POST' : 'PUT';
Branch: 1 branch
BranchTrueFalseTotal
$questionnaire->is_new ? : 0 0 0
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],
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 ▶ my ($self, %data) = @_;
Subroutine register_place: not called CC=1 SCAR=6.9
Pod undocumented
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 ▶ my ($self, $event, $user) = @_;
Subroutine add_owner_to_event: not called CC=1 SCAR=6.9
Pod undocumented
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 ▶ my ($self, $place, %data) = @_;
Subroutine update_place: not called CC=1 SCAR=6.9
Pod undocumented
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 },
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 ▶ my $self = shift;
Subroutine fetch_all_places: not called CC=1 SCAR=6.9
Pod undocumented
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;
195 }
196  
197 sub fetch_place_by_id {
198 0 ▶ my ($self, $place_id) = @_;
Subroutine fetch_place_by_id: not called CC=2 SCAR=17.9
Pod undocumented
199 0 my $uri = sprintf 'http://connpass.com/api/place/%d', $place_id;
200  
201 0 my $res = $self->{mech}->get($uri);
202 0 ▶ return if $res->code == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 404 0 0 0
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 ▶ my ($self, $place) = @_;
Subroutine refetch_place: not called CC=1 SCAR=6.9
Pod undocumented
211 0 return $self->fetch_place_by_id($place->id);
212 }
213  
214 sub search_users_by_name {
215 0 ▶ my ($self, $name) = @_;
Subroutine search_users_by_name: not called CC=1 SCAR=6.9
Pod undocumented
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;
224 }
225  
226 sub fetch_managed_events {
227 0 ▶ my $self = shift;
Subroutine fetch_managed_events: not called CC=1 SCAR=6.9
Pod undocumented
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 ▶ wq($res->decoded_content)->find('#EventManageTable .event_list > table')->map(sub { $_->data('obj') })
Subroutine __ANON__: not called CC=1 SCAR=6.9
233 0 };
234 }
235  
236 sub fetch_organized_groups {
237 0 ▶ my $self = shift;
Subroutine fetch_organized_groups: not called CC=1 SCAR=6.9
Pod undocumented
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 ▶ my $title = $_->text;
Subroutine __ANON__: not called CC=2 SCAR=17.9
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 ▶ return unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
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;
256 }
257  
258 sub fetch_participants_info {
259 0 ▶ my ($self, $event) = @_;
Subroutine fetch_participants_info: not called CC=4 SCAR=30.0
Pod undocumented
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;
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 {
277 0 my $header = shift @lines;
278 0 my $success = $csv->parse($header);
279 0 ▶ die "Invalid CSV syntax: $header" unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 0 0
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 ▶ die "Invalid CSV syntax: $line" unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 0 0
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 1;
300 __END__
↑ Summary blib/lib/WWW/Connpass/Event/Question.pm »