| 1 | package Net::ZooTool; | ||
| 2 | # ABSTRACT: a Moose interface to the Zootool API | ||
| 3 | 6 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | with 'Net::ZooTool::Utils'; | ||
| 5 | |||
| 6 | 6 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 0 | ▶ | use Net::ZooTool::Auth; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use Net::ZooTool::User; | ||
| 10 | use Net::ZooTool::Item; | ||
| 11 | |||
| 12 | use namespace::autoclean; | ||
| 13 | |||
| 14 | our $VERSION = '0.003'; | ||
| 15 | |||
| 16 | has auth => ( | ||
| 17 | isa => 'Net::ZooTool::Auth', | ||
| 18 | is => 'ro', | ||
| 19 | ); | ||
| 20 | |||
| 21 | around BUILDARGS => sub { | ||
| 22 | my $orig = shift; | ||
| 23 | my $class = shift; | ||
| 24 | |||
| 25 | # Transform normal params to hashref | ||
| 26 | if ( !ref $_[0] ) { | ||
| 27 | if ( scalar @_ == 1 ) { | ||
| 28 | return $class->$orig( apikey => $_[0] ); | ||
| 29 | } | ||
| 30 | elsif ( scalar @_ == 3 ) { | ||
| 31 | return $class->$orig( apikey => $_[0], user => $_[1], password => $_[2] ); | ||
| 32 | } | ||
| 33 | else { | ||
| 34 | croak "Unaccepted params"; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | # Hashref checkings | ||
| 39 | if ( ref $_[0] and !$_[0]->{apikey} ) { | ||
| 40 | croak "You have to provide at least the apikey as either parameter or hashref"; | ||
| 41 | } | ||
| 42 | |||
| 43 | # You need to provide username and password | ||
| 44 | if ( defined $_[0]->{user} and !defined $_[0]->{password} ) { | ||
| 45 | croak "If you provide user you also need to provide password"; | ||
| 46 | } | ||
| 47 | |||
| 48 | if ( defined $_[0]->{password} and !defined $_[0]->{user} ) { | ||
| 49 | croak "If you provide password you also need to provide username"; | ||
| 50 | } | ||
| 51 | |||
| 52 | # If you have reached here everything is good | ||
| 53 | |||
| 54 | return $class->$orig(@_); | ||
| 55 | }; | ||
| 56 | |||
| 57 | sub BUILD { | ||
| 58 | my $self = shift; | ||
| 59 | my $args = shift; | ||
| 60 | |||
| 61 | $self->{auth} = Net::ZooTool::Auth->new( | ||
| 62 | { | ||
| 63 | apikey => $args->{apikey}, | ||
| 64 | user => $args->{user}, | ||
| 65 | password => $args->{password}, | ||
| 66 | } | ||
| 67 | ); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub user { | ||
| 71 | my $self = shift; | ||
| 72 | return Net::ZooTool::User->new({ auth => $self->auth }); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub item { | ||
| 76 | my $self = shift; | ||
| 77 | return Net::ZooTool::Item->new({ auth => $self->auth }); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub add { | ||
| 81 | my ( $self, $args ) = @_; | ||
| 82 | |||
| 83 | $args->{apikey} = $self->auth->apikey; | ||
| 84 | |||
| 85 | my $data = _fetch('/add/' . _hash_to_query_string($args), $self->auth); | ||
| 86 | return $data; | ||
| 87 | } | ||
| 88 | |||
| 89 | no Moose; | ||
| 90 | __PACKAGE__->meta->make_immutable; | ||
| 91 | |||
| 92 | 1; | ||
| 93 | |||
| 94 | __END__ | ||