=================================================================== RCS file: /cvs/HOPE/Net-OpenAMD/lib/Net/OpenAMD.pm,v retrieving revision 1.16 retrieving revision 1.20 diff -u -r1.16 -r1.20 --- HOPE/Net-OpenAMD/lib/Net/OpenAMD.pm 2010/07/09 23:04:21 1.16 +++ HOPE/Net-OpenAMD/lib/Net/OpenAMD.pm 2010/07/17 12:48:28 1.20 @@ -1,12 +1,12 @@ package Net::OpenAMD; -# $AFresh1: OpenAMD.pm,v 1.15 2010/06/30 19:02:48 andrew Exp $ +# $AFresh1: OpenAMD.pm,v 1.19 2010/07/14 05:21:23 andrew Exp $ use warnings; use strict; use Carp; -use version; our $VERSION = qv('0.0.3'); +use version; our $VERSION = qv('0.1.1'); my $BASE_URI = 'https://api.hope.net/api/'; use Scalar::Util qw( refaddr ); @@ -15,12 +15,15 @@ use LWP::UserAgent; use URI; use Net::OAuth; -use JSON qw/ from_json /; +use JSON; { - my @attr_refs - = \( my %base_uri_of, my %ua_of, my %auth_of, my %actions_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 ) = @_; @@ -33,6 +36,7 @@ $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 )]; @@ -51,16 +55,19 @@ my ( $self, $action, $query ) = @_; my $ident = _ident($self); - my $uri = URI->new_abs( $action, $base_uri_of{$ident} ); - $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; - my @data = map { from_json($_) } split /\r?\n/xms, - $response->decoded_content; + my $data; + eval { + $data = $json_of{$ident}->decode( $response->decoded_content ); + }; + croak "Invalid JSON from [$uri]" if $@; - return @data == 1 ? $data[0] : \@data; + return $data; } sub stats { croak 'Unused feature' } @@ -88,7 +95,7 @@ =head1 VERSION -This document describes Net::OpenAMD version 0.0.1 +This document describes Net::OpenAMD version 0.0.3 =head1 SYNOPSIS