From 37848e36b991ed5e7dcff33d84b4f73694eddf5e Mon Sep 17 00:00:00 2001 From: franck cuny Date: Fri, 8 Oct 2010 12:01:30 +0200 Subject: add new_from_string and some tests --- lib/Net/HTTP/Spore.pm | 54 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 16 deletions(-) (limited to 'lib/Net') 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 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 object, with methods -generated from the specification file. +generated from the specification string. =back -- cgit 1.4.1