diff options
author | franck cuny <franck@lumberjaph.net> | 2010-10-08 12:01:30 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-10-08 12:01:30 +0200 |
commit | 37848e36b991ed5e7dcff33d84b4f73694eddf5e (patch) | |
tree | d160c6a0a143e53475efca5935987bb14435aa64 /lib/Net | |
parent | update API; add POD (diff) | |
download | net-http-spore-37848e36b991ed5e7dcff33d84b4f73694eddf5e.tar.gz |
add new_from_string and some tests
Diffstat (limited to 'lib/Net')
-rw-r--r-- | lib/Net/HTTP/Spore.pm | 54 |
1 files changed, 38 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 |