From 85a634ef2e68115636e8a17becbc6a968cab0eae Mon Sep 17 00:00:00 2001 From: franck cuny Date: Tue, 12 Oct 2010 17:18:25 +0200 Subject: update POD --- lib/Net/HTTP/Spore.pm | 22 ++++++++++++-- lib/Net/HTTP/Spore/Meta.pm | 2 ++ lib/Net/HTTP/Spore/Meta/Method.pm | 29 ++++++++++++++++++ lib/Net/HTTP/Spore/Middleware.pm | 2 ++ lib/Net/HTTP/Spore/Middleware/Auth.pm | 6 ++++ lib/Net/HTTP/Spore/Middleware/Format.pm | 4 +++ lib/Net/HTTP/Spore/Middleware/Format/Auto.pm | 44 ++++++++++++++++++++++++---- lib/Net/HTTP/Spore/Response.pm | 1 + 8 files changed, 102 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/Net/HTTP/Spore.pm b/lib/Net/HTTP/Spore.pm index b510fa0..23d5bc5 100644 --- a/lib/Net/HTTP/Spore.pm +++ b/lib/Net/HTTP/Spore.pm @@ -100,20 +100,36 @@ sub _add_methods { my $client = Net::HTTP::Spore->new_from_spec('twitter.json'); - $client->enable('Auth::OAuth'); $client->enable('Format::JSON'); + $client->enable('Auth::OAuth'); my $timeline = $client->public_timeline(format => 'json'); my $tweets = $timeline->body; + foreach my $tweet (@$tweets) { - print $tweet->{user}->{screen_name}. " says ".$tweet->{text}."\n"; - } + print $tweet->{user}->{screen_name}. " says ".$tweet->{text}."\n"; } my $friends_timeline = $client->friends_timeline(format => 'json'); =head1 DESCRIPTION +This module is an implementation of the SPORE specification. To use this client, you need to use or to write a SPORE specification of an API. Some specifications are available L. + +=head2 CLIENT CREATION + +First you need to create a client. This can be done using two methods, B and B. The client will read the specification file to create a appropriate methods to interact with the API. + +=head2 MIDDLEWARES + +It's possible to activate some middlewares to extend the usage of the client. If you're using an API that discuss in JSON, you can enable the middleware L. + + $client->enable('Format::JSON'); + +or only on some path + + $client->enable_if(sub{$_->[0]->path =~ m!/path/to/json/stuff!}, 'Format::JSON'); + =head2 METHODS =over 4 diff --git a/lib/Net/HTTP/Spore/Meta.pm b/lib/Net/HTTP/Spore/Meta.pm index 4cbd05e..ec773b6 100644 --- a/lib/Net/HTTP/Spore/Meta.pm +++ b/lib/Net/HTTP/Spore/Meta.pm @@ -1,5 +1,7 @@ package Net::HTTP::Spore::Meta; +# ABSTRACT: Meta class for all SPORE object + use Moose; use Moose::Exporter; use Moose::Util::MetaRole; diff --git a/lib/Net/HTTP/Spore/Meta/Method.pm b/lib/Net/HTTP/Spore/Meta/Method.pm index 3494350..3567497 100644 --- a/lib/Net/HTTP/Spore/Meta/Method.pm +++ b/lib/Net/HTTP/Spore/Meta/Method.pm @@ -163,5 +163,34 @@ sub wrap { =head1 SYNOPSIS + my $spore_method = Net::HTTP::Spore::Meta::Method->wrap( + 'user_timeline', + method => 'GET', + path => '/user/:name' + ); + =head1 DESCRIPTION +=head1 METHODS + +=over 4 + +=item B + +=item B + +=item B + +=item B + +=item B + +=item B + +=item B + +=item B + +=item B + +=back diff --git a/lib/Net/HTTP/Spore/Middleware.pm b/lib/Net/HTTP/Spore/Middleware.pm index 6d284b5..2855a72 100644 --- a/lib/Net/HTTP/Spore/Middleware.pm +++ b/lib/Net/HTTP/Spore/Middleware.pm @@ -1,5 +1,7 @@ package Net::HTTP::Spore::Middleware; +# ABSTRACT: middlewares base class + use strict; use warnings; diff --git a/lib/Net/HTTP/Spore/Middleware/Auth.pm b/lib/Net/HTTP/Spore/Middleware/Auth.pm index 619215c..0d422a5 100644 --- a/lib/Net/HTTP/Spore/Middleware/Auth.pm +++ b/lib/Net/HTTP/Spore/Middleware/Auth.pm @@ -1,5 +1,7 @@ package Net::HTTP::Spore::Middleware::Auth; +# ABSTRACT: base class for Authentication middlewares + use Moose; extends 'Net::HTTP::Spore::Middleware'; @@ -8,3 +10,7 @@ sub should_authenticate { $_[1]->env->{'spore.authentication'} } sub call { die "should be implemented" } 1; + +=head1 DESCRIPTION + +Authentication middleware should extends this base class and implement the B method diff --git a/lib/Net/HTTP/Spore/Middleware/Format.pm b/lib/Net/HTTP/Spore/Middleware/Format.pm index 559c1e5..2741295 100644 --- a/lib/Net/HTTP/Spore/Middleware/Format.pm +++ b/lib/Net/HTTP/Spore/Middleware/Format.pm @@ -83,6 +83,8 @@ If the environment contains a B (under the name 'spore.payload'), it sh =head1 METHODS +=over 4 + =item serializer_key name of the extension serializer should check to be sure to not encode a payload already encoded, or set the headers that have already been defined @@ -116,3 +118,5 @@ this method returns 1 if serialization have not already been done this method returns 1 if deserialization have not already been done =item call + +=back diff --git a/lib/Net/HTTP/Spore/Middleware/Format/Auto.pm b/lib/Net/HTTP/Spore/Middleware/Format/Auto.pm index fd66b8c..0bc1eb0 100644 --- a/lib/Net/HTTP/Spore/Middleware/Format/Auto.pm +++ b/lib/Net/HTTP/Spore/Middleware/Format/Auto.pm @@ -1,17 +1,51 @@ package Net::HTTP::Spore::Middleware::Format::Auto; use Moose; +use MooseX::Types::Moose qw/HashRef Object/; extends 'Net::HTTP::Spore::Middleware::Format'; +use Try::Tiny; + +has seriliazer => ( + is => 'rw', + isa => HashRef [Object], + lazy => 1, + default => sub { {} }, +); + sub call { my ( $self, $req ) = @_; - $req->env->{'sporex.format'} = 1; + my $formats = $req->env->{'spore.format'}; + + foreach my $format (@$formats) { + my $cls = "Net::HTTP::Spore::Middleware::Format::" . $format; + if ( Class::MOP::load($cls) ) { + my $s = $cls->new; + $self->serializer->{$format} = $s; + try { + if ( $req->env->{'spore.payload'} ) { + $req->env->{'spore.payload'} = + $s->encode( $req->env->{'spore.payload'} ); + $req->header( $s->content_type ); + } + $req->header( $s->accept_type ); + $req->env->{$self->serializer_key} = 1; + }; + last if $req->env->{$self->serializer_key} == 1; + } + } - return $self->response_cb( sub { - my $res = shift; - return $res; - }); + return $self->response_cb( + sub { + my $res = shift; + return $res; + } + ); } 1; + +=head1 DESCRIPTION + +B diff --git a/lib/Net/HTTP/Spore/Response.pm b/lib/Net/HTTP/Spore/Response.pm index 4667e08..03ab2e7 100644 --- a/lib/Net/HTTP/Spore/Response.pm +++ b/lib/Net/HTTP/Spore/Response.pm @@ -229,3 +229,4 @@ The third and final element is the body =back +=back -- cgit v1.2.3