| 1 | package Net::IChat::Message::File; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use EO; | ||
| 7 | use LWP::UserAgent; | ||
| 8 | use Class::Accessor::Chained; | ||
| 9 | use base qw( EO Class::Accessor::Chained ); | ||
| 10 | |||
| 11 | our $VERSION = '0.01'; | ||
| 12 | |||
| 13 | Net::IChat::Message::File->mk_accessors( | ||
| 14 | qw( | ||
| 15 | url | ||
| 16 | ) | ||
| 17 | ); | ||
| 18 | |||
| 19 | exception Net::IChat::Message::File::CouldNotFetch; | ||
| 20 | |||
| 21 | sub parse { | ||
| 22 | my $self = shift; | ||
| 23 | my $doc = shift; | ||
| 24 | my $val = $doc->findvalue('//iq'); | ||
| 25 | $val =~ s/\n//g; | ||
| 26 | $self->url( $val ); | ||
| 27 | $self; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub load { | ||
| 31 | my $self = shift; | ||
| 32 | my $ua = LWP::UserAgent->new(); | ||
| 33 | my $response = $ua->get( $self->url ); | ||
| 34 | if ($response->is_success()) { | ||
| 35 | return $response->content; | ||
| 36 | } else { | ||
| 37 | throw Net::IChat::Message::File::CouldNotFetch text => "could not fetch file: " . $response->status_line; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | 1; | ||
| 42 |