Skip to content

Add C2 & C3 answers for challenge #012 #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions challenge-012/kian-meng-ang/perl5/ch-2.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env perl
# vi:et:sw=4 ts=4 ft=perl

use strict;
use warnings;
use utf8;
use feature qw(say);

use Array::Utils qw(intersect);

my ($separator, @paths) = (q|/|, ());

# No need $_ to prohibit useless topic
push @paths, chomp && [split /$separator/] while (<DATA>);

# Start with a base path. Compare and find common array elements against
# subsequenty paths.
my $common_path = shift @paths;
@{$common_path} = intersect(@{$common_path}, @{$_}) foreach @paths;

# Display the common prefix paths.
# $ perl ch-2.pl
# /a/b
say join $separator, @{$common_path};

# Note to self:
# __END__ and __DATA__ token cannot mixed within the same file.

1;

__DATA__
/a/b/c/d
/a/b/cd
/a/b/cc
/a/b/c/d/e
75 changes: 75 additions & 0 deletions challenge-012/kian-meng-ang/perl5/ch-3.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/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 Const::Fast;
use HTTP::Tiny;
use JSON::Tiny qw(decode_json);
use XML::Hash::XS;

const my $BASE_URL => 'https://www.abbreviations.com/services/v2/syno.php';
const my $SERVICE => 'synonyms';

MAIN: {
my $word = $ARGV[0] || croak q|Missing word!|;
my $word_info = _find_word_info(_config(), $word);

say $word_info->{synonyms};
}

sub _config {
my $rc_file = qq|$ENV{HOME}/.stands4rc|;
my $config = Config::Tiny->read($rc_file) || croak qq|Missing $rc_file!|;

$config->{$SERVICE}->{uid} || croak q|Missing API uid!|;
$config->{$SERVICE}->{token} || croak q|Missing API token!|;

return $config;
}

sub _find_word_info {
my ($config, $word) = @_;

my $params = {
uid => $config->{$SERVICE}->{uid},
token => $config->{$SERVICE}->{token},
word => $word,
format => 'json',
};

my $ua = HTTP::Tiny->new();
my $response = $ua->get($BASE_URL, $params);

if (!$response->{success}) {
croak qq|ERROR: $response->{code}, $response->{status}, $response->{reason}!|;
return;
}

# Error response is always a XML string regardless the `format` was
# explicitly set to `json`. See (2).
my $results = ($response->{content} =~ m/xml/)
? xml2hash($response->{content})
: decode_json($response->{content})->{results};

croak qq|ERROR: $results->{error}| if (exists $results->{error});

return $results->{results}->{result};
}

1;

__END__

Note to self:
(1) `HTTP::Tiny` does not have built-in way to deserialize JSON or XML response
data compare to `Mojo::UserAgent`.
(2) Is there a better way to resolve inconsistency in returned HTTP body
content? How do we check for JSON or XML string?
(3) Not all `XX::Tiny` CPAN modules were created or maintained equally.