blib/lib/Net/MW/API.pm

filter: Statementstmt 68.1% 30 / 44 Branchbran 50.0% 6 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 54.5% 6 / 11 Podpod 100% 2 / 2 totaltotal 63.7% 44 / 69 CC 7 SCAR 12.3
CC
7
Cov
64%
_build_raw_xml
6.5
audio_url
6.0
_subdir
4.0
CRAP 3.4
↑ Summary
1 package Net::MW::API;
2 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 our $VERSION = '0.08';
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 has 'dict' => (
9 is => 'rw',
10 isa => 'Str',
11 required => 1
12 );
13  
14 1 has 'word' => (
15 is => 'rw',
16 isa => 'Str',
17 required => 1
18 );
19  
20 1 has 'key' => (
21 is => 'ro',
22 isa => 'Str',
23 required => 1
24 );
25  
26 ## the query format
27 ## http://www.dictionaryapi.com/api/v1/references/$dic/xml/$word?key=$key
28  
29 has 'url' => (
30 is => 'ro',
31 lazy => 1,
32 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 my $self = shift;
34 0 "http://www.dictionaryapi.com/api/v1/references/"
35 . uri_escape( $self->dict ) . "/xml/"
36 . uri_escape( $self->word ) . "?key="
37 . uri_escape( $self->key );
38 }
39 1 );
40  
41 1 has 'raw_xml' => (
42 is => 'ro',
43 lazy => 1,
44 builder => '_build_raw_xml',
45 );
46  
47 0 ▶sub _build_raw_xml {
Subroutine _build_raw_xml: not called CC=3 SCAR=18.7
48 1 ▶ use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 0 my $self = shift;
50 0 my $response = HTTP::Tiny->new->get( $self->url );
51 0 ▶ die "Failed!\n" unless $response->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"success"} 0 0 0
52 0 ▶ $response->{content} if length $response->{content};
Branch: 1 branch
BranchTrueFalseTotal
if length $response->{"content"} 0 0 0
53 }
54  
55 has 'dom' => (
56 is => 'ro',
57 lazy => 1,
58 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 my $xml = shift->raw_xml;
60 0 my $dom = XML::LibXML->load_xml( string => $xml );
61 },
62 1 );
63  
64 0 ▶sub entries {
Subroutine entries: not called CC=1 SCAR=6.9
Pod documented
65 0 my $self = shift;
66 0 my @entries = $self->dom->getElementsByTagName("entry");
67 }
68  
69 #In the XML, audio references look like this:
70 #<wav>heart001.wav</wav>
71  
72 #These need to be converted to a URL like this:
73 #http://media.merriam-webster.com/soundc11/h/heart001.wav
74  
75 #Start with the base URL: http://media.merriam-webster.com/soundc11/
76 #Add the first letter of the wav file as a subdirectory ("h" in the example above).*
77 #Add the name of the wav file.
78 #* Regarding the subdirectory element of the URL there are three exceptions:
79  
80 #If the file name begins with "bix", the subdirectory should be "bix".
81 #If the file name begins with "gg", the subdirectory should be "gg".
82 #If the file name begins with a number, the subdirectory should be "number".
83  
84 5 ▶sub _subdir {
Subroutine _subdir: called CC=4 SCAR=13.9
85 5 my $filename = shift;
86 5 ▶ if ( $filename =~ /^bix/ ) { return "bix" }
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /^bix/) 1 4 5
87 4 ▶ if ( $filename =~ /^gg/ ) { return "gg" }
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /^gg/) 1 3 4
88 3 ▶ if ( $filename =~ /^(\d+).*/ ) { return $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /^(\d+).*/) 2 1 3
89 1 return substr $filename, 0, 1;
90 }
91  
92 0 ▶sub audio_url {
Subroutine audio_url: not called CC=2 SCAR=17.9
Pod documented
93 0 my $self = shift;
94 0 ▶ my $tag = $self->dom->getElementsByTagName("wav") or die "can not find the audio uril for" . $self->word;
Branch: 1 branch
BranchTrueFalseTotal
my $tag = $self->dom->getElementsByTagName("wav") or die 'can not find the audio uril for' . $self->word 0 0 0
95 0 my $wave = $tag->[0]->string_value;
96 0 "http://media.merriam-webster.com/soundc11/" . _subdir($wave) . "/$wave";
97 }
98  
99 1 1;
100 __END__
↑ Summary