summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-10-08 12:01:30 +0200
committerfranck cuny <franck@lumberjaph.net>2010-10-08 12:01:30 +0200
commit37848e36b991ed5e7dcff33d84b4f73694eddf5e (patch)
treed160c6a0a143e53475efca5935987bb14435aa64
parentupdate API; add POD (diff)
downloadnet-http-spore-37848e36b991ed5e7dcff33d84b4f73694eddf5e.tar.gz
add new_from_string and some tests
-rw-r--r--lib/Net/HTTP/Spore.pm54
-rw-r--r--t/spore/01_new_from_string.t50
2 files changed, 88 insertions, 16 deletions
diff --git a/lib/Net/HTTP/Spore.pm b/lib/Net/HTTP/Spore.pm
index 4ee6ac3..b510fa0 100644
--- a/lib/Net/HTTP/Spore.pm
+++ b/lib/Net/HTTP/Spore.pm
@@ -13,28 +13,21 @@ use Net::HTTP::Spore::Core;
 
 our $VERSION = 0.01;
 
-sub new_from_spec {
-    my ( $class, $spec_file, %args ) = @_;
-
-    unless ( -f $spec_file ) {
-        Carp::confess("$spec_file does not exists");
-    }
-
-    my ( $content, $spec );
+sub new_from_string {
+    my ($class, $string, %args) = @_;
 
-    $content < io($spec_file);
+    my $spec;
 
     try {
-        $spec = JSON::decode_json($content);
-    }
-    catch {
-        Carp::confess( "unable to parse JSON spec: " . $_ );
+        $spec = JSON::decode_json($string);
+    }catch{
+        Carp::confess("unable to parse JSON spec: ".$_);
     };
 
     my ( $spore_class, $spore_object );
-
     # XXX should we let the possibility to override this super class, or add
     # another superclasses?
+
     $spore_class =
       Class::MOP::Class->create_anon_class(
         superclasses => ['Net::HTTP::Spore::Core'] );
@@ -67,6 +60,29 @@ sub new_from_spec {
     return $spore_object;
 }
 
+sub new_from_spec {
+    my ( $class, $spec_file, %args ) = @_;
+
+    Carp::confess("specification file is missing") unless $spec_file;
+
+    my ( $content, $spec );
+
+    if ( $spec_file =~ m!^http(s)?://! ) {
+        my $uri     = URI->new($spec_file);
+        my $req = HTTP::Request->new(GET => $spec_file);
+        my $ua  = LWP::UserAgent->new();
+        my $res = $ua->request( $req );
+        $content = $res->content;
+    }
+    else {
+        unless ( -f $spec_file ) {
+            Carp::confess("$spec_file does not exists");
+        }
+        $content < io($spec_file);
+    }
+
+    $class->new_from_string( $content, %args );
+}
 
 sub _add_methods {
     my ($class, $methods_spec) = @_;
@@ -102,9 +118,15 @@ sub _add_methods {
 
 =over 4
 
-=item new_from_spec($specification_file, %args
+=item new_from_spec($specification_file, %args)
+
+Create and return a L<Net::HTTP::Spore::Core> object, with methods
+generated from the specification file. The specification file can
+either be a file on disk or a remote URL.
+
+=item new_from_string($specification_string, %args)
 
 Create and return a L<Net::HTTP::Spore::Core> object, with methods
-generated from the specification file.
+generated from the specification string.
 
 =back
diff --git a/t/spore/01_new_from_string.t b/t/spore/01_new_from_string.t
new file mode 100644
index 0000000..34bfedf
--- /dev/null
+++ b/t/spore/01_new_from_string.t
@@ -0,0 +1,50 @@
+use strict;
+use warnings;
+use Test::More;
+use Test::Exception;
+
+plan tests => 14;
+
+use IO::All;
+use Net::HTTP::Spore;
+
+my $couchdb_spec = 't/specs/couchdb.json';
+my %args = ( api_base_url => 'http://localhost:5984', );
+my $content < io($couchdb_spec);
+
+dies_ok { Net::HTTP::Spore->new_from_spec };
+like $@, qr/specification file is missing/;
+
+dies_ok { Net::HTTP::Spore->new_from_spec( "/foo/bar/baz", ) };
+like $@, qr/does not exists/;
+
+dies_ok { Net::HTTP::Spore->new_from_spec( $couchdb_spec, ) };
+like $@, qr/api_base_url is missing/;
+
+ok my $client = Net::HTTP::Spore->new_from_spec( $couchdb_spec, %args );
+ok $client = Net::HTTP::Spore->new_from_string( $content, %args );
+
+SKIP: {
+    skip "require RUN_HTTP_TEST", 1 unless $ENV{RUN_HTTP_TEST};
+    ok $client = Net::HTTP::Spore->new_from_spec(
+        'http://github.com/franckcuny/spore/raw/master/services/github.json',
+        %args );
+}
+
+dies_ok {
+    Net::HTTP::Spore->new_from_string(
+'{"api_base_url" : "http://services.org/restapi/","methods" : { "get_info" : { "method" : "GET" } } }'
+    );
+};
+like $@, qr/Attribute \(path\) is required/;
+
+dies_ok {
+    Net::HTTP::Spore->new_from_string(
+'{"api_base_url" : "http://services.org/restapi/","methods" : { "get_info" : { "method" : "PET", "path":"/info" } } }'
+    );
+};
+like $@, qr/Attribute \(method\) does not pass the type constraint/;
+
+ok $client = Net::HTTP::Spore->new_from_string(
+'{"api_base_url" : "http://services.org/restapi/","methods" : { "get_info" : { "path" : "/show", "method" : "GET" } } }'
+);