summary refs log tree commit diff
path: root/lib/Net
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-10-25 17:23:14 +0200
committerfranck cuny <franck@lumberjaph.net>2010-10-25 17:23:14 +0200
commit6c5dba1f8d4d012cf4a9d1e10fd20094a737b071 (patch)
tree47c8ba1b25126557e3504bd1d600a335ad87ecc0 /lib/Net
parentupdate tests to use the dummy api, and replace api_base_url with base_url (diff)
downloadnet-http-spore-6c5dba1f8d4d012cf4a9d1e10fd20094a737b071.tar.gz
api spec can be modularized:
* add two new methods: new_from_specs and new_from_strings
  theses two methods can receive more than one description to create a client
* add tests and api desc
Diffstat (limited to 'lib/Net')
-rw-r--r--lib/Net/HTTP/Spore.pm108
1 files changed, 81 insertions, 27 deletions
diff --git a/lib/Net/HTTP/Spore.pm b/lib/Net/HTTP/Spore.pm
index 34f4740..03606a0 100644
--- a/lib/Net/HTTP/Spore.pm
+++ b/lib/Net/HTTP/Spore.pm
@@ -8,70 +8,124 @@ use IO::All;
 use JSON;
 use Carp;
 use Try::Tiny;
+use Scalar::Util;
 
 use Net::HTTP::Spore::Core;
 
 our $VERSION = 0.01;
 
+# XXX should we let the possibility to override this super class, or add
+# another superclasses?
+
 sub new_from_string {
     my ($class, $string, %args) = @_;
 
-    my $spec;
+    my $spore_class =
+      Class::MOP::Class->create_anon_class(
+        superclasses => ['Net::HTTP::Spore::Core'] );
 
-    try {
-        $spec = JSON::decode_json($string);
-    }catch{
-        Carp::confess("unable to parse JSON spec: ".$_);
-    };
+    my $spore_object = _attach_spec_to_class($string, \%args, $spore_class);
+
+    return $spore_object;
+}
+
+sub new_from_strings {
+    my $class = shift;
 
-    my ( $spore_class, $spore_object );
-    # XXX should we let the possibility to override this super class, or add
-    # another superclasses?
+    my $opts;
+    if (ref ($_[-1]) eq 'HASH') {
+        $opts = pop @_;
+    }
+    my @strings = @_;
 
-    $spore_class =
+    my $spore_class =
       Class::MOP::Class->create_anon_class(
         superclasses => ['Net::HTTP::Spore::Core'] );
 
+    my $spore_object = undef;
+    foreach my $string (@strings) {
+        $spore_object = _attach_spec_to_class($string, $opts, $spore_class, $spore_object);
+    }
+    return $spore_object;
+}
+
+sub new_from_spec {
+    my ( $class, $spec_file, %args ) = @_;
+
+    Carp::confess("specification file is missing") unless $spec_file;
+
+    my $content = _read_spec($spec_file);
+
+    $class->new_from_string( $content, %args );
+}
+
+sub new_from_specs {
+    my $class = shift;
+
+    my $opts;
+    if (ref ($_[-1]) eq 'HASH') {
+        $opts = pop @_;
+    }
+    my @specs = @_;
+
+    my @strings;
+    foreach my $spec (@specs) {
+        push @strings,_read_spec($spec);
+    }
+
+    $class->new_from_strings(@strings, $opts);
+}
+
+sub _attach_spec_to_class {
+    my ( $string, $opts, $class, $object ) = @_;
+
+    my $spec;
+    try {
+        $spec = JSON::decode_json($string);
+    }
+    catch {
+        Carp::confess( "unable to parse JSON spec: " . $_ );
+    };
+
     try {
         my $base_url;
-        if ( $spec->{base_url} && !$args{base_url} ) {
-            $args{base_url} = $spec->{base_url};
+        if ( $spec->{base_url} && !$opts->{base_url} ) {
+            $opts->{base_url} = $spec->{base_url};
         }
-        elsif ( !$args{base_url} ) {
+        elsif ( !$opts->{base_url} ) {
             die "base_url is missing!";
         }
 
         if ( $spec->{formats} ) {
-            $args{formats} = $spec->{formats};
+            $opts->{formats} = $spec->{formats};
         }
 
         if ( $spec->{authentication} ) {
-            $args{authentication} = $spec->{authentication};
+            $opts->{authentication} = $spec->{authentication};
         }
 
-        $spore_object = $spore_class->new_object(%args);
-        $spore_object = _add_methods( $spore_object, $spec->{methods} );
-
+        if ( !$object ) {
+            $object = $class->new_object(%$opts);
+        }
+        $object = _add_methods( $object, $spec->{methods} );
     }
     catch {
         Carp::confess( "unable to create new Net::HTTP::Spore object: " . $_ );
     };
 
-    return $spore_object;
+    return $object;
 }
 
-sub new_from_spec {
-    my ( $class, $spec_file, %args ) = @_;
-
-    Carp::confess("specification file is missing") unless $spec_file;
+sub _read_spec {
+    my $spec_file = shift;
 
-    my ( $content, $spec );
+    my $content;
 
     if ( $spec_file =~ m!^http(s)?://! ) {
         my $uri = URI->new($spec_file);
-        my $req = HTTP::Request->new(GET => $spec_file);
+        my $req = HTTP::Request->new( GET => $spec_file );
         my $ua  = LWP::UserAgent->new();
-        my $res = $ua->request( $req );
+        my $res = $ua->request($req);
         $content = $res->content;
     }
     else {
@@ -81,7 +135,7 @@ sub new_from_spec {
         $content < io($spec_file);
     }
 
-    $class->new_from_string( $content, %args );
+    return $content;
 }
 
 sub _add_methods {