| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Dictionary::Cambridge; |
|
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:JINNKS'; |
|
3
|
|
|
|
|
|
|
# ABSTRACT: A simple module for the Cambridge Dictionary API, only implemented |
|
4
|
|
|
|
|
|
|
# one method to get an entry (meaning of a word from the dictionary) |
|
5
|
|
|
|
|
|
|
$Dictionary::Cambridge::VERSION = '0.01'; # TRIAL |
|
6
|
1
|
|
|
1
|
|
1311
|
use Moose; |
|
|
1
|
|
|
|
|
469584
|
|
|
|
1
|
|
|
|
|
7
|
|
|
7
|
1
|
|
|
1
|
|
7803
|
use HTTP::Request; |
|
|
1
|
|
|
|
|
22896
|
|
|
|
1
|
|
|
|
|
32
|
|
|
8
|
1
|
|
|
1
|
|
1004
|
use LWP::UserAgent; |
|
|
1
|
|
|
|
|
24100
|
|
|
|
1
|
|
|
|
|
42
|
|
|
9
|
1
|
|
|
1
|
|
703
|
use URI::Encode; |
|
|
1
|
|
|
|
|
14587
|
|
|
|
1
|
|
|
|
|
56
|
|
|
10
|
1
|
|
|
1
|
|
7
|
use JSON; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
10
|
|
|
11
|
1
|
|
|
1
|
|
843
|
use namespace::autoclean; |
|
|
1
|
|
|
|
|
8463
|
|
|
|
1
|
|
|
|
|
5
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
with 'Dictionary::Cambridge::Response'; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has "base_url" => ( |
|
16
|
|
|
|
|
|
|
is => 'ro', |
|
17
|
|
|
|
|
|
|
isa => 'Str', |
|
18
|
|
|
|
|
|
|
default => 'https://dictionary.cambridge.org/api/v1/' |
|
19
|
|
|
|
|
|
|
); |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has "dictionary" => ( |
|
22
|
|
|
|
|
|
|
is => 'rw', |
|
23
|
|
|
|
|
|
|
isa => 'Str', |
|
24
|
|
|
|
|
|
|
required => 1 |
|
25
|
|
|
|
|
|
|
); |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has "format" => ( |
|
28
|
|
|
|
|
|
|
is => 'rw', |
|
29
|
|
|
|
|
|
|
isa => 'Str', |
|
30
|
|
|
|
|
|
|
default => 'xml' |
|
31
|
|
|
|
|
|
|
); |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
has "access_key" => ( |
|
34
|
|
|
|
|
|
|
is => 'rw', |
|
35
|
|
|
|
|
|
|
isa => 'Str', |
|
36
|
|
|
|
|
|
|
required => 1 |
|
37
|
|
|
|
|
|
|
); |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
has "user_agent" => ( |
|
40
|
|
|
|
|
|
|
is => 'ro', |
|
41
|
|
|
|
|
|
|
isa => 'LWP::UserAgent', |
|
42
|
|
|
|
|
|
|
lazy_build => 1 |
|
43
|
|
|
|
|
|
|
); |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
has "encode_uri" => ( |
|
46
|
|
|
|
|
|
|
is => 'ro', |
|
47
|
|
|
|
|
|
|
isa => 'URI::Encode', |
|
48
|
|
|
|
|
|
|
lazy_build => 1 |
|
49
|
|
|
|
|
|
|
); |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
has "json" => ( |
|
52
|
|
|
|
|
|
|
is => 'ro', |
|
53
|
|
|
|
|
|
|
isa => 'JSON', |
|
54
|
|
|
|
|
|
|
lazy_build => 1 |
|
55
|
|
|
|
|
|
|
); |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub _build_user_agent { |
|
58
|
|
|
|
|
|
|
|
|
59
|
0
|
|
|
0
|
|
|
return LWP::UserAgent->new(); |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub _build_http_request { |
|
63
|
|
|
|
|
|
|
|
|
64
|
0
|
|
|
0
|
|
|
return HTTP::Request->new(); |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _build_encode_uri { |
|
68
|
|
|
|
|
|
|
|
|
69
|
0
|
|
|
0
|
|
|
return URI::Encode->new(); |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub _build_json { |
|
73
|
|
|
|
|
|
|
|
|
74
|
0
|
|
|
0
|
|
|
return JSON->new()->utf8; |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub get_entry { |
|
80
|
|
|
|
|
|
|
|
|
81
|
0
|
|
|
0
|
|
|
my ( $self, $word ) = @_; |
|
82
|
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
my $response; |
|
84
|
|
|
|
|
|
|
my $hashed_content; |
|
85
|
|
|
|
|
|
|
#return an error message unless there is entry_id and dict_id |
|
86
|
0
|
0
|
0
|
|
|
|
return "Dictionary id or word not found" unless $self->dictionary and $word; |
|
87
|
0
|
0
|
|
|
|
|
return "format of the reponse content is required" unless $self->format; |
|
88
|
0
|
0
|
0
|
|
|
|
return "Format allowed is html or xml" |
|
89
|
|
|
|
|
|
|
unless $self->format eq 'xml' |
|
90
|
|
|
|
|
|
|
or $self->format eq 'html'; |
|
91
|
0
|
|
|
|
|
|
$self->user_agent->default_header( accessKey => $self->access_key ); |
|
92
|
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
my $uri = $self->base_url; |
|
94
|
0
|
|
|
|
|
|
$uri .= 'dictionaries/' . $self->dictionary . '/entries/'; |
|
95
|
0
|
|
|
|
|
|
$uri .= $word . '/?format=' . $self->format; |
|
96
|
0
|
|
|
|
|
|
$uri = $self->encode_uri->encode($uri); |
|
97
|
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
eval { $response = $self->user_agent->get($uri); }; |
|
|
0
|
|
|
|
|
|
|
|
99
|
0
|
0
|
|
|
|
|
if ( my $e = $@ ) { |
|
100
|
0
|
|
|
|
|
|
die "Could not get response from API $e"; |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
|
|
103
|
0
|
0
|
0
|
|
|
|
if ( $response->is_success and $response->content ) { |
|
104
|
0
|
|
|
|
|
|
my $data = $self->json->decode( $response->content ); |
|
105
|
0
|
|
|
|
|
|
$hashed_content = $self->parse_xml_def_eg($data->{entryContent}); |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
else { |
|
108
|
0
|
|
|
|
|
|
my $data = $self->json->decode($response->content); |
|
109
|
0
|
|
|
|
|
|
return $data->{errorMessage}; |
|
110
|
|
|
|
|
|
|
} |
|
111
|
0
|
|
|
|
|
|
$hashed_content; |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
|
115
|
|
|
|
|
|
|
1; |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__END__ |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=pod |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=encoding UTF-8 |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 NAME |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Dictionary::Cambridge - A simple module for the Cambridge Dictionary API, only implemented |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head1 VERSION |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
version 0.01 |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
use Dictionary::Cambridge |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
my $dictionary = Dictionary::Cambridge->new( |
|
136
|
|
|
|
|
|
|
access_key => $ENV{ACCESS_KEY}, |
|
137
|
|
|
|
|
|
|
dictionary => 'british', |
|
138
|
|
|
|
|
|
|
format => 'xml' |
|
139
|
|
|
|
|
|
|
); |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
my $meaning = $dictionary->get_entry("test"); |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
A simple module to interact with Cambridge Dictionary API, this module will only be able to get the meaning of the words |
|
146
|
|
|
|
|
|
|
and their relevant examples if they exist. Also this is my first release so please be patient on mistake and errors. |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head2 METHODS |
|
149
|
|
|
|
|
|
|
get_entry |
|
150
|
|
|
|
|
|
|
params: word to get the meaning of |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
L<http://dictionary-api.cambridge.org/api/resources> |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 AUTHOR |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Farhan Siddiqui <forsadia@gmail.com> |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
This software is copyright (c) 2015 by Farhan Siddiqui. |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
165
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=cut |