From Code to Community: Sponsoring The Perl and Raku Conference 2025 Learn more

Changes 03
MANIFEST 03
META.json 11
META.yml 11
lib/Feed/Data/Object.pm 10
lib/Feed/Data/Parser/Base.pm 20
lib/Feed/Data/Parser/YAML.pm 048
lib/Feed/Data/Parser.pm 33
lib/Feed/Data/Stream.pm 31
lib/Feed/Data.pm 312
t/01-feed.t 11
t/12-yaml.t 097
t/data/test.yml 015
13 files changed (This is a version diff) 15185
@@ -12,3 +12,6 @@ Revision history for Feed-Data
0.05 2020-04-17
- adds new parser Feed::Data::Parser::Table
+
+0.06 2020-05-04
+ - adds new parser Feed::Data::Parser::YAML
@@ -23,6 +23,7 @@ lib/Feed/Data/Parser/Meta.pm
lib/Feed/Data/Parser/RSS.pm
lib/Feed/Data/Parser/Table.pm
lib/Feed/Data/Parser/Text.pm
+lib/Feed/Data/Parser/YAML.pm
lib/Feed/Data/Stream.pm
Makefile.PL
MANIFEST This list of files
@@ -40,6 +41,7 @@ t/08-okay.t
t/09-json.t
t/10-csv.t
t/11-table.t
+t/12-yaml.t
t/data/atom-10-example.xml
t/data/atom-full.xml
t/data/atom.xml
@@ -56,6 +58,7 @@ t/data/test.csv
t/data/test.html
t/data/test.json
t/data/test.txt
+t/data/test.yml
t/data/theory.atom
t/data/theory.rss
t/manifest.t
@@ -54,6 +54,6 @@
}
},
"release_status" : "stable",
- "version" : "0.05",
+ "version" : "0.06",
"x_serialization_backend" : "JSON::PP version 2.97001"
}
@@ -36,5 +36,5 @@ requires:
XML::Atom::Feed: '0'
XML::RSS::LibXML: '0.3105'
perl: '5.006'
-version: '0.05'
+version: '0.06'
x_serialization_backend: 'CPAN::Meta::YAML version 0.018'
@@ -1,6 +1,5 @@
package Feed::Data::Object;
-our $VERSION = '0.01';
use Moo;
use Carp qw/croak/;
use Class::Load qw/load_class/;
@@ -15,8 +15,6 @@ BEGIN {
);
}
-our $VERSION = '0.01';
-
has 'content_ref' => (
is => 'rw',
lazy => 1,
@@ -0,0 +1,48 @@
+package Feed::Data::Parser::YAML;
+
+use Moo;
+extends 'Feed::Data::Parser::Base';
+use Compiled::Params::OO qw/cpo/;
+use Types::Standard qw/Object HashRef Str/;
+use YAML::XS;
+
+our $validate;
+BEGIN {
+ $validate = cpo(
+ get_value => [Object, HashRef, Str],
+ );
+}
+
+has '+parser' => (
+ default => sub {
+ my $self = shift;
+ my $content = $self->content_ref;
+ my $matches = YAML::XS::Load $$content;
+ return { items => $matches };
+ },
+);
+
+has '+potential_fields' => (
+ default => sub {
+ return {
+ title => 'title',
+ description => 'description',
+ date => 'date',
+ author => 'author',
+ category => 'category',
+ permalink => 'permalink',
+ comment => 'comment',
+ link => 'link',
+ content => 'content',
+ image => 'image',
+ };
+ },
+);
+
+sub get_value {
+ my ($self, $item, $action) = $validate->get_value->(@_);
+ my $value = $item->{$action};
+ return $value // '';
+}
+
+1; # End of Data::Feed
@@ -6,9 +6,6 @@ use Carp qw/croak/;
use Class::Load qw/load_class/;
use Types::Standard qw/Object ScalarRef Str/;
-
-our $VERSION = '0.01';
-
has 'stream' => (
is => 'ro',
isa => ScalarRef,
@@ -29,6 +26,8 @@ has 'parse_tag' => (
$tag = 'json';
} elsif ($$content =~ m/^([A-Za-z]+,)/) {
$tag = 'csv';
+ } elsif ($$content =~ m/---/) {
+ $tag = 'yaml';
} else {
while ( $$content =~ /<(\S+)/sg) {
(my $t = $1) =~ tr/a-zA-Z0-9:\-\?!//cd;
@@ -56,6 +55,7 @@ has 'parser_type' => (
return 'JSON' if $tag =~ /^json/;
return 'CSV' if $tag =~ /^csv/;
return 'Table' if $tag =~ /^table/;
+ return 'YAML' if $tag =~ /^yaml/;
return croak "Could not find a parser";
}
);
@@ -18,8 +18,6 @@ BEGIN {
);
}
-our $VERSION = '0.01';
-
has 'stream' => (
is => 'rw',
isa => Str,
@@ -34,7 +32,7 @@ has 'stream_type' => (
my $self = shift;
return 'url' if $self->stream =~ m{^http}xms;
return 'string' if $self->stream =~ m{\<\?xml}xms;
- return 'file' if $self->stream =~ m{(\.xml|\.html|\.txt|\.json|\.csv)}xms;
+ return 'file' if $self->stream =~ m{(\.xml|\.html|\.txt|\.json|\.csv|\.yml)}xms;
}
);
@@ -12,9 +12,10 @@ use JSON;
use Compiled::Params::OO qw/cpo/;
use XML::RSS::LibXML;
use Text::CSV_XS qw/csv/;
+use YAML::XS qw//;
use 5.006;
-our $VERSION = '0.05';
+our $VERSION = '0.06';
our $validate;
BEGIN {
@@ -26,6 +27,7 @@ BEGIN {
raw => [Any, Optional->of(Str)],
text => [Any, Optional->of(Str)],
json => [Any, Optional->of(Str)],
+ yaml => [Any, Optional->of(Str)],
csv => [Any, Optional->of(Str)],
table => [Any, Optional->of(Str)],
convert_feed => [Any, Str, Str]
@@ -158,6 +160,12 @@ sub _json {
return $json->pretty->encode( \@render );
}
+sub _yaml {
+ my ( $self, $type ) = $validate->yaml->(@_);
+ my @render = $self->_convert_feed('generate', 'json');
+ return YAML::XS::Dump \@render;
+}
+
sub _table {
my ( $self, $type ) = $validate->table->(@_);
my @render = $self->_convert_feed('generate', 'json');
@@ -236,7 +244,7 @@ Feed::Data - dynamic data feeds
=head1 VERSION
-Version 0.05
+Version 0.06
=head1 SYNOPSIS
@@ -257,7 +265,7 @@ Version 0.05
$object->render('text'); # text, html, xml..
$object->hash('text'); # text, html, xml...
$object->fields('title', 'description'); # returns title and description object
- $object->edit(title => 'WoW', description => 'something amazing'); # sets
+ $object->edit(title => 'TTI', description => 'Should have been a PWA.'); # sets
$object->title->text;
$object->link->raw;
@@ -427,6 +435,7 @@ render the feed using the passed in format, defaults to text.
# json
# rss
# csv
+ # yaml
# table
# styled_table
@@ -9,7 +9,7 @@ BEGIN {
my $feed = Feed::Data->new();
$feed->parse( 't/data/rss20.xml' );
-$feed->write('test.html', 'table');
+$feed->write('test.yaml', 'yaml');
subtest 'feed options' => sub {
plan tests => 13;
@@ -0,0 +1,97 @@
+use strict;
+use warnings;
+use Test::More;
+
+BEGIN {
+ use_ok("Feed::Data");
+}
+
+my $feed = Feed::Data->new();
+
+$feed->parse( 't/data/test.yml' );
+
+ok($feed->write('test.yml', 'yaml'), 'write');
+
+subtest 'feed options' => sub {
+ plan tests => 13;
+ test_options({
+ action => 'all',
+ feed => $feed,
+ });
+ test_options({
+ action => 'count',
+ feed => $feed,
+ output => 2,
+ });
+ test_options({
+ action => 'get',
+ feed => $feed,
+ input => 1,
+ isa_object => 'Feed::Data::Object'
+ });
+ test_options({
+ action => 'pop',
+ feed => $feed,
+ isa_object => 'Feed::Data::Object'
+ });
+ test_options({
+ action => 'insert',
+ feed => $feed,
+ input => bless( {
+ 'object' => {
+ 'title' => 'Entry Two',
+ 'description' => 'Hello!...',
+ 'pub_date' => 'Sat, 29 May 2004 23:39:25 -0800'
+ } }, 'Feed::Data::Object' ),
+ count => 2,
+ });
+ test_options({
+ action => 'delete',
+ feed => $feed,
+ input => 1,
+ count => 1,
+ });
+ test_options({
+ action => 'pop', # cant call delete on index 0
+ feed => $feed,
+ });
+ test_options({
+ action => 'is_empty',
+ feed => $feed,
+ });
+};
+
+done_testing();
+
+sub test_options {
+ my ($args) = @_;
+
+ my $feed = $args->{feed};
+ my $action = $args->{action};
+ my $input = $args->{input};
+ my $test;
+
+ if ($input) {
+ ok($test = $feed->$action($input), "action: $action");
+ }
+ else {
+ ok($test = $feed->$action, "action: $action");
+ }
+
+ if (my $output = $args->{output}) {
+ is($test, $output, "correct output for action: $action - $output");
+ }
+ elsif (my $isa_object = $args->{isa_object}) {
+ $input ||= 'no input';
+ isa_ok($test, $isa_object, "correct output for action: $action input: $input");
+ }
+
+ if (my $count = $args->{count}){
+ is($feed->count, $count, "correct count: $count after action $action");
+ }
+}
+
+
+
+1;
@@ -0,0 +1,15 @@
+---
+- author: Melody
+ category: Travel
+ date: Sat, 29 May 2004 23:39:25 -0800
+ description: Hello!...
+ title: Entry Two
+- category: Sports
+ date: Sat, 08 May 2004 23:03:28 -0800
+ description: This is a test. Why don't you come down to our place for a coffee and
+ a chat?...
+ title: Test