Skip to content

Commit

Permalink
Add /credit to API, improve trending example, refactor _make_url
Browse files Browse the repository at this point in the history
  • Loading branch information
leto committed Sep 2, 2009
1 parent fb828c4 commit b98186b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
12 changes: 11 additions & 1 deletion examples/trending.pl
Expand Up @@ -4,6 +4,16 @@
use Net::Topsy;
use Data::Dumper;

=head1 SYNOPSIS
TOPSY_API_KEY=somekey perl -Ilib examples/trending.pl
=cut

my $topsy = Net::Topsy->new( beta_key => $ENV{TOPSY_API_KEY} );
my $search = $topsy->trending;
warn Dumper [ $search ];
my $topics = $search->{response}{list};

for my $topic (@$topics) {
printf "%-20s\t:\t%s\n", $topic->{term} , $topic->{url};
}
22 changes: 15 additions & 7 deletions lib/Net/Topsy.pm
Expand Up @@ -87,6 +87,10 @@ has API => ( isa => 'HashRef', is => 'ro', default => sub {
perpage => 0,
},
},
'/credit' => {
args => {
},
},
'/trackbacks' => {
args => {
url => 1,
Expand Down Expand Up @@ -170,15 +174,20 @@ sub _validate_params {

sub _make_url {
my ($self,$params,$route) = @_;
$route = $self->base_url . $route . $self->format;
my $url = $route ."?beta=" . $self->beta_key;
while( my ($k,$v) = each %$params) {
$url .= "&$k=" . uri_escape($v) . "&" if defined $v;
$route = $self->base_url . $route . $self->format;
my $url = $route . "?beta=" . $self->beta_key;
while (my ($k,$v) = each %$params) {
$url .= "&$k=". uri_escape($v) if defined $v;
}
#warn "requesting $url";
return $url;
}

sub credit {
my ($self, $params) = @_;
return $self->_topsy_api($params, '/credit');
}

sub stats {
my ($self, $params) = @_;
return $self->_topsy_api($params, '/stats');
Expand Down Expand Up @@ -222,10 +231,9 @@ sub related {
sub _handle_response {
my ($self, $response ) = @_;
if ($response->is_success) {
my $obj = $self->_from_json( $response->content );
return $obj;
return $self->_from_json( $response->content );
} else {
die $response->status_line;
croak $response->status_line;
}
}

Expand Down
3 changes: 2 additions & 1 deletion t/01_basic.t
Expand Up @@ -9,7 +9,7 @@ use Mock::LWP::UserAgent;

use Net::Topsy;

plan tests => 12;
plan tests => 13;

my $nt = Net::Topsy->new( beta_key => 'foo' );
isa_ok $nt, 'Net::Topsy';
Expand All @@ -29,4 +29,5 @@ for my $method (@api_url_methods) {
ok($result, "got a result from $method" );
}

ok( $nt->credit, "got a result from credit" );
1;
1 change: 0 additions & 1 deletion t/lib/Mock/LWP/UserAgent.pm
Expand Up @@ -101,7 +101,6 @@ sub _topsy_rest_api {

sub _validate_basic_url {
my ($self, $url) = @_;

my $uri = URI->new($url);

die "scheme: expected http\n" unless $uri->scheme eq 'http';
Expand Down

0 comments on commit b98186b

Please sign in to comment.