aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKian-Meng, Ang <kianmeng@cpan.org>2019-06-23 17:22:09 +0800
committerKian-Meng, Ang <kianmeng@cpan.org>2019-06-23 17:52:39 +0800
commit44bfe801adc34a4cb5e9b9551ef15c6daaad3bb5 (patch)
tree67a88efd03f8d310a4da00c1b8883516c0012dc2
parentad39df8b6e1363fdbaa43d3e2f5808ed1e6feb29 (diff)
downloadperlweeklychallenge-club-44bfe801adc34a4cb5e9b9551ef15c6daaad3bb5.tar.gz
perlweeklychallenge-club-44bfe801adc34a4cb5e9b9551ef15c6daaad3bb5.tar.bz2
perlweeklychallenge-club-44bfe801adc34a4cb5e9b9551ef15c6daaad3bb5.zip
Add C1 & C3 answer for challenge #013
-rw-r--r--challenge-013/kian-meng-ang/perl5/ch-1.pl33
-rw-r--r--challenge-013/kian-meng-ang/perl5/ch-3.pl152
2 files changed, 185 insertions, 0 deletions
diff --git a/challenge-013/kian-meng-ang/perl5/ch-1.pl b/challenge-013/kian-meng-ang/perl5/ch-1.pl
new file mode 100644
index 0000000000..5698766a01
--- /dev/null
+++ b/challenge-013/kian-meng-ang/perl5/ch-1.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+# vi:et:sw=4 ts=4 ft=perl
+
+use strict;
+use warnings;
+use utf8;
+use feature qw(say);
+use DateTime;
+
+foreach my $month (1..12) {
+ my $dt = DateTime->last_day_of_month(year => 2019, month => $month);
+ while ($dt->day_of_week() != 5) {
+ $dt->subtract(days => 1);
+ }
+ say $dt->ymd(q|/|);
+}
+
+1;
+
+__END__
+$ perl ch-1.pl
+2019/01/25
+2019/02/22
+2019/03/29
+2019/04/26
+2019/05/31
+2019/06/28
+2019/07/26
+2019/08/30
+2019/09/27
+2019/10/25
+2019/11/29
+2019/12/27
diff --git a/challenge-013/kian-meng-ang/perl5/ch-3.pl b/challenge-013/kian-meng-ang/perl5/ch-3.pl
new file mode 100644
index 0000000000..7ec287218c
--- /dev/null
+++ b/challenge-013/kian-meng-ang/perl5/ch-3.pl
@@ -0,0 +1,152 @@
+#!/usr/bin/env perl
+# vi:et:sw=4 ts=4 ft=perl
+
+use strict;
+use warnings;
+use utf8;
+use feature qw(say);
+use Carp;
+use Config::Tiny;
+use JSON::MaybeXS;
+use REST::Client;
+use Data::Dumper;
+use Text::ANSITable;
+binmode(STDOUT, ":utf8");
+
+# Output
+# $ perl ch-3.pl soliloquy
+# Word: soliloquy
+# 1 Definition: speech you make to yourself
+#
+# Part of Speech: noun
+#
+# Type of: speech, voice communication, speech communication, spoken
+# communication, spoken language, language, oral communication
+#
+# Synonyms: monologue
+#
+# 2 Definition: a (usually long) dramatic speech intended to give the illusion
+# of unspoken reflections
+#
+# Part of Speech: noun
+#
+# Type of: actor's line, speech, words
+#
+# Synonyms: -
+
+MAIN: {
+ my $word = $ARGV[0] || croak q|Missing word!|;
+ my $word_info = _fetch_word(_config(), $word);
+ _display_word_info($word, $word_info);
+}
+
+sub _display_word_info {
+ my ($word, $word_info) = @_;
+
+ my $t = Text::ANSITable->new(use_utf8 => 1, pad => 0);
+ $t->border_style('Default::none_ascii');
+ $t->columns(['', qq|Word: $word|]);
+
+ my $i = 1;
+ foreach (@{$word_info->{results}}) {
+ my $info = '';
+ $info .= "Definition:\n" . $_->{definition} . "\n\n";
+ $info .= "Part of Speech:\n" . $_->{partOfSpeech} . "\n\n";
+ $info .= "Type of:\n" . (join ', ', @{$_->{typeOf}}) . "\n\n";
+ $info .= "Synonyms:\n"
+ . (($_->{synonyms} && join ', ', @{$_->{synonyms}}) || '-') . "\n";
+
+ $t->add_row([$i++, $info]);
+ }
+ print $t->draw;
+}
+
+sub _fetch_word {
+ my ($config, $word) = @_;
+
+ my $sample_json = do { local $/; <DATA> };
+
+ # The JSON string is already decoded, skip it otherwise you will expect
+ # "Wide character in subroutine entry at ch-3.pl line 25, <DATA> line 1."
+ # error.
+ # See https://stackoverflow.com/a/10710462
+ return JSON::MaybeXS->new->utf8(0)->decode($sample_json) if $config->{test};
+
+ my $client = REST::Client->new;
+ $client->addHeader('X-Mashape-Key', $config->{key});
+ $client->setHost($config->{url});
+
+ my $response = $client->GET("/words/$word");
+ my $body = $response->responseContent();
+ my $code = $response->responseCode();
+ $code >= 400 && croak sprintf 'Error: HTTP (%s) %s', $code, $body;
+
+ my $json = decode_json($body);
+ return $json;
+}
+
+sub _config {
+ my $rc_file = qq|$ENV{HOME}/.wordsapi.rc|;
+ my $config = Config::Tiny->read($rc_file)->{_} || croak qq|Missing $rc_file!|;
+
+ $config->{url} || croak q|Missing API URL!|;
+ $config->{key} || croak q|Missing API key!|;
+ $config->{test} || croak q|Missing test flag!|;
+
+ return $config;
+}
+
+
+# Sample data from https://www.wordsapi.com/docs/
+# Unfortunately, you have to sign up with your credit card details before you
+# can obtain the API key. ;-(
+
+1;
+__DATA__
+{
+ "results":[
+ {
+ "definition":"speech you make to yourself",
+ "partOfSpeech":"noun",
+ "synonyms":[
+ "monologue"
+ ],
+ "typeOf":[
+ "speech",
+ "voice communication",
+ "speech communication",
+ "spoken communication",
+ "spoken language",
+ "language",
+ "oral communication"
+ ],
+ "derivation":[
+ "soliloquize"
+ ]
+ },
+ {
+ "definition":"a (usually long) dramatic speech intended to give the illusion of unspoken reflections",
+ "partOfSpeech":"noun",
+ "typeOf":[
+ "actor's line",
+ "speech",
+ "words"
+ ],
+ "derivation":[
+ "soliloquize"
+ ]
+ }
+ ],
+ "syllables":{
+ "count":4,
+ "list":[
+ "so",
+ "lil",
+ "o",
+ "quy"
+ ]
+ },
+ "pronunciation":{
+ "all":"sə'lɪləkwi"
+ }
+}