|
| 1 | +package X::ACT::Client; |
| 2 | +use Mojo::Base -base; |
| 3 | +use feature qw(signatures); |
| 4 | +no warnings qw(experimental::signatures); |
| 5 | +use Mojo::UserAgent; |
| 6 | +use Mojo::URL; |
| 7 | + |
| 8 | +has ua => sub{ Mojo::UserAgent->new }; |
| 9 | +has user => sub{ die 'user attribute is required' }; |
| 10 | +has password => sub{ die 'password attribute is required' }; |
| 11 | +has url_base => sub{ Mojo::URL->new('http://workshop.barcelona.pm') }; |
| 12 | +has project => 'barcelona2016'; |
| 13 | + |
| 14 | +sub login($self, $cb=undef) { |
| 15 | + my @form = ( form => { |
| 16 | + credential_0 => $self->user, |
| 17 | + credential_1 => $self->password, |
| 18 | + destination => '/'. $self->project .'/main' |
| 19 | + }); |
| 20 | + |
| 21 | + if ($cb) { |
| 22 | + return $self->ua->post( |
| 23 | + $self->_url('LOGIN'), @form, sub{ my $tx = pop; $cb->(@_, $self->_login($tx)) } |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + $self->_login( $self->ua->post( $self->_url('LOGIN'), @form ) ); |
| 28 | +} |
| 29 | +sub _login($self, $tx) { |
| 30 | + die 'Login has failed' unless $tx->res->code == 302; |
| 31 | + $self; |
| 32 | +} |
| 33 | + |
| 34 | +sub userdata($self, $cb=undef) { |
| 35 | + if ($cb) { |
| 36 | + return $self->ua->get( $self->_url('change') => sub{ |
| 37 | + my $tx = pop; |
| 38 | + $cb->(@_, _userdata($tx)) |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + _userdata( $self->ua->get($self->_url('change')) ); |
| 43 | +} |
| 44 | +sub _userdata($tx, $data={}) { |
| 45 | + if ( my $err = $tx->error ) { |
| 46 | + die "$err->{code} response: $err->{message}" if $err->{code}; |
| 47 | + die "Connection error: $err->{message}"; |
| 48 | + } |
| 49 | + |
| 50 | + my $dom = $tx->res->dom; |
| 51 | + |
| 52 | + my $selector = 'input[type="radio"][checked]' |
| 53 | + . ',input[type="email"]' |
| 54 | + . ',input[type="text"]'; |
| 55 | + |
| 56 | + $dom->find($selector)->each(sub{ |
| 57 | + $data->{$_->attr('name')} = $_->attr('value'); |
| 58 | + }); |
| 59 | + $dom->find('textarea')->each(sub{ |
| 60 | + $data->{$_->attr('name')} = $_->text; |
| 61 | + }); |
| 62 | + $dom->find('select')->each(sub{ |
| 63 | + $data->{$_->attr('name')} = eval{$_->at('option[selected]')->attr('value')}||''; |
| 64 | + }); |
| 65 | + |
| 66 | + $data; |
| 67 | +} |
| 68 | + |
| 69 | +sub _url($self, @path) { |
| 70 | + $self->url_base->clone->path( '/'. join('/', $self->project, @path) ); |
| 71 | +} |
| 72 | + |
| 73 | +1; |
0 commit comments