Add tdwtf_series function for new API call
authorMarius Gavrilescu <marius@ieval.ro>
Sat, 19 Mar 2016 18:01:10 +0000 (20:01 +0200)
committerMarius Gavrilescu <marius@ieval.ro>
Sat, 19 Mar 2016 18:01:10 +0000 (20:01 +0200)
lib/WebService/TDWTF.pm
t/WebService-TDWTF.t

index 0aa6dff1f5e5674a0ec4f17ecd76a2396c3ae98a..fe9ea4da73a9e4b02f48b624c2bb116848907d49 100644 (file)
@@ -11,7 +11,7 @@ use JSON::MaybeXS qw/decode_json/;
 use Scalar::Util qw/looks_like_number/;
 use WebService::TDWTF::Article;
 
-my @subs = qw/article list_recent list_series list_author/;
+my @subs = qw/article list_recent list_series list_author series/;
 our @EXPORT = map { "tdwtf_$_" } @subs;
 our @EXPORT_OK = (@EXPORT, @subs);
 our %EXPORT_TAGS = (all => [@EXPORT_OK]);
@@ -57,11 +57,14 @@ sub list_recent { my $url = @_ == 2 ? 'articles' : 'articles/recent'; _list $url
 sub list_series { _list 'series',   @_ }
 sub list_author { _list 'author',   @_ }
 
+sub series { @{_query "$BASE_URL/series/"} }
+
 BEGIN {
        *tdwtf_article     = \&article;
        *tdwtf_list_recent = \&list_recent;
        *tdwtf_list_series = \&list_series;
        *tdwtf_list_author = \&list_author;
+       *tdwtf_series      = \&series;
 }
 
 1;
@@ -100,6 +103,13 @@ WebService::TDWTF - retrieve articles from thedailywtf.com
   # All Error'd articles published in January 2014
   my @jan14_errord = tdwtf_list_series 'errord', 2014, 1;
 
+  my @series = series;           # List of all series
+  my $series = series;           # Number of series ($series == @series)
+  print $series[0]->Slug;        # alexs-soapbox
+  print $series[0]->Title;       # Alex's Soapbox
+  print $series[0]->Description; # <description of this series>
+  print $series[0]->CssClass;    # tales
+
 =head1 DESCRIPTION
 
 WebService::TDWTF is an interface to the API of L<http://thedailywtf.com>.
@@ -192,6 +202,16 @@ With two arguments, returns all articles by the given author published
 in the given month of the given year. I<$month> is an integer between
 1 and 12.
 
+=item B<tdwtf_series>
+
+=item B<series>
+
+In list context, returns a list of all existing article series. Each
+series is an unblessed hashref with the keys C<Slug>, C<Title>,
+C<Description> and C<CssClass>.
+
+In scalar context, returns the number of existing article series.
+
 =back
 
 =head1 NOTES
index 2c2d53ea00d224b8a1369f50b3c8257ab9fe7613..74c95753aac4a9a0b1e8ec3dfba5738e8096e4d8 100644 (file)
@@ -3,7 +3,7 @@ use strict;
 use warnings;
 
 use Test::RequiresInternet ('thedailywtf.com' => 80);
-use Test::More tests => 14;
+use Test::More tests => 16;
 BEGIN { use_ok('WebService::TDWTF') };
 
 my $art = tdwtf_article;
@@ -30,6 +30,11 @@ is @erik, 8, 'tdwtf_list_author \'erik-gern\'';
 my @sod = tdwtf_list_series 'code-sod', 5;
 is @sod, 5, 'tdwtf_list_series \'code-sod\', 5';
 
+my @series = tdwtf_series;
+note 'Found ' . @series . ' series';
+cmp_ok @series, '==', scalar tdwtf_series, 'tdwtf_series scalar context';
+my ($codesod) = grep { $_->{Title} =~ /codesod/i } @series;
+is $codesod->{Slug}, 'code-sod', 'tdwtf_series finds CodeSOD';
 
 my ($last) = tdwtf_list_recent 1;
 ok !defined $last->NextArticle, 'last article has no next article';
This page took 0.014566 seconds and 4 git commands to generate.