version 1.10, 2010/06/27 04:59:32 |
version 1.14, 2010/06/30 19:45:42 |
|
|
package Net::OpenAMD; |
package Net::OpenAMD; |
|
|
# $AFresh1: OpenAMD.pm,v 1.9 2010/06/27 03:45:07 andrew Exp $ |
# $AFresh1: OpenAMD.pm,v 1.13 2010/06/30 05:30:17 andrew Exp $ |
|
|
use warnings; |
use warnings; |
use strict; |
use strict; |
use Carp; |
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/'; |
my $BASE_URI = 'https://api.hope.net/api/'; |
|
|
use Scalar::Util qw( refaddr ); |
use Scalar::Util qw( refaddr ); |
|
|
use LWP::UserAgent; |
use LWP::UserAgent; |
use URI; |
use URI; |
use Net::OAuth; |
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 %helpers_of ); |
|
|
sub new { |
sub new { |
my ( $class, $options ) = @_; |
my ( $class, $options ) = @_; |
my $self = bless do { \my $x }, $class; |
my $self = bless do { \my $x }, $class; |
my $ident = _ident($self); |
my $ident = _ident($self); |
|
|
$options //= {}; |
$options ||= {}; |
|
|
croak 'Options should be a hashref' if ref $options ne 'HASH'; |
croak 'Options should be a hashref' if ref $options ne 'HASH'; |
|
|
$base_uri_of{$ident} = $options->{base_uri} || $BASE_URI; |
$base_uri_of{$ident} = $options->{base_uri} || $BASE_URI; |
$ua_of{$ident} = $options->{ua} || LWP::UserAgent->new(); |
$ua_of{$ident} = $options->{ua} || LWP::UserAgent->new(); |
|
$helpers_of{$ident} = $options->{helpers} |
|
|| [qw( location speakers talks interests users )]; |
|
|
|
foreach my $helper ( @{ $helpers_of{$ident} } ) { |
|
## no critic |
|
no strict 'refs'; |
|
*{$helper} = sub { shift->get( $helper, @_ ) }; |
|
} |
|
|
# XXX Authenticate |
# XXX Authenticate |
|
|
return $self; |
return $self; |
|
|
$uri->query($query); |
$uri->query($query); |
|
|
my $response = $ua_of{$ident}->get($uri); |
my $response = $ua_of{$ident}->get($uri); |
|
croak $response->status_line if !$response->is_success; |
|
|
if ( !$response->is_success ) { |
my @data = map { from_json($_) } split /\r?\n/xms, |
croak $response->status_line; |
$response->decoded_content; |
} |
|
|
|
return JSON::Any->jsonToObj( $response->decoded_content ); |
return @data == 1 ? $data[0] : \@data; |
} |
} |
|
|
sub location { return shift->get( 'location', @_ ) } |
sub stats { croak 'Unused feature' } |
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 DESTROY { |
sub DESTROY { |
my ($self) = @_; |
my ($self) = @_; |
|
|
|
|
=head1 DIAGNOSTICS |
=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 |
If the remote API returns a successful response that contains valid JSON, that |
will be decoded and returned. |
will be decoded and returned. |
|
|