Changeset 1604

Show
Ignore:
Timestamp:
07/07/05 07:11:04
Author:
miyagawa
Message:

whole refactoring of the code: 0.03

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • XML-Atom-Lifeblog/trunk/Changes

    r1589 r1604  
    11Revision history for Perl extension XML::Atom::Lifeblog 
     2 
     30.03  Wed Jul  6 14:43:23 PDT 2005 
     4        * Support explicit Content-Type and title with Media object 
    25 
    360.02  Thu Mar 31 20:29:05 JST 2005 
  • XML-Atom-Lifeblog/trunk/MANIFEST

    r1529 r1604  
    11Changes 
    22lib/XML/Atom/Lifeblog.pm 
     3lib/XML/Atom/Lifeblog/Media.pm 
    34Makefile.PL 
    45MANIFEST                        This list of files 
    5 META.yml                        Module meta-data (added by MakeMaker) 
    66REAMDE 
    77t/00_compile.t 
  • XML-Atom-Lifeblog/trunk/Makefile.PL

    r1535 r1604  
    77        MIME::Types => 0, 
    88        XML::Atom => 0.11, 
     9        File::Type => 0, 
    910    }, 
    1011); 
  • XML-Atom-Lifeblog/trunk/lib/XML/Atom/Lifeblog.pm

    r1589 r1604  
    33use strict; 
    44use vars qw($VERSION); 
    5 $VERSION = '0.02'; 
     5$VERSION = '0.03'; 
    66 
    77use Encode; 
     
    1010use XML::Atom::Client; 
    1111use XML::Atom::Entry; 
     12use XML::Atom::Lifeblog::Media; 
    1213use base qw(XML::Atom::Client); 
    1314 
    1415sub postLifeblog { 
    1516    my($self, $post_uri, $title, $body, $media) = @_; 
    16     my($content, $media_title); 
    17     if (ref($media)) { 
    18         return $self->error("post scalarref is now depreciated."); 
     17    if (!UNIVERSAL::isa($media, "XML::Atom::Lifeblog::Media") && !ref($media)) { 
     18        $media = XML::Atom::Lifeblog::Media->new(filename => $media); 
     19    } elsif (ref($media) eq 'HASH') { 
     20        $media = XML::Atom::Lifeblog::Media->new(%$media); 
    1921    } 
    2022 
    21     # XXX should it support chunked POST? 
    22     local $/; 
    23     open my $fh, $media or return $self->error("$media: $!"); 
    24     $content = <$fh>; 
    25     $media_title = File::Basename::basename($media); 
    26  
    27     my $mime_type  = $self->_guess_mime_type($media); 
    28     my $atom_media = $self->_create_media($media_title, $content, $mime_type); 
     23    my $atom_media = $self->_create_media($media); 
    2924    my $posted = $self->_post_entry($post_uri, $atom_media) 
    3025        or return $self->error("POST ($media) failed: " . $self->errstr); 
    31     my $atom_body = $self->_create_body($title, $body, $posted->id, $mime_type); 
     26    my $atom_body = $self->_create_body($title, $body, $posted->id, $media->type); 
    3227    return $self->_post_entry($post_uri, $atom_body); 
    3328} 
     
    4641 
    4742sub _create_media { 
    48     my($self, $media_title, $content, $mime_type) = @_; 
     43    my($self, $media) = @_; 
    4944 
    5045    my $entry = XML::Atom::Entry->new(); 
    51     $entry->title($media_title); 
    52     $entry->content($content); 
    53     $entry->content->type($mime_type); 
     46    $entry->title($media->title); 
     47    $entry->content($media->content); 
     48    $entry->content->type($media->type); 
    5449 
    5550    # add <standalone>1</standalone> 
     
    112107  my $entry = $client->postLifeblog($PostURI, $title, $body, "foobar.jpg"); 
    113108 
     109  my $media = XML::Atom::Lifeblog::Media->new(content => $data); 
     110  my $entry = $client->postLifeblog($PostURI, $title, $body, $media); 
     111 
    114112=head1 DESCRIPTION 
    115113 
     
    121119XML::Atom::Lifeblog is a subclass of XML::Atom::Client. 
    122120 
    123 =head1 postLifeblog 
     121=over 4 
     122 
     123=item postLifeblog 
    124124 
    125125  my $entry = $client->postLifeblog($PostURI, $title, $body, $media); 
    126126 
    127127Creates a new Lifeblog entry and post it to a Lifeblog aware server 
    128 using C<< <standalone> >> element. C<$media> is a filepath of image or video files to be posted. 
     128using C<< <standalone> >> element. C<$media> is either a 
     129XML::Atom::Lifeblog::Media object, or a filepath of media file to be 
     130posted. 
    129131 
    130132Returns XML::Atom::Entry object for the posted entry. 
     133 
     134There're several ways to create Media object. At least you should specify how to fetch media data. C<filename>, C<filehandle> or C<content>. 
     135 
     136  # create Media object 
     137  # Content-Type is auto-guessed and media title is auto-determined 
     138  my $media = XML::Atom::Lifeblog::Media->new(filename => "foo.jpg"); 
     139  my $media = XML::Atom::Lifeblog::Media->new(filehandle => $fh); 
     140  my $media = XML::Atom::Lifeblog::Media->new(content  => $data); 
     141 
     142If you omit other parameters like C<type> and C<title>, they're automatically guessed and generated using MIME type and file magic. If you want to specify them explicitly, you can do this like: 
     143 
     144  my $media = XML::Atom::Lifeblog::Media->new( 
     145      filehandle => $fh, type => "video/3gpp", title => "My dog.3gp", 
     146  ); 
     147 
     148  # Then post it with $title & $body to $PostURI 
     149  my $entry = $client->postLifeblog($PostURI, $title, $body, $media); 
     150 
     151=back 
    131152 
    132153=head1 AUTHOR 
  • XML-Atom-Lifeblog/trunk/t/01_live.t

    r1535 r1604  
    11use strict; 
    22use XML::Atom::Lifeblog; 
     3use XML::Atom::Lifeblog::Media; 
    34 
    45use Test::More; 
     
    2526ok $entry->link->href, $entry->link->href; 
    2627 
    27 $tmp = "t/jedi.3gp"; 
    28 mirror "http://joi.ito.com/images/jedi.3gp" => $tmp; 
     28## test XML::Atom::Lifeblog::Media 
     29$tmp = "t/me.jpg"; 
     30my $content = slurp($tmp); 
     31open my($fh), $tmp; 
    2932 
    30 $entry = $client->postLifeblog($uri, "Hello", "From Joi", $tmp) or warn $client->errstr; 
    31 ok $entry->link->href, $entry->link->href; 
     33my @tests = ( 
     34    { filename => $tmp }, { type => "image/jpeg", title => "me.jpg" }, 
     35    { content  => $content }, { type => "image/jpeg", title => qr/^XML::Atom.*\.jpeg/ }, 
     36    { content  => $content, type => "image/gif" }, { type => "image/gif", title => qr/^XML::Atom.*\.gif/ }, 
     37    { content  => $content, title => "foobar.jpg" }, { type => "image/jpeg", title => "foobar.jpg" }, 
     38    { filehandle => $fh }, { type => "image/jpeg", title => qr/^XML::Atom.*\.jpeg/ }, 
     39); 
     40 
     41while (my($in, $out) = splice @tests, 0, 2) { 
     42    my $media = XML::Atom::Lifeblog::Media->new(%$in); 
     43    for my $test (keys %$out) { 
     44        if (ref($out->{$test}) && ref($out->{$test}) eq 'Regexp') { 
     45            like $media->$test(), $out->{$test}, $test; 
     46        } else { 
     47            is $media->$test(), $out->{$test}, $test; 
     48        } 
     49    } 
     50
     51 
     52sub slurp { open my $fh, shift; join '', <$fh> } 
    3253 
    3354END { unlink $_ for qw(t/me.jpg t/jedi.3gp) };