Skip to content

Commit

Permalink
More work on coercions
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Jun 19, 2016
1 parent ef9d8c9 commit 6f2da67
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
2 changes: 1 addition & 1 deletion bin/gigs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ print "\n";

foreach (@$evnts) {
print $_->start->strftime('%A %d %B %Y'), "\n";
print ' ', $_->performances->[0]->artist->displayName;
print ' ', $_->performance->[0]->artist->displayName;
print ' at ', $_->venue->displayName, "\n\n";
}
11 changes: 1 addition & 10 deletions lib/Net/Songkick.pm
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,7 @@ sub get_upcoming_events {
my $resp = $self->_request($url, \%req_args);

if ($self->return_format eq 'perl') {
# my $evnts;

# # my $xp = XML::LibXML->new->parse_string($resp);
# foreach ($xp->findnodes('//event')) {
# push @$evnts, Net::Songkick::Event->new_from_xml($_);
# }
# return wantarray ? @$evnts : $evnts;
# } else {
# return $resp;


my $data = $self->json_decoder->decode($resp);
my $events;
foreach (@{$data->{resultsPage}{results}{event}}) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Net/Songkick/Artist.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ package Net::Songkick::Artist;
use strict;
use warnings;

use Moose::Util::TypeConstraints;
use Moose;

coerce 'Net::Songkick::Artist',
from 'HashRef',
via { Net::Songkick::Artist->new($_) };

has $_ => (
is => 'ro',
isa => 'Str',
Expand Down
29 changes: 27 additions & 2 deletions lib/Net/Songkick/Event.pm
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ has location => (
coerce => 1,
);

has performances => (
has performance => (
is => 'ro',
isa => 'ArrayRef[Net::Songkick::Performance]',
);
Expand All @@ -45,7 +45,32 @@ has venue => (
coerce => 1,
);

use Data::Dumper;
around BUILDARGS => sub {
my $orig = shift;
my $class = shift;

my %args;

if (@_ == 1) {
%args = %{$_[0]};
} else {
%args = @_;
}

if (exists $args{start} and ! $args{start}) {
$args{start} = DateTime->new_from_epoch(epoch => 0);
}

if (exists $args{performance}) {
foreach (@{$args{performance}}) {
if (ref $_ ne 'Net::Songkick::Performance') {
$_ = Net::Songkick::Performance->new($_);
}
}
}

$class->$orig(\%args);
};

=head1 METHODS
Expand Down
6 changes: 6 additions & 0 deletions lib/Net/Songkick/Performance.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ package Net::Songkick::Performance;
use strict;
use warnings;
use Moose;
use Moose::Util::TypeConstraints;

use Net::Songkick::Artist;

coerce 'Net::Songkick::Performance',
from 'HashRef',
via { Net::Songkick::Performance->new($_) };

has $_ => (
is => 'ro',
isa => 'Str',
Expand All @@ -20,6 +25,7 @@ has $_ => (
has artist => (
is => 'ro',
isa => 'Net::Songkick::Artist',
coerce => 1,
);

=head1 METHODS
Expand Down
14 changes: 9 additions & 5 deletions lib/Net/Songkick/Types.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Net::Songkick::Types - USeful type stuff for Net::Songkick
package Net::Songkick::Types;

use Moose::Util::TypeConstraints;

use Data::Dumper;
use DateTime::Format::Strptime;

subtype 'Net::Songkick::DateTime',
Expand All @@ -21,11 +21,15 @@ subtype 'Net::Songkick::DateTime',
coerce 'Net::Songkick::DateTime',
from 'HashRef',
via {
if ($_) {
DateTime::Format::Strptime->new(
pattern => '%Y-%m-%d %H:%M:%S%z',
)->parse_datetime($_);
my $dt = DateTime::Format::Strptime->new(
pattern => '%Y-%m-%dT%H:%M:%S%z',
)->parse_datetime($_->{datetime});
if (!$dt) {
$dt = DateTime::Format::Strptime->new(
pattern => '%Y-%m-%d',
)->parse_datetime($_->{date});
}
return $dt;
};

1;
Expand Down

0 comments on commit 6f2da67

Please sign in to comment.