blib/lib/JSON/Feed.pm

filter: Statementstmt 88.8% 32 / 36 Branchbran 25.0% 1 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.8% 9 / 11 Podpod 100% 5 / 5 totaltotal 83.9% 47 / 56 SCAR 11.1
CC
3
Cov
82%
to_string
3.8
__ANON__
2.0
set
2.0
CRAP 3.0
↑ Summary blib/lib/JSON/Feed/Types.pm »
1 package JSON::Feed;
2 2 our $VERSION = '1.000';
3 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use JSON qw<to_json from_json>;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use JSON::Feed::Types qw<JSONFeed JSONFeedItem>;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 has feed => (
9 is => 'ro',
10 default => sub {
11 return +{
12 0 ▶ version => "https://jsonfeed.org/version/1",
Subroutine __ANON__: not called CC=1 SCAR=6.9
13 title => 'Untitle',
14 items => [],
15 };
16 },
17 2 isa => JSONFeed,
18 );
19  
20 around BUILDARGS => sub {
21 12 ▶ my ( $orig, $class, %args ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
22 return +{
23 12 feed => +{
24 version => "https://jsonfeed.org/version/1",
25 items => [],
26 %args
27 }
28 };
29 2 };
30  
31 sub get {
32 11 ▶ my ( $self, $attr_name ) = @_;
Subroutine get: called CC=1 SCAR=0.0
Pod documented
33 11 return $self->feed->{$attr_name};
34 }
35  
36 sub set {
37 0 ▶ my ( $self, $attr_name, $v ) = @_;
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
38 0 return $self->feed->{$attr_name} = $v;
39 }
40  
41 sub add_item {
42 2 ▶ my ( $self, %item ) = @_;
Subroutine add_item: called CC=1 SCAR=0.0
Pod documented
43 2 my $item = \%item;
44 2 JSONFeedItem->assert_valid($item);
45 2 push @{ $self->feed->{items} }, $item;
46 }
47  
48 sub to_string {
49 1 ▶ my ($self) = @_;
Subroutine to_string: called CC=3 SCAR=13.3
Pod documented
50 1 my $feed = $self->feed;
51 1 ▶ if (exists $feed->{expired}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $feed->{"expired"}) 0 1 1
52 0 ▶ $feed->{expired} = $feed->{expired} ? JSON::true : JSON::false;
Branch: 1 branch
BranchTrueFalseTotal
$feed->{'expired'} ? : 0 0 0
53 }
54 1 return to_json( $feed );
55 }
56  
57 sub from_string {
58 11 ▶ my ( $class, $text ) = @_;
Subroutine from_string: called CC=1 SCAR=0.0
Pod documented
59 11 my $data = from_json($text);
60 11 return $class->new( %$data );
61 }
62  
63 2 1;
64  
65 =head1 NAME
66  
67 JSON::Feed - Syndication with JSON.
68  
69 =head1 SYNOPSIS
70  
71 Parsing:
72  
73 JSON::Feed->from_string( $json_text );
74  
75 Generating:
76  
77 # Initialize, with some content.
78 my $feed = JSON::Feed->new(
79 title => "An example of JSON feed",
80 feed_url => "https://example.com/feed.json",
81 items => [
82 +{
83 id => 42,
84 url => 'https://example.com/item/42',
85 summary => 'An item with some summary',
86 date_published: "2019-03-06T09:24:03+09:00"
87 },
88 +{
89 id => 623,
90 url => 'https://example.com/item/623',
91 summary => 'An item with some summary',
92 date_published: "2019-03-07T06:22:51+09:00"
93 },
94 ]
95 );
96  
97 # Mutate
98 $feed->set( description => 'Some description here.' );
99 $feed->add_item(
100 id => 789,
101 title => "Another URL-less item here",
102 summary => "Another item here. Lorem ipsum yatta yatta yatta.",
103 );
104  
105 # Output
106 print $fh $feed->to_string;
107  
108 =head1 DESCRIPTION
109  
110 L<JSON Feed|https://jsonfeed.org/> is a simple format for website syndication
111 with JSON, instead of XML.
112  
113 This module implements minimal amout of API for parsing and/or generating such
114 feeds. The users of this module should glance over the jsonfeed spec in order
115 to correctly generate a JSON::Feed.
116  
117 Here's a short but yet comprehensive Type mapping between jsonfeed spec and
118 perl.
119  
120 | jsonfeed | perl |
121 |----------+----------------------------|
122 | object | HashRef |
123 | boolean | JSON::true, or JSON::false |
124 | string | Str |
125 | array | ArrayRef |
126  
127 =head1 METHODS
128  
129 =over 4
130  
131 =item set( $attr, $value )
132  
133 The C<$attr> here must be a name from one top-level attribute
134 from L<v1 spec|https://jsonfeed.org/version/1>.
135  
136 The passed C<$value> thus must be the corresponding value.
137  
138 Most of the values from spec are strings and that maps to a perl scalar veraible.
139 The term `object` in the spec is mapped to a perl HashRef.
140  
141 Be aware that the spec allows feed extensions by prefixng attributes with
142 underscore character. Thus, all strings begin with C<'_'> are valid. Whatever
143 those extented attributes mapped to are left as-is.
144  
145 =item get( $attr )
146  
147 Retrieve the value of the given top-level varible.
148  
149 =item add_item( JSONFeedItem $item )
150  
151 Apend the given C<$item> to the C<items> attribute. The type of input C<$item>
152 is described in the "Items" section of L<the spec|https://jsonfeed.org/version/1>.
153  
154 =item to_string()
155  
156 Stringify this JSON Feed. At this moment, the feed structure is checked and if
157 it is invalid, an exception is thrown.
158  
159 =item from_string( $json_text )
160  
161 Take a reference to a string that is assumed to be a valid json feed and
162 produce an object from it. Exception will be thrown if the input is not a
163 valid JSON feed.
164  
165 This method is supposed to be consume the output of C<to_string> method
166 without throwing exceptions.
167  
168 =back
169  
170 =head1 References
171  
172 JSON Feed spec v1 L<https://jsonfeed.org/version/1>
173  
174 =head1 AUTHOR
175  
176 Kang-min Liu <gugod@gugod.org>
177  
178 =head1 LICENSE
179  
180 CC0
↑ Summary blib/lib/JSON/Feed/Types.pm »