=================================================================== RCS file: /cvs/HOPE/Net-OpenAMD/lib/Net/OpenAMD.pm,v retrieving revision 1.7 retrieving revision 1.17 diff -u -r1.7 -r1.17 --- HOPE/Net-OpenAMD/lib/Net/OpenAMD.pm 2010/06/27 04:11:22 1.7 +++ HOPE/Net-OpenAMD/lib/Net/OpenAMD.pm 2010/07/13 01:13:22 1.17 @@ -1,38 +1,51 @@ package Net::OpenAMD; -# $AFresh1: OpenAMD.pm,v 1.6 2010/06/27 00:52:33 andrew Exp $ +# $AFresh1: OpenAMD.pm,v 1.16 2010/07/09 22:04:21 andrew Exp $ use warnings; use strict; use Carp; -use version; our $VERSION = qv('0.0.1'); +use version; our $VERSION = qv('0.0.4'); +my $BASE_URI = 'https://api.hope.net/api/'; -my $BASE_URL = 'https://api.hope.net/api/'; +use Scalar::Util qw( refaddr ); +*_ident = \&refaddr; -use Class::Std::Utils; - use LWP::UserAgent; use URI; use Net::OAuth; -use JSON::Any; +use JSON; { - my @attr_refs = \( my %base_url_of, my %ua_of, my %auth_of, ); + my @attr_refs = \( + my %base_uri_of, + my %ua_of, my %auth_of, my %actions_of, + my %json_of, + ); sub new { my ( $class, $options ) = @_; - my $self = bless anon_scalar(), $class; - my $ident = ident($self); + 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_url_of{$ident} = $options->{base_url} || $BASE_URL; + $base_uri_of{$ident} = $options->{base_uri} || $BASE_URI; $ua_of{$ident} = $options->{ua} || LWP::UserAgent->new(); + $json_of{$ident} = $options->{json} || JSON->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; @@ -40,30 +53,28 @@ sub get { my ( $self, $action, $query ) = @_; - my $ident = ident($self); + my $ident = _ident($self); - my $uri = URI->new( $base_url_of{$ident} . '/' . $action ); - $uri->query($query); + my $uri = URI->new_abs( $action . '/', $base_uri_of{$ident} ); + $uri->query_form($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; + eval { + $data = $json_of{$ident}->decode( $response->decoded_content ); + }; + croak "Invalid JSON from [$uri]" if $@; - return JSON::Any->jsonToObj( $response->decoded_content ); + return $data; } - sub location { my $self = shift; return $self->get( 'location', @_ ) } - sub speakers { my $self = shift; return $self->get( 'speakers', @_ ) } - sub talks { my $self = shift; return $self->get( 'talks', @_ ) } - sub interests { my $self = shift; return $self->get( 'interests', @_ ) } - sub users { my $self = shift; return $self->get( 'users', @_ ) } sub stats { croak 'Unused feature' } sub DESTROY { my ($self) = @_; - my $ident = ident $self; + my $ident = _ident $self; foreach my $attr_ref (@attr_refs) { delete $attr_ref->{$ident}; @@ -76,6 +87,7 @@ 1; # Magic true value required at end of module __END__ + =head1 NAME Net::OpenAMD - Perl interface to the OpenAMD API @@ -83,7 +95,7 @@ =head1 VERSION -This document describes Net::OpenAMD version 0.0.1 +This document describes Net::OpenAMD version 0.0.4 =head1 SYNOPSIS @@ -122,10 +134,13 @@ =over -=item base_url +=item base_uri -A URL to the API, currently defaults to https://api.hope.net/api +A URL to the API, currently defaults to https://api.hope.net/api/ +Most likely it should end with a / to make URI happy, so notice that if you +are having 404 errors you don't expect. + =item ua Should be a pre-configured LWP::UserAgent or similar that returns a @@ -165,13 +180,13 @@ =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. =head1 DIAGNOSTICS -All methods should croak when an error occours. +All methods should croak when an error occurs. If the remote API returns a successful response that contains valid JSON, that will be decoded and returned.