| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::Marvel::Client; | 
| 2 | 2 |  |  | 2 |  | 19220 | use base qw/ Class::Accessor /; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 1397 |  | 
| 3 | 2 |  |  | 2 |  | 3742 | use Carp; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 168 |  | 
| 4 | 2 |  |  | 2 |  | 13 | use Digest::MD5; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 73 |  | 
| 5 | 2 |  |  | 2 |  | 712 | use JSON; | 
|  | 2 |  |  |  |  | 16982 |  | 
|  | 2 |  |  |  |  | 15 |  | 
| 6 | 2 |  |  | 2 |  | 2140 | use LWP::UserAgent; | 
|  | 2 |  |  |  |  | 102802 |  | 
|  | 2 |  |  |  |  | 99 |  | 
| 7 | 2 |  |  | 2 |  | 27 | use URI; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 1464 |  | 
| 8 |  |  |  |  |  |  | __PACKAGE__->follow_best_practice; | 
| 9 |  |  |  |  |  |  | __PACKAGE__->mk_accessors(qw/ public_key private_key timestamp /); | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 1 |  |  | 1 | 0 | 11 | sub get_endpoint    { "http://gateway.marvel.com" } | 
| 12 | 1 |  |  | 1 | 0 | 12 | sub get_api_version { "v1" } | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | sub characters { | 
| 15 | 0 |  |  | 0 | 0 | 0 | my ($self, $params) = @_; | 
| 16 | 0 |  |  |  |  | 0 | my %p = %$params; | 
| 17 | 0 |  |  |  |  | 0 | my $uri = $self->uri({ path => 'characters', params => \%p }); | 
| 18 | 0 |  |  |  |  | 0 | my $http_res = $self->_get_http_res($uri); | 
| 19 | 0 |  |  |  |  | 0 | return $self->_get_json_content($http_res); | 
| 20 |  |  |  |  |  |  | } | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | sub _get_http_res { | 
| 23 | 0 |  |  | 0 |  | 0 | my ($self, $uri) = @_; | 
| 24 | 0 |  |  |  |  | 0 | my $http_res = $self->ua->get($uri); | 
| 25 | 0 | 0 |  |  |  | 0 | croak $http_res->status_line if !$http_res->is_success; | 
| 26 | 0 |  |  |  |  | 0 | return $http_res; | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | sub _get_json_content { | 
| 30 | 0 |  |  | 0 |  | 0 | my ($self, $http_res) = @_; | 
| 31 | 0 |  |  |  |  | 0 | my $json = JSON->new(); | 
| 32 | 0 |  |  |  |  | 0 | my $content = $json->allow_nonref->relaxed->decode( $http_res->decoded_content ); | 
| 33 | 0 |  |  |  |  | 0 | return $content; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | # ref: https://developer.marvel.com/documentation/authorization | 
| 37 |  |  |  |  |  |  | sub hash { | 
| 38 | 3 |  |  | 3 | 0 | 1108 | my ($self, $time) = @_; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 3 | 100 |  |  |  | 11 | if (!defined $time) { | 
| 41 | 1 | 50 |  |  |  | 4 | croak "need a timestamp to create a md5 hash" if !defined $self->get_timestamp; | 
| 42 | 1 |  |  |  |  | 11 | $time = $self->get_timestamp; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 3 |  |  |  |  | 21 | my $md5 = Digest::MD5->new; | 
| 46 | 3 |  |  |  |  | 8 | $md5->add( $time, $self->get_private_key, $self->get_public_key ); | 
| 47 | 3 |  |  |  |  | 63 | return $md5->hexdigest; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 0 |  |  | 0 | 0 | 0 | sub ua { LWP::UserAgent->new() } | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | sub uri { | 
| 53 | 1 |  |  | 1 | 0 | 4 | my ($self, $args) = @_; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 1 |  | 33 |  |  | 4 | my $time = $self->get_timestamp || time; | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 1 |  |  |  |  | 13 | my @paths; | 
| 58 | 1 | 50 | 33 |  |  | 16 | if (exists $args->{path} && ref $args->{path} eq 'ARRAY') { | 
|  |  | 50 | 33 |  |  |  |  | 
| 59 | 0 |  |  |  |  | 0 | @paths = @{ $args->{path} }; | 
|  | 0 |  |  |  |  | 0 |  | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  | elsif (exists $args->{path} && ref $args->{path} eq '') { | 
| 62 | 1 |  |  |  |  | 3 | push(@paths, $args->{path}); | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 1 |  |  |  |  | 2 | my %params; | 
| 66 | 1 | 50 | 33 |  |  | 4 | if (exists $args->{params} && ref $args->{params} eq 'HASH') { | 
| 67 | 0 |  |  |  |  | 0 | %params = %{ $args->{params} }; | 
|  | 0 |  |  |  |  | 0 |  | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 1 |  |  |  |  | 6 | my %query_params = ( | 
| 71 |  |  |  |  |  |  | %params, | 
| 72 |  |  |  |  |  |  | ts     => $time, | 
| 73 |  |  |  |  |  |  | hash   => $self->hash($time), | 
| 74 |  |  |  |  |  |  | apikey => $self->get_public_key, | 
| 75 |  |  |  |  |  |  | ); | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 1 |  |  |  |  | 12 | my $uri = URI->new( $self->get_endpoint ); | 
| 78 | 1 |  |  |  |  | 9096 | $uri->path( $self->uri_path(@paths) ); | 
| 79 | 1 |  |  |  |  | 176 | $uri->query_form(%query_params); | 
| 80 | 1 |  |  |  |  | 163 | return $uri; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | sub uri_path { | 
| 84 | 1 |  |  | 1 | 0 | 4 | my ($self, @resources) = @_; | 
| 85 | 1 | 50 |  |  |  | 6 | croak "need a resource to query" if @resources < 1; | 
| 86 | 1 |  |  |  |  | 5 | return join('/', $self->get_api_version, 'public', @resources ); | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | 1; |