File Coverage

blib/lib/WWW/Marvel/Client.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


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