blib/lib/WWW/Connpass/Session.pm

filter: Statementstmt 22.0% 38 / 172 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 35.2% 12 / 34 Podpod 0.0% 0 / 18 totaltotal 20.4% 50 / 244 CC 11 SCAR 13.5
CC
11
Cov
20%
_check_response_error_or_throw
12.0
new
6.0
fetch_event_by_id
6.0
CRAP 3.9
« blib/lib/WWW/Connpass/Event/Waitlist.pm ↑ Summary blib/lib/WWW/Connpass/Event/Questionnaire.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 JSON 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use WWW::Connpass::Agent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use WWW::Connpass::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use WWW::Connpass::Event::Questionnaire;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use WWW::Connpass::Group;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use WWW::Connpass::Place;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use WWW::Connpass::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 my $_JSON = JSON->new->utf8;
18  
19 sub new {
20 0 ▶ my ($class, $user, $pass, $opt) = @_;
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
21  
22 0 my $mech = WWW::Connpass::Agent->new(%$opt, cookie_jar => {});
23 0 $mech->get('https://connpass.com/login/');
24 0 $mech->form_id('login_form');
25 0 $mech->set_fields(username => $user, password => $pass);
26 0 my $res = $mech->submit();
27 0 _check_response_error_or_throw($res);
28  
29 0 ▶ my $error = wq($res->decoded_content)->find('.errorlist > li')->map(sub { $_->text });
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 0 ▶ if (@$error) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$error) 0 0 0
31 0 my $message = join "\n", @$error;
32 0 croak "Failed to login by user: $user. error: $message";
33 }
34  
35 0 return bless {
36 mech => $mech,
37 user => $user,
38 } => $class;
39 }
40  
41 0 ▶sub user { shift->{user} }
Subroutine user: not called CC=1 SCAR=6.9
Pod undocumented
42  
43 sub _check_response_error_or_throw {
44 0 ▶ my $res = shift;
Subroutine _check_response_error_or_throw: not called CC=3 SCAR=24.8
45 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
46 0 my $message = sprintf '[ERROR] %d %s: %s', $res->code, $res->message, $res->decoded_content;
47 0 ▶ $message = "=REQUEST\n".$res->request->as_string."\nRESPONSE=\n".$res->as_string if $ENV{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 0 0
48 0 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
49 0 croak $message;
50 }
51 0 return $res;
52 }
53  
54 sub new_event {
55 0 ▶ my ($self, $title) = @_;
Subroutine new_event: not called CC=1 SCAR=6.9
Pod undocumented
56  
57 0 my $res = $self->{mech}->request_like_xhr(POST => 'http://connpass.com/api/event/', {
58 title => $title,
59 place => undef,
60 });
61 0 _check_response_error_or_throw($res);
62  
63 0 my $data = $_JSON->decode($res->decoded_content);
64 0 return WWW::Connpass::Event->new(session => $self, event => $data);
65 }
66  
67 sub fetch_event_by_id {
68 0 ▶ my ($self, $event_id) = @_;
Subroutine fetch_event_by_id: not called CC=2 SCAR=17.9
Pod undocumented
69 0 my $uri = sprintf 'http://connpass.com/api/event/%d', $event_id;
70  
71 0 my $res = $self->{mech}->get($uri);
72 0 ▶ return if $res->code == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 404 0 0 0
73 0 _check_response_error_or_throw($res);
74  
75 0 my $data = $_JSON->decode($res->decoded_content);
76 0 return WWW::Connpass::Event->new(session => $self, event => $data);
77 }
78  
79 sub refetch_event {
80 0 ▶ my ($self, $event) = @_;
Subroutine refetch_event: not called CC=1 SCAR=6.9
Pod undocumented
81 0 return $self->fetch_event_by_id($event->id);
82 }
83  
84 sub update_event {
85 0 ▶ my ($self, $event, $diff) = @_;
Subroutine update_event: not called CC=2 SCAR=17.9
Pod undocumented
86 0 my $uri = sprintf 'http://connpass.com/api/event/%d', $event->id;
87  
88 my $res = $self->{mech}->request_like_xhr(PUT => $uri, {
89 0 ▶ %{ $event->raw_data },
Branch: 1 branch
BranchTrueFalseTotal
$event->place ? : 0 0 0
90 $event->place ? (
91 place => $event->place->{id},
92 0 ) : (),
93 %$diff,
94 });
95 0 _check_response_error_or_throw($res);
96  
97 0 $event = $_JSON->decode($res->decoded_content);
98 0 return WWW::Connpass::Event->new(session => $self, event => $event);
99 }
100  
101 sub update_waitlist_count {
102 0 ▶ my ($self, $event, @waitlist_count) = @_;
Subroutine update_waitlist_count: not called CC=2 SCAR=17.9
Pod undocumented
103 0 my %update = map { $_->id => $_ } grep { !$_->is_new } @waitlist_count;
104 0 ▶ my @update = map { $_->raw_data } map { delete $update{$_->id} || $_ } $event->waitlist_count();
Branch: 1 branch
BranchTrueFalseTotal
delete $update{$_->id} or $_ 0 0 0
105 0 push @update => map { $_->raw_data } grep { $_->is_new } @waitlist_count;
106  
107 0 my $uri = sprintf 'http://connpass.com/api/event/%d/participation_type/', $event->id;
108 0 my $res = $self->{mech}->request_like_xhr(PUT => $uri, \@update);
109 0 _check_response_error_or_throw($res);
110  
111 0 return $self->refetch_event($event);
112 }
113  
114 sub fetch_questionnaire_by_event {
115 0 ▶ my ($self, $event) = @_;
Subroutine fetch_questionnaire_by_event: not called CC=2 SCAR=17.9
Pod undocumented
116 0 my $uri = sprintf 'http://connpass.com/api/question/%d', $event->id;
117 0 my $res = $self->{mech}->get($uri);
118 # HTTP::Response
119 0 ▶ if ($res->code == 404) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 404) 0 0 0
120 0 return WWW::Connpass::Event::Questionnaire->new(
121 session => $self,
122 questionnaire => {
123 id => undef,
124 questions => [],
125 event => $event->id,
126 },
127 );
128 }
129 0 _check_response_error_or_throw($res);
130  
131 0 my $data = $_JSON->decode($res->decoded_content);
132 0 return WWW::Connpass::Event::Questionnaire->new(session => $self, questionnaire => $data);
133 }
134  
135 sub update_questionnaire {
136 0 ▶ my ($self, $questionnaire, @question) = @_;
Subroutine update_questionnaire: not called CC=2 SCAR=17.9
Pod undocumented
137  
138 0 ▶ my $method = $questionnaire->is_new ? 'POST' : 'PUT';
Branch: 1 branch
BranchTrueFalseTotal
$questionnaire->is_new ? : 0 0 0
139 0 my $uri = sprintf 'http://connpass.com/api/question/%d', $questionnaire->event;
140 my $res = $self->{mech}->request_like_xhr($method => $uri, {
141 0 %{ $questionnaire->raw_data },
142 0 questions => [map { $_->raw_data } @question],
143 });
144 0 _check_response_error_or_throw($res);
145  
146 0 my $data = $_JSON->decode($res->decoded_content);
147 0 return WWW::Connpass::Event::Questionnaire->new(session => $self, questionnaire => $data);
148 }
149  
150 sub register_place {
151 0 ▶ my ($self, %data) = @_;
Subroutine register_place: not called CC=1 SCAR=6.9
Pod undocumented
152  
153 0 my $res = $self->{mech}->request_like_xhr(POST => 'http://connpass.com/api/place/', \%data);
154 0 _check_response_error_or_throw($res);
155  
156 0 my $data = $_JSON->decode($res->decoded_content);
157 0 return WWW::Connpass::Event::Place->new(session => $self, place => $data);
158 }
159  
160 sub add_owner_to_event {
161 0 ▶ my ($self, $event, $user) = @_;
Subroutine add_owner_to_event: not called CC=1 SCAR=6.9
Pod undocumented
162 0 my $uri = sprintf 'http://connpass.com/api/event/%d/owner/%d', $event->id, $user->id;
163 0 my $res = $self->{mech}->request_like_xhr(POST => $uri, { id => $user->id });
164 0 _check_response_error_or_throw($res);
165  
166 0 my $data = $_JSON->decode($res->decoded_content);
167 0 return WWW::Connpass::User->new(user => $data);
168 }
169  
170 sub update_place {
171 0 ▶ my ($self, $place, %data) = @_;
Subroutine update_place: not called CC=1 SCAR=6.9
Pod undocumented
172 0 my $uri = sprintf 'http://connpass.com/api/place/%d', $place->id;
173 my $res = $self->{mech}->request_like_xhr(PUT => $uri, {
174 0 %{ $place->raw_data },
175 %data,
176 });
177 0 _check_response_error_or_throw($res);
178  
179 0 my $data = $_JSON->decode($res->decoded_content);
180 0 return WWW::Connpass::Place->new(session => $self, place => $data);
181 }
182  
183 sub fetch_all_places {
184 0 ▶ my $self = shift;
Subroutine fetch_all_places: not called CC=1 SCAR=6.9
Pod undocumented
185  
186 0 my $res = $self->{mech}->get('http://connpass.com/api/place/');
187 0 _check_response_error_or_throw($res);
188  
189 0 my $data = $_JSON->decode($res->decoded_content);
190 0 return map { WWW::Connpass::Place->new(session => $self, place => $_) } @$data;
191 }
192  
193 sub fetch_place_by_id {
194 0 ▶ my ($self, $place_id) = @_;
Subroutine fetch_place_by_id: not called CC=2 SCAR=17.9
Pod undocumented
195 0 my $uri = sprintf 'http://connpass.com/api/place/%d', $place_id;
196  
197 0 my $res = $self->{mech}->get($uri);
198 0 ▶ return if $res->code == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 404 0 0 0
199 0 _check_response_error_or_throw($res);
200  
201 0 my $data = $_JSON->decode($res->decoded_content);
202 0 return WWW::Connpass::Place->new(session => $self, place => $data);
203 }
204  
205 sub refetch_place {
206 0 ▶ my ($self, $place) = @_;
Subroutine refetch_place: not called CC=1 SCAR=6.9
Pod undocumented
207 0 return $self->fetch_place_by_id($place->id);
208 }
209  
210 sub search_users_by_name {
211 0 ▶ my ($self, $name) = @_;
Subroutine search_users_by_name: not called CC=1 SCAR=6.9
Pod undocumented
212 0 my $uri = URI->new('http://connpass.com/api/user/');
213 0 $uri->query_form(q => $name);
214  
215 0 my $res = $self->{mech}->get($uri);
216 0 _check_response_error_or_throw($res);
217  
218 0 my $data = $_JSON->decode($res->decoded_content);
219 0 return map { WWW::Connpass::User->new(user => $_) } @$data;
220 }
221  
222 sub fetch_managed_events {
223 0 ▶ my $self = shift;
Subroutine fetch_managed_events: not called CC=1 SCAR=6.9
Pod undocumented
224 0 my $res = $self->{mech}->get('http://connpass.com/editmanage/');
225 0 _check_response_error_or_throw($res);
226 0 return map { WWW::Connpass::Event->new(session => $self, event => $_) }
227 0 map { $_JSON->decode($_) } @{
228 0 ▶ wq($res->decoded_content)->find('#EventManageTable .event_list > table')->map(sub { $_->data('obj') })
Subroutine __ANON__: not called CC=1 SCAR=6.9
229 0 };
230 }
231  
232 sub fetch_organized_groups {
233 0 ▶ my $self = shift;
Subroutine fetch_organized_groups: not called CC=1 SCAR=6.9
Pod undocumented
234 0 my $res = $self->{mech}->get('http://connpass.com/group/');
235 0 _check_response_error_or_throw($res);
236  
237 my $groups = wq($res->decoded_content)->find('.series_lists_area .series_list .title a')->map(sub {
238 0 ▶ my $title = $_->text;
Subroutine __ANON__: not called CC=2 SCAR=17.9
239 0 my $url = $_->attr('href');
240 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/$};
241 0 my ($name) = $url =~ m{^https?://([^.]+)\.connpass\.com/};
242 0 ▶ return unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
243 return {
244 0 id => $id,
245 name => $name,
246 title => $title,
247 url => $url,
248 };
249 0 });
250  
251 0 return map { WWW::Connpass::Group->new(session => $self, group => $_) } @$groups;
252 }
253  
254 1 1;
255 __END__
« blib/lib/WWW/Connpass/Event/Waitlist.pm ↑ Summary blib/lib/WWW/Connpass/Event/Questionnaire.pm »