File Coverage

blib/lib/WebService/WordsAPI.pm
Criterion Covered Total %
statement 8 21 38.1
branch 0 4 0.0
condition n/a
subroutine 3 7 42.8
pod 3 3 100.0
total 14 35 40.0


line stmt bran cond sub pod time code
1             package WebService::WordsAPI;
2             $WebService::WordsAPI::VERSION = '0.01';
3 1     1   513 use 5.006;
  1         3  
4 1     1   414 use Moo;
  1         8791  
  1         5  
5 1     1   1155 use JSON::MaybeXS;
  1         2  
  1         349  
6              
7             has key => (
8             is => 'ro',
9             required => 1,
10             );
11              
12             has ua => (
13             is => 'ro',
14             default => sub {
15             require HTTP::Tiny;
16             require IO::Socket::SSL;
17             return HTTP::Tiny->new;
18             },
19             );
20              
21             has base_url => (
22             is => 'ro',
23             default => sub { 'https://wordsapiv1.p.mashape.com/words' },
24             );
25              
26             has return_type => (
27             is => 'ro',
28             default => sub { 'perl' },
29             );
30              
31             sub _request
32             {
33 0     0     my ($self, $relurl) = @_;
34 0           my $url = $self->base_url.'/'.$relurl;
35 0           my $headers = {
36             "X-Mashape-Key" => $self->key,
37             "Accept" => "application/json",
38             };
39 0           my $response = $self->ua->get($url, { headers => $headers });
40              
41 0 0         if (not $response->{success}) {
42 0           die "failed $response->{status} $response->{reason}\n";
43             }
44              
45             return $self->return_type eq 'json'
46             ? $response->{content}
47             : decode_json($response->{content})
48 0 0         ;
49             }
50              
51             sub get_word_details
52             {
53 0     0 1   my ($self, $word) = @_;
54              
55 0           return $self->_request($word);
56             }
57              
58             sub rhymes_with
59             {
60 0     0 1   my ($self, $word) = @_;
61              
62 0           return $self->_request($word.'/rhymes');
63             }
64              
65             sub definitions
66             {
67 0     0 1   my ($self, $word) = @_;
68              
69 0           return $self->_request($word.'/definitions');
70             }
71              
72             1;
73              
74             =head1 NAME
75              
76             WebService::WordsAPI - a draft Perl 5 interface to the WordsAPI service
77              
78             =head1 SYNOPSIS
79              
80             use WebService::WordsAPI;
81             my $api = WebService::WordsAPI->new(key => '...');
82             my $details = $api->get_word_details('plan');
83             print $details->{results}[0]{definition}, "\n";
84              
85             =head1 DESCRIPTION
86              
87             This module is an interface to the WordsAPI service,
88             which provides an API for getting information about words,
89             including definitions, pronunciations, number of syllables, and more.
90              
91             This is very much a first cut at an interface,
92             so (a) the interface may well change, and
93             (b) contributions are welcome.
94              
95             To use this module you need an API I from L,
96             which is where WordsAPI is hosted.
97             They have a free level, which gets you 2500 lookups per day.
98              
99             All of the instance methods take a word and by default
100             return a Perl data structure.
101             You can request to get back the underlying JSON response instead,
102             by setting the C attribute.
103              
104             =head1 METHODS
105              
106             =head2 new
107              
108             You must provide the B that you got from rapidapi,
109             and can optionally specify the B,
110             which should be C<'perl'> or C<'json'>.
111              
112             my $api = WebService::WordsAPI->new(
113             key => '...',
114             return_type => 'json',
115             );
116              
117             I figured people wouldn't want to vary the return type
118             on a method-by-method basis,
119             which is why it's not something you can specify on individual methods.
120              
121             =head2 get_word_details
122              
123             This is the main function of the API.
124             It takes a word and returns a structure with various bits of information.
125             You may get multiple entries in the result.
126             For example when looking up "wind", it can be a verb,
127             as in I,
128             and a noun,
129             as in I.
130              
131             my $details = $api->get_word_details('wind');
132             foreach my $result (@{ $details->{results} }) {
133             printf "%s: %s\n",
134             $result->{partOfSpeech},
135             $result->{definition};
136             }
137              
138             Look at the L
139             to see exactly what is returned.
140              
141             =head2 definitions
142              
143             This returns just the definitions for the word.
144             As noted above, this may return multiple definitions:
145              
146             my $result = $api->definitions('wind');
147             foreach my $entry (@{ $result->{definitions} }) {
148             printf "%s: %s\n",
149             $entry->{partOfSpeech},
150             $entry->{definition};
151             }
152              
153             =head2 rhymes_with
154              
155             This takes a word and returns one or more lists of words
156             that rhyme with the given word:
157              
158             my $results = $api->rhymes_with('wind');
159             my $rhymes = $results->{rhymes};
160              
161             foreach my $pos (keys %$rhymes) {
162             my $words = @{ $rhymes->{ $pos } };
163             print "\n$pos: @words\n";
164             }
165              
166             =head1 SEE ALSO
167              
168             L is the home page for the service;
169             documentation for the API can be found at L.
170              
171             =head1 REPOSITORY
172              
173             L
174              
175             =head1 AUTHOR
176              
177             Neil Bowers Eneilb@cpan.orgE
178              
179             =head1 LICENSE AND COPYRIGHT
180              
181             This software is copyright (c) 2019 by Neil Bowers .
182              
183             This is free software; you can redistribute it and/or modify it under
184             the same terms as the Perl 5 programming language system itself.
185