=================================================================== RCS file: /cvs/HOPE/Net-OpenAMD/lib/Net/OpenAMD.pm,v retrieving revision 1.11 retrieving revision 1.15 diff -u -r1.11 -r1.15 --- HOPE/Net-OpenAMD/lib/Net/OpenAMD.pm 2010/06/28 18:49:31 1.11 +++ HOPE/Net-OpenAMD/lib/Net/OpenAMD.pm 2010/06/30 20:02:48 1.15 @@ -1,13 +1,12 @@ package Net::OpenAMD; -# $AFresh1: OpenAMD.pm,v 1.10 2010/06/27 03:59:32 andrew Exp $ +# $AFresh1: OpenAMD.pm,v 1.14 2010/06/30 18:45:42 andrew Exp $ use warnings; use strict; use Carp; -use version; our $VERSION = qv('0.0.1'); - +use version; our $VERSION = qv('0.0.3'); my $BASE_URI = 'https://api.hope.net/api/'; use Scalar::Util qw( refaddr ); @@ -16,24 +15,33 @@ use LWP::UserAgent; use URI; use Net::OAuth; -use JSON::Any; +use JSON qw/ from_json /; { - my @attr_refs = \( my %base_uri_of, my %ua_of, my %auth_of, ); + my @attr_refs + = \( my %base_uri_of, my %ua_of, my %auth_of, my %actions_of ); sub new { my ( $class, $options ) = @_; my $self = bless do { \my $x }, $class; my $ident = _ident($self); - $options //= {}; + $options ||= {}; croak 'Options should be a hashref' if ref $options ne 'HASH'; $base_uri_of{$ident} = $options->{base_uri} || $BASE_URI; $ua_of{$ident} = $options->{ua} || LWP::UserAgent->new(); + $actions_of{$ident} = $options->{actions} + || [qw( location speakers talks interests users )]; + foreach my $action ( @{ $actions_of{$ident} } ) { + ## no critic + no strict 'refs'; + *{$action} = sub { shift->get( $action, @_ ) }; + } + # XXX Authenticate return $self; @@ -47,20 +55,15 @@ $uri->query($query); my $response = $ua_of{$ident}->get($uri); + croak $response->status_line if !$response->is_success; - if ( !$response->is_success ) { - croak $response->status_line; - } + my @data = map { from_json($_) } split /\r?\n/xms, + $response->decoded_content; - return JSON::Any->jsonToObj( $response->decoded_content ); + return @data == 1 ? $data[0] : \@data; } - sub location { return shift->get( 'location', @_ ) } - sub speakers { return shift->get( 'speakers', @_ ) } - sub talks { return shift->get( 'talks', @_ ) } - sub interests { return shift->get( 'interests', @_ ) } - sub users { return shift->get( 'users', @_ ) } - sub stats { croak 'Unused feature' } + sub stats { croak 'Unused feature' } sub DESTROY { my ($self) = @_; @@ -77,6 +80,7 @@ 1; # Magic true value required at end of module __END__ + =head1 NAME Net::OpenAMD - Perl interface to the OpenAMD API @@ -169,7 +173,7 @@ =back -Unless specified, there is nothing different about any of the helper methods +Unless specified, there is nothing different about any of the action methods than just calling get($action) instead. Depending on API changes, this may not always be the case.