Skip to content

Instantly share code, notes, and snippets.

@j1n3l0
Created April 14, 2010 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j1n3l0/366432 to your computer and use it in GitHub Desktop.
Save j1n3l0/366432 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use Modern::Perl;
use Test::Most;
{
package RestClient;
use Moose;
use Data::Dumper;
has url => (
is => 'ro',
isa => 'Str',
required => 1,
);
sub POST { join '|', shift->url, @_ }
sub response_content { [ split( /\|/, $_[1] ) ] }
no Moose;
1;
}
my $data = '{"mgi":"MGI:12345"}';
my $rest_client = RestClient->new( url => 'http://example.com' );
my $post_response = $rest_client->POST( 'alleles.json', $data );
is $post_response, 'http://example.com|alleles.json|{"mgi":"MGI:12345"}',
'POST works';
is_deeply $rest_client->response_content($post_response),
[ 'http://example.com', 'alleles.json', $data ], 'response_content works';
{
package CurryClass;
use Moose;
use Moose::Util::TypeConstraints;
has _url => ( is => 'ro', isa => 'Str', default => 'http://delegate.org' );
has _rest_client => (
lazy_build => 1,
isa => duck_type( [qw/ POST response_content /] ),
handles => {
post => [ POST => 'alleles.json' ],
content => [ response_content => POST => 'alleles.json' ],
},
);
sub _build__rest_client { RestClient->new( url => shift->_url ) }
no Moose;
1;
}
my $curry_class = CurryClass->new;
can_ok $curry_class, qw/ post content /;
is $curry_class->post($data),
'http://delegate.org|alleles.json|{"mgi":"MGI:12345"}',
'curry_class post works';
TODO: {
local $TODO = 'if only ... (partial application)';
is_deeply $curry_class->content($data),
[ 'http://delegate.org', 'alleles.json', $data ],
'curry_class content works';
}
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment