File Coverage

blib/lib/WWW/Myki.pm
Criterion Covered Total %
statement 21 46 45.6
branch 0 6 0.0
condition n/a
subroutine 7 10 70.0
pod 2 2 100.0
total 30 64 46.8


line stmt bran cond sub pod time code
1             package WWW::Myki;
2              
3 1     1   31975 use warnings;
  1         2  
  1         37  
4 1     1   6 use strict;
  1         2  
  1         34  
5              
6 1     1   714 use WWW::Myki::Transaction;
  1         2  
  1         27  
7 1     1   677 use WWW::Myki::Card;
  1         4  
  1         38  
8 1     1   1757 use WWW::Mechanize;
  1         233238  
  1         44  
9 1     1   13 use HTML::TreeBuilder;
  1         3  
  1         16  
10 1     1   27 use Carp qw(croak);
  1         2  
  1         533  
11              
12             our $VERSION = '0.04';
13              
14             sub new {
15 0     0 1   my( $class, %args ) = @_;
16 0           my $self = {};
17 0           bless $self, $class;
18 0 0         $args{username} ? $self->{username} = $args{username} : croak 'Constructor failed: must specify username';
19 0 0         $args{password} ? $self->{password} = $args{password} : croak 'Constructor failed: must specify password';
20 0           $self->{_mech} = WWW::Mechanize->new( ssl_opts => { verify_hostname => 0 } );
21 0           $self->{_url} = {
22             base => 'https://www.mymyki.com.au/NTSWebPortal/',
23             account => 'Registered/MyMykiAccount.aspx?menu=My+myki+account',
24             login => 'Login.aspx'
25             };
26 0           $self->_login;
27 0           return $self
28             }
29              
30             sub _login {
31 0     0     my $self = shift;
32 0           $self->{_mech}->get( $self->{_url}->{base} . $self->{_url}->{login} );
33 0           $self->{_mech}->form_name( 'aspnetForm' );
34 0           $self->{_mech}->field( 'ctl00$uxContentPlaceHolder$uxUsername', $self->{username} );
35 0           $self->{_mech}->field( 'ctl00$uxContentPlaceHolder$uxPassword', $self->{password} );
36 0           $self->{_mech}->click( 'ctl00$uxContentPlaceHolder$uxLogin' );
37 0           my %form = (
38             '__EVENTTARGET' => 'ctl00$uxContentPlaceHolder$uxTimer',
39             '__EVENTVALIDATION' => $self->{_mech}->value('__EVENTVALIDATION'),
40             '__VIEWSTATE' => $self->{_mech}->value('__VIEWSTATE'),
41             'ctl00$ScriptManager1' => 'ctl00$uxContentPlaceHolder$Panel|ctl00$uxContentPlaceHolder$uxTimer'
42             );
43 0           my $r = $self->{_mech}->post( $self->{_url}->{base} . $self->{_url}->{account}, \%form );
44 0           my $t = HTML::TreeBuilder->new_from_content( $r->content );
45 0           $t = $t->look_down( id => 'ctl00_uxContentPlaceHolder_uxMyCards' );
46 0           my @cards = $t->look_down( _tag => 'td' );
47 0 0         @cards or croak 'Unable to retrieve card information';
48            
49 0           for( my $i=0; $i<@cards; $i+=4 ) {
50 0           $self->{_cards}->{$cards[$i]->as_text} =
51             WWW::Myki::Card->new(
52             id => $cards[$i]->as_text,
53             _mech => $self->{_mech},
54             _link => $self->{_url}->{base} . 'Registered/' . $cards[$i]->look_down( _tag => 'a' )->attr( 'href' ),
55             holder => $cards[$i+1]->as_text,
56             money => $cards[$i+2]->as_text,
57             pass => $cards[$i+2]->as_text
58             )
59             }
60             }
61              
62             sub cards {
63 0     0 1   return values %{$_[0]->{_cards}}
  0            
64             }
65              
66             =head1 NAME
67              
68             WWW::Myki - A simple Perl interface to Myki online account management portal
69              
70             =head1 VERSION 0.01
71              
72             =head1 SYNOPSIS
73              
74             use WWW::Myki;
75              
76             my $myki = WWW::Myki->new(
77             username => 'myusername',
78             password => 'mypassw0rd'
79             );
80              
81             # Print card number, card holder, Myki money and Myki pass balances
82            
83             foreach $card ( $myki->cards ) {
84             print "Card number: ". $card->id ." - Holder: ". $card->holder "\n";
85             print "Myki money balance : ". $card->money ." - Myki pass balance: ". $card->pass ."\n";
86             }
87              
88             # Print the date, time, service, description and cost of our last 15 transactions
89              
90             foreach my $trip ( $card->transactions ) {
91             printf( "%10s %8s %-10s %-20s %-5s\n",
92             $trip->date,
93             $trip->time,
94             $trip->service,
95             $trip->desc,
96             $trip->debit )
97             }
98            
99             =head1 DESCRIPTION
100              
101             L provides a simple interface to the Myki online account management portal functionality
102             for registered Myki users.
103              
104             =head1 METHODS
105              
106             =head2 new ( %args )
107              
108             Constructor. Creates a new WWW::Myki object and attempts a login using the provided credentials.
109             On successful login, returns a WWW::Myki object.
110              
111             The constructor accepts an anonymous hash of two mandatory parameters:
112              
113             =over
114              
115             =item *
116              
117             username
118              
119             Your registered Myki account username.
120              
121             =item *
122              
123             password
124              
125             Your registered Myki account password.
126              
127             =back
128              
129             =cut
130              
131             =head2 cards
132              
133             my @cards = $myki->cards;
134              
135             Returns an array of L objects. Each object is representative of a Myki card
136             registered to the specified account.
137              
138             =head1 AUTHOR
139              
140             Luke Poskitt, C<< >>
141              
142             =head1 BUGS
143              
144             Please report any bugs or feature requests to C, or through
145             the web interface at L. I will be notified, and then you'll
146             automatically be notified of progress on your bug as I make changes.
147              
148             =head1 SUPPORT
149              
150             You can find documentation for this module with the perldoc command.
151              
152             perldoc WWW::Myki
153              
154             You can also look for information at:
155              
156             =over 4
157              
158             =item * RT: CPAN's request tracker
159              
160             L
161              
162             =item * AnnoCPAN: Annotated CPAN documentation
163              
164             L
165              
166             =item * CPAN Ratings
167              
168             L
169              
170             =item * Search CPAN
171              
172             L
173              
174             =back
175              
176             =head1 LICENSE AND COPYRIGHT
177              
178             Copyright 2012 Luke Poskitt.
179              
180             This program is free software; you can redistribute it and/or modify it
181             under the terms of either: the GNU General Public License as published
182             by the Free Software Foundation; or the Artistic License.
183              
184             See http://dev.perl.org/licenses/ for more information.
185              
186             =head1 SEE ALSO
187              
188             L, L
189              
190             =cut
191              
192             1;