From afcde73887075e3befd4bf35815ee0f757b093b6 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 16 Jun 2009 21:10:03 -0500 Subject: start making the main interface sane --- lib/WWW/Unfuddle.pm | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/WWW/Unfuddle.pm b/lib/WWW/Unfuddle.pm index 26d3717..93dcb15 100644 --- a/lib/WWW/Unfuddle.pm +++ b/lib/WWW/Unfuddle.pm @@ -3,6 +3,8 @@ use Moose; use LWP::UserAgent; use JSON; +use WWW::Unfuddle::Project; + has username => ( is => 'ro', isa => 'Str', @@ -45,15 +47,27 @@ sub _api_url { return 'http://' . $self->_domain . '/api/v1/' . join('/', @path) . '.json'; } -sub list_projects { +sub request { my $self = shift; - my $request = HTTP::Request->new(GET => $self->_api_url('projects')); - $request->header(Accept => 'application/json'); - my $response = $self->ua->request($request); - my $projects = from_json($response->content); - for my $project (@$projects) { - print $project->{short_name}, "\n"; + my ($method, @path) = @_; + my $req = HTTP::Request->new($method => $self->_api_url(@path)); + $req->header(Accept => 'application/json'); + my $resp = $self->ua->request($req); + if ($resp->code != 200) { + confess sprintf 'Error (%d): %s', $resp->code, $resp->content; } + return from_json($resp->content); +} + +sub get { + my $self = shift; + $self->request(GET => @_); +} + +sub projects { + my $self = shift; + return map { WWW::Unfuddle::Project->new($_) } @{ $self->get('projects') }; + } __PACKAGE__->meta->make_immutable; -- cgit v1.2.3