[Bps-public-commit] net-lighthouse branch, master, updated. 3d1ca3f8a550a07b1bd96bf4fc374047c797609d

sunnavy at bestpractical.com sunnavy at bestpractical.com
Fri Sep 4 00:02:36 EDT 2009


The branch, master has been updated
       via  3d1ca3f8a550a07b1bd96bf4fc374047c797609d (commit)
      from  49ae40d9ca124f5b767e7e3518c515605870abdd (commit)

Summary of changes:
 lib/Net/Lighthouse/Token.pm |   20 +++++++++++++++++++-
 t/08-token.t                |   27 ++++++++++++++++++---------
 2 files changed, 37 insertions(+), 10 deletions(-)

- Log -----------------------------------------------------------------
commit 3d1ca3f8a550a07b1bd96bf4fc374047c797609d
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Fri Sep 4 12:02:31 2009 +0800

    token should have load method

diff --git a/lib/Net/Lighthouse/Token.pm b/lib/Net/Lighthouse/Token.pm
index 137349c..5ca0252 100644
--- a/lib/Net/Lighthouse/Token.pm
+++ b/lib/Net/Lighthouse/Token.pm
@@ -1,8 +1,8 @@
 package Net::Lighthouse::Token;
 use Any::Moose;
-use XML::Simple;
 use Params::Validate ':all';
 use Net::Lighthouse::Util;
+use base 'Net::Lighthouse';
 
 # read only attr
 has 'created_at' => (
@@ -38,6 +38,24 @@ has [ 'account', 'note' ] => (
 no Any::Moose;
 __PACKAGE__->meta->make_immutable;
 
+sub load {
+    my $self = shift;
+    validate_pos( @_, { type => SCALAR, regex => qr/^\w{40}$/ } );
+    my $token = shift;
+
+    my $ua = $self->ua;
+    my $url = $self->base_url . '/tokens/' . $token . '.xml';
+    my $res = $ua->get( $url );
+    if ( $res->is_success ) {
+        $self->load_from_xml( $res->content );
+    }
+    else {
+        die "try to get $url failed: "
+          . $res->status_line . "\n"
+          . $res->content;
+    }
+}
+
 sub load_from_xml {
     my $self = shift;
     my $ref = Net::Lighthouse::Util->translate_from_xml(shift);
diff --git a/t/08-token.t b/t/08-token.t
index 5401016..821188e 100644
--- a/t/08-token.t
+++ b/t/08-token.t
@@ -1,13 +1,14 @@
 use strict;
 use warnings;
 
-use Test::More tests => 19;
+use Test::More tests => 20;
 use DateTime;
+use Test::Mock::LWP;
 
 use_ok('Net::Lighthouse::Token');
 can_ok( 'Net::Lighthouse::Token', 'new' );
 
-my $token = Net::Lighthouse::Token->new;
+my $token = Net::Lighthouse::Token->new( account => 'sunnavy' );
 isa_ok( $token, 'Net::Lighthouse::Token' );
 
 for my $attr (
@@ -18,14 +19,22 @@ for my $attr (
     can_ok( $token, $attr );
 }
 
-can_ok( $token, 'load_from_xml' );
+for my $method ( qw/load load_from_xml/ ) {
+    can_ok( $token, $method );
+}
+
+$Mock_ua->mock( get            => sub { $Mock_response } );
+$Mock_ua->mock( default_header => sub { } );                  # to erase warning
+$Mock_response->mock(
+    content => sub {
+        local $/;
+        open my $fh, '<', 't/data/token.xml' or die $!;
+        <$fh>;
+    }
+);
+
+my $m = $token->load('a'x40);
 
-my $xml = do {
-    local $/;
-    open my $fh, '<', 't/data/token.xml' or die $!;
-    <$fh>;
-};
-my $m = $token->load_from_xml($xml);
 is( $m, $token, 'load return $self' );
 my %hash = (
     'created_at' => DateTime->new(

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list