line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JSON::Feed; |
2
|
|
|
|
|
|
|
our $VERSION = '1.001'; |
3
|
2
|
|
|
2
|
|
473492
|
use Moo; |
|
2
|
|
|
|
|
22838
|
|
|
2
|
|
|
|
|
10
|
|
4
|
2
|
|
|
2
|
|
3909
|
use namespace::clean; |
|
2
|
|
|
|
|
23302
|
|
|
2
|
|
|
|
|
15
|
|
5
|
2
|
|
|
2
|
|
1314
|
use JSON qw; |
|
2
|
|
|
|
|
10365
|
|
|
2
|
|
|
|
|
13
|
|
6
|
2
|
|
|
2
|
|
1235
|
use JSON::Feed::Types qw; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
21
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has feed => ( |
9
|
|
|
|
|
|
|
is => 'ro', |
10
|
|
|
|
|
|
|
default => sub { |
11
|
|
|
|
|
|
|
return +{ |
12
|
|
|
|
|
|
|
version => "https://jsonfeed.org/version/1", |
13
|
|
|
|
|
|
|
title => 'Untitle', |
14
|
|
|
|
|
|
|
items => [], |
15
|
|
|
|
|
|
|
}; |
16
|
|
|
|
|
|
|
}, |
17
|
|
|
|
|
|
|
isa => JSONFeed, |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
around BUILDARGS => sub { |
21
|
|
|
|
|
|
|
my ( $orig, $class, %args ) = @_; |
22
|
|
|
|
|
|
|
return +{ |
23
|
|
|
|
|
|
|
feed => +{ |
24
|
|
|
|
|
|
|
version => "https://jsonfeed.org/version/1", |
25
|
|
|
|
|
|
|
items => [], |
26
|
|
|
|
|
|
|
%args |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
}; |
29
|
|
|
|
|
|
|
}; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub get { |
32
|
11
|
|
|
11
|
1
|
2006
|
my ( $self, $attr_name ) = @_; |
33
|
11
|
|
|
|
|
73
|
return $self->feed->{$attr_name}; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub set { |
37
|
0
|
|
|
0
|
1
|
0
|
my ( $self, $attr_name, $v ) = @_; |
38
|
0
|
|
|
|
|
0
|
return $self->feed->{$attr_name} = $v; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub add_item { |
42
|
2
|
|
|
2
|
1
|
208
|
my ( $self, %item ) = @_; |
43
|
2
|
|
|
|
|
5
|
my $item = \%item; |
44
|
2
|
|
|
|
|
8
|
JSONFeedItem->assert_valid($item); |
45
|
2
|
|
|
|
|
164
|
push @{ $self->feed->{items} }, $item; |
|
2
|
|
|
|
|
14
|
|
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub to_string { |
49
|
1
|
|
|
1
|
1
|
6
|
my ($self) = @_; |
50
|
1
|
|
|
|
|
4
|
my $feed = $self->feed; |
51
|
1
|
50
|
|
|
|
5
|
if (exists $feed->{expired}) { |
52
|
0
|
0
|
|
|
|
0
|
$feed->{expired} = $feed->{expired} ? JSON::true : JSON::false; |
53
|
|
|
|
|
|
|
} |
54
|
1
|
|
|
|
|
6
|
return to_json( $feed ); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub from_string { |
58
|
11
|
|
|
11
|
1
|
18315
|
my ( $class, $text ) = @_; |
59
|
11
|
|
|
|
|
60
|
my $data = from_json($text); |
60
|
11
|
|
|
|
|
5582
|
return $class->new( %$data ); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
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 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. |
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 attribute. The type of input C<$item> |
152
|
|
|
|
|
|
|
is described in the "Items" section of L. |
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 method |
166
|
|
|
|
|
|
|
without throwing exceptions. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=back |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 References |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
JSON Feed spec v1 L |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 AUTHOR |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Kang-min Liu |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 LICENSE |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
CC0 |