From 7d350619965f8732ac15d27cf8bdd0ed8454442b Mon Sep 17 00:00:00 2001 From: Gianni Ceccarelli Date: Sat, 29 Dec 2012 13:00:02 +0000 Subject: add helper methods to Entries --- lib/Feed.pm | 2 ++ lib/Feed/HelperRole/FeedEntry.pm | 28 ++++++++++++++++++++++++++++ lib/Feed/HelperRole/Mail.pm | 6 +++--- lib/Feed/Role/DeDupe.pm | 6 +++--- 4 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 lib/Feed/HelperRole/FeedEntry.pm (limited to 'lib') diff --git a/lib/Feed.pm b/lib/Feed.pm index 7264638..0bfe224 100644 --- a/lib/Feed.pm +++ b/lib/Feed.pm @@ -9,6 +9,7 @@ use XML::Feed; use Log::Log4perl; use LWP::UserAgent; use URI::Fetch; +use Moose::Util 'ensure_all_roles'; sub log { my ($self) = @_; @@ -112,6 +113,7 @@ sub process { $self->log->trace('process - begin'); for my $entry ($self->entries) { + ensure_all_roles($entry,'Feed::HelperRole::FeedEntry'); $self->process_entry($entry); } diff --git a/lib/Feed/HelperRole/FeedEntry.pm b/lib/Feed/HelperRole/FeedEntry.pm new file mode 100644 index 0000000..3abe2e1 --- /dev/null +++ b/lib/Feed/HelperRole/FeedEntry.pm @@ -0,0 +1,28 @@ +package Feed::HelperRole::FeedEntry; +use Moose::Role; +use 5.012; +use namespace::autoclean; + +requires 'content','summary','id','link','modified','issued'; + +sub unified_id { + my ($self) = @_; + + return $self->id // $self->link; +} + +sub unified_date { + my ($self) = @_; + + return $self->modified // $self->issued; +} + +sub unified_content { + my ($self) = @_; + + my $c = $self->content; + return $c if $c->body; + return $self->summary; +} + +1; diff --git a/lib/Feed/HelperRole/Mail.pm b/lib/Feed/HelperRole/Mail.pm index 44a80c0..e0e239e 100644 --- a/lib/Feed/HelperRole/Mail.pm +++ b/lib/Feed/HelperRole/Mail.pm @@ -73,14 +73,14 @@ sub entry_to_mime { my ($self,$entry) = @_; my $from = 'feeder@localhost'; - my $date = $entry->modified // $entry->issued + my $date = $entry->unified_date // DateTime->now(time_zone=>'UTC'); my $from_name = $entry->author // $self->title; try { $from_name = decode('utf-8',$from_name) }; $from_name =~ tr/,//d; my $subject = $entry->title//'(no title)'; try { $subject = decode('utf-8',$subject) }; - my $id = sha1_hex($entry->id // $entry->link); + my $id = sha1_hex($entry->unified_id); my $body = $self->prepare_body($entry); @@ -116,7 +116,7 @@ sub prepare_body { feeder => $self, feed => $self->feed, entry => $entry, - content => $entry->content, + content => $entry->unified_content, }, \$out, ) diff --git a/lib/Feed/Role/DeDupe.pm b/lib/Feed/Role/DeDupe.pm index 4e470d7..4c0c6d0 100644 --- a/lib/Feed/Role/DeDupe.pm +++ b/lib/Feed/Role/DeDupe.pm @@ -144,12 +144,12 @@ sub mark_seen { sub _entry_id { my ($self,$e) = @_; - my $id = $e->id // $e->link; + my $id = $e->unified_id; $self->log->trace("_entry_id: $id"); if ($self->dedupe_use_date) { - my $date = $e->modified // $e->issued; + my $date = $e->unified_date; if ($date) { $id .= '-' . $date->iso8601; $self->log->trace("_entry_id: (with date) $id"); @@ -157,7 +157,7 @@ sub _entry_id { } if ($self->dedupe_use_body) { - my $body = $e->content->body; + my $body = $e->unified_content->body; if ($body) { my $content_digest = sha1_base64(encode('utf-8',$body)); $id .= '-' . $content_digest; -- cgit v1.2.3