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