WWW-Marvel

 view release on metacpan or  search on metacpan

examples/all_characters_names.pl  view on Meta::CPAN

use strict;
use warnings;
use Data::Dumper;
use WWW::Marvel::Client;
use WWW::Marvel::Config::File;
use WWW::Marvel::Response;

my $cfg = WWW::Marvel::Config::File->new();
my $client = WWW::Marvel::Client->new({
	public_key  => $cfg->get_public_key,
	private_key => $cfg->get_private_key,
});

my $total_records = undef;
my $founded = 0;
my $offset = 0;
my $limit = 100;
my $calls = 0;
do {
	my $res_data = $client->characters({ limit => $limit, offset => $offset });
	my $res = WWW::Marvel::Response->new($res_data);

lib/WWW/Marvel.pm  view on Meta::CPAN


=head1 SYNOPSIS

  use WWW::Marvel::Config;
  use WWW::Marvel::Client;

  my $cfg = WWW::Marvel::Config::File->new("my_marvel.conf");

  my $client = WWW::Marvel::Client->new({
    public_key  => $cfg->get_public_key,
	private_key => $cfg->get_private_key
  });

  my $data = $client->characters({ name => 'spider-man' });


=head1 DESCRIPTION

This module is an interface to Marvel Comics API
to "create awesome stuff with the world's greatest comic api".

lib/WWW/Marvel/Client.pm  view on Meta::CPAN

use warnings;
use Carp;
use Digest::MD5;
use JSON;
use LWP::UserAgent;
use URI;

sub new {
	my ($class, $args) = @_;
	my $self = bless {}, $class;
	for my $k (qw/ private_key public_key /) {
		$self->{$k} = $args->{$k} if exists $args->{$k};
	}
	return $self;
}

sub get_private_key { $_[0]->{private_key} }
sub get_public_key  { $_[0]->{public_key} }
sub get_timestamp   { $_[0]->{timestamp} }
sub set_timestamp {
	my ($self, $timestamp) = @_;
	$self->{timestamp} = $timestamp;
	return $self;
}

sub get_endpoint    { "http://gateway.marvel.com" }
sub get_api_version { "v1" }

lib/WWW/Marvel/Client.pm  view on Meta::CPAN

# ref: https://developer.marvel.com/documentation/authorization
sub hash {
	my ($self, $time) = @_;

	if (!defined $time) {
		croak "need a timestamp to create a md5 hash" if !defined $self->get_timestamp;
		$time = $self->get_timestamp;
	}

	my $md5 = Digest::MD5->new;
	$md5->add( $time, $self->get_private_key, $self->get_public_key );
	return $md5->hexdigest;
}

sub _get_json_content {
	my ($self, $http_res) = @_;
	my $content = $self->json->decode( $http_res->decoded_content );
	return $content;
}

sub json { $_[0]->{_json} //= JSON->new()->allow_nonref->relaxed }

lib/WWW/Marvel/Config.pm  view on Meta::CPAN

package WWW::Marvel::Config;
use strict;
use warnings;
use Carp;

# my $cfg = WWW::Marvel::Config->new({
#	private_key => 'a1b2',
#	public_key  => 'c3d4',
# });
# $cfg->get_private_key(); # a1b2
# $cfg->get_public_key();  # c3d4

my @KEYS = (qw/ private_key public_key /);

sub new {
	my ($class, $args) = @_;

	my $self = bless {}, $class;
	$self->_set_keys($args);

	return $self;
}

sub get_private_key { shift->_get_auth_key('private_key') }
sub get_public_key  { shift->_get_auth_key('public_key') }

sub _get_auth_key {
	my ($self, $name) = @_;
	croak "Unknown '$name' key" if !exists $self->{ $name };
	$self->{ $name };
}

sub _set_keys {
	my ($self, $hash) = @_;

scripts/characters.pl  view on Meta::CPAN

	printf "  FILTERS:\n";
	printf "    --%-12s \t Filter on %s [%s]\n", $_, $_, $FILTERS{$_} for (sort keys %FILTERS);
	printf "  FLAGS:\n";
	printf "    --%-12s \t Show %s\n", $_, $_ for (@FLAGS);
}

my $OPT = commandline_options();
my $cfg = WWW::Marvel::Config::File->new($OPT->{config});
my $client = WWW::Marvel::Client->new({
	public_key  => $cfg->get_public_key,
	private_key => $cfg->get_private_key,
});
my %filters =
	map { $_, $OPT->{$_} }
	grep { defined $OPT->{$_} }
	keys %FILTERS;

my $res_data = $client->characters(\%filters);
my $res = WWW::Marvel::Response->new($res_data);

my %FMT = (

t/www-marvel-client.t  view on Meta::CPAN

# -*- perl -*-

use Test::More tests => 7;

BEGIN { use_ok( 'WWW::Marvel::Client' ); }

my $client = WWW::Marvel::Client->new({ public_key => 1234, private_key => 'abcd' });
# write a test for this croaking condition
# $client->hash();

$client->set_timestamp(1431297266);
is($client->get_timestamp, 1431297266, "get timestamp");
is($client->hash, '0d9b0a1ffe216482153a667fb4b68dac', 'md5 hash with ts = 1431297266');
is($client->hash(1), 'ffd275c5130566a2916217b101f26150', 'md5 hash with ts = 1');

my $uri = $client->uri({path => ''});
diag $uri;

t/www-marvel-config-file.conf  view on Meta::CPAN

[auth]
public_key=1234
private_key=abcd

t/www-marvel-config-file.t  view on Meta::CPAN

use Test::Exception;
use Test::More tests => 5;

BEGIN { use_ok( 'WWW::Marvel::Config::File' ); }

my $path = abs_path catfile(dirname(__FILE__), 'www-marvel-config-file.conf');

{
	my $cfg = WWW::Marvel::Config::File->new($path);
	is($cfg->get_public_key, '1234', 'get public key');
	is($cfg->get_private_key, 'abcd', 'get private key');
	is($cfg->get_config_filename, $path, 'get config filename');
}

{
	dies_ok { WWW::Marvel::Config::File->new("fake_path") } "Unvalid config file";
}

t/www-marvel-config.t  view on Meta::CPAN

# -*- perl -*-

use Test::More tests => 5;
use Test::Exception;

BEGIN { use_ok( 'WWW::Marvel::Config' ); }

{
	my $cfg = WWW::Marvel::Config->new({ public_key => 1234, private_key => 'abcd' });
	is($cfg->get_public_key, '1234', 'get public key');
	is($cfg->get_private_key, 'abcd', 'get private key');
}

{
	my $cfg = WWW::Marvel::Config->new();
	dies_ok { $cfg->get_public_key  } "No 'public_key' key set";
	dies_ok { $cfg->get_private_key } "No 'private_key' key set";
}



( run in 0.460 second using v1.01-cache-2.11-cpan-fd5d4e115d8 )