Add the :all export tag
[webservice-tdwtf.git] / lib / WebService / TDWTF.pm
CommitLineData
54c23de0
MG
1package WebService::TDWTF;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Exporter/;
7
8use Carp;
9use HTTP::Tiny;
10use JSON::MaybeXS qw/decode_json/;
11use Scalar::Util qw/looks_like_number/;
12use WebService::TDWTF::Article;
13
14my @subs = qw/article list_recent list_series list_author/;
15our @EXPORT = map { "tdwtf_$_" } @subs;
16our @EXPORT_OK = (@EXPORT, @subs);
e7459a70 17our %EXPORT_TAGS = (all => [@EXPORT_OK]);
54c23de0
MG
18our $VERSION = '0.001';
19our $AGENT = "WebService-TDWTF/$VERSION";
20our $BASE_URL = 'http://thedailywtf.com/api';
21
22sub _ht { HTTP::Tiny->new(agent => $AGENT) }
23
24sub _query {
25 my ($url) = @_;
26
27 my $ht = _ht;
28 my $response = $ht->get($url);
29 croak $response->{reason} unless $response->{success};
30 $response = decode_json $response->{content};
31 croak $response->{Status} if ref $response eq 'HASH' && !exists $response->{BodyHtml};
32
33 $response
34}
35
36sub _objectify {
37 my ($response) = @_;
38
39 return map { _objectify($_) } @$response if ref $response eq 'ARRAY';
40 WebService::TDWTF::Article->new($response)
41}
42
43sub article {
44 my ($id_or_slug, $only_body_and_html) = @_;
45 my $url = "$BASE_URL/articles/";
46 $url .= @_ == 0 ? 'random' : looks_like_number $id_or_slug ? "/id/$id_or_slug" : "/slug/$id_or_slug";
47 $url .= '/true' if $only_body_and_html;
48 _objectify _query $url
49}
50
51sub _list {
52 my $url = join '/', $BASE_URL, @_;
53 _objectify _query $url
54}
55
56sub list_recent { my $url = @_ == 2 ? 'articles' : 'articles/recent'; _list $url, @_ }
57sub list_series { _list 'series', @_ }
58sub list_author { _list 'author', @_ }
59
60BEGIN {
61 *tdwtf_article = \&article;
62 *tdwtf_list_recent = \&list_recent;
63 *tdwtf_list_series = \&list_series;
64 *tdwtf_list_author = \&list_author;
65}
66
671;
68__END__
69
70=encoding utf-8
71
72=head1 NAME
73
73e25708 74WebService::TDWTF - retrieve articles from thedailywtf.com
54c23de0
MG
75
76=head1 SYNOPSIS
77
78 use WebService::TDWTF;
79 my $random_article = tdwtf_article;
80 say $random_article->Title;
81 say $random_article->Body;
82
83 my $x = tdwtf_article 8301;
84 say $x->Title; # Your Recommended Virus
85 my $y = tdwtf_article 'your-recommended-virus'; # $x and $y are equivalent
86
87 my @recent = tdwtf_list_recent;
88 say scalar @recent; # 8
89 @recent = tdwtf_list_recent 10;
90 say scalar @recent; # 10
91
92 my @dec15 = tdwtf_list_recent 2015, 12;
93 say $dec15[0]->Title; # Best of 2015: The A(nti)-Team
94 say $dec15[0]->Body; # (this makes an API call, see NOTES)
95 say $dec15[0]->Body; # (this doesn't make an API call)
96
97 my @erik = tdwtf_list_author 'erik-gern'; # (most recent 8 articles by Erik Gern)
98 my @sod = tdwtf_list_series 'code-sod', 5; # (most recent 5 CodeSOD articles)
99
100 # All Error'd articles published in January 2014
101 my @jan14_errord = tdwtf_list_series 'errord', 2014, 1;
102
103=head1 DESCRIPTION
104
105WebService::TDWTF is an interface to the API of L<http://thedailywtf.com>.
106Quoting the website's sidebar:
107
108 Founded in 2004 by Alex Papadimoulis, The Daily WTF is your
109 how-not-to guide for developing software. We recount tales of
110 disastrous development, from project management gone spectacularly
111 bad to inexplicable coding choices.
112
113This module exports the following functions:
114
115=over
116
117=item B<tdwtf_article>()
118
119=item B<tdwtf_article>(I<$id_or_slug>)
120
121=item B<article>()
122
123=item B<article>(I<$id_or_slug>)
124
125With an argument, returns a L<WebService::TDWTF> object representing
126the article with the given ID or slug.
127
128With no arguments, returns a L<WebService::TDWTF> object representing
129a random article.
130
131=item B<tdwtf_list_recent>()
132
133=item B<tdwtf_list_recent>(I<$count>)
134
135=item B<tdwtf_list_recent>(I<$year>, I<$month>)
136
137=item B<list_recent>()
138
139=item B<list_recent>(I<$count>)
140
141=item B<list_recent>(I<$year>, I<$month>)
142
143With no arguments, returns the most recent 8 articles.
144
145With one argument, returns the most recent I<$count> articles.
146I<$count> is at most 100.
147
148With two arguments, returns all articles published in the given month
149of the given year. I<$month> is an integer between 1 and 12.
150
151=item B<tdwtf_list_series>(I<$slug>)
152
153=item B<tdwtf_list_series>(I<$slug>, I<$count>)
154
155=item B<tdwtf_list_series>(I<$slug>, I<$year>, I<$month>)
156
157=item B<list_series>(I<$slug>)
158
159=item B<list_series>(I<$slug>, I<$count>)
160
161=item B<list_series>(I<$slug>, I<$year>, I<$month>)
162
163With no arguments, returns the most recent 8 articles in the given
164series.
165
166With one argument, returns the most recent I<$count> articles in the
167given series. I<$count> is at most 100.
168
169With two arguments, returns all articles in the given series published
170in the given month of the given year. I<$month> is an integer between
1711 and 12.
172
173=item B<tdwtf_list_author>(I<$slug>)
174
175=item B<tdwtf_list_author>(I<$slug>, I<$count>)
176
177=item B<tdwtf_list_author>(I<$slug>, I<$year>, I<$month>)
178
179=item B<list_author>(I<$slug>)
180
181=item B<list_author>(I<$slug>, I<$count>)
182
183=item B<list_author>(I<$slug>, I<$year>, I<$month>)
184
185With no arguments, returns the most recent 8 articles by the given
186author.
187
188With one argument, returns the most recent I<$count> articles by the
189given author. I<$count> is at most 100.
190
191With two arguments, returns all articles by the given author published
192in the given month of the given year. I<$month> is an integer between
1931 and 12.
194
195=back
196
197=head1 NOTES
198
199All functions are exported of the name B<tdwtf_foo> are exported by
200default. The unprefixed variants can be exported on request.
201
202The B<tdwtf_list_*> functions return a list of incomplete
203L<WebService::TDWTF::Article> objects. These objects contain all of
204the fields of a normal object, except for BodyHtml and FooterAdHtml.
205For these objects, the B<Body> mehod of L<WebService::TDWTF::Article>
206retrieves the BodyHtml and FooterAdHtml fields from the API and saves
207them into the object.
208
209All B<tdwtf_list_*> functions return articles in reverse chronological
210order. That is, the first element of the list is the most recent article.
211
212=head1 SEE ALSO
213
214L<http://thedailywtf.com/>
215
216L<https://github.com/tdwtf/WtfWebApp/blob/master/Docs/API.md>
217
218=head1 AUTHOR
219
220Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
221
222=head1 COPYRIGHT AND LICENSE
223
224Copyright (C) 2016 by Marius Gavrilescu
225
226This library is free software; you can redistribute it and/or modify
227it under the same terms as Perl itself, either Perl version 5.20.2 or,
228at your option, any later version of Perl 5 you may have available.
229
230
231=cut
This page took 0.023658 seconds and 4 git commands to generate.