| blib/lib/CPAN/Search/Author.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 48 | 93 | 51.6 |
| branch | 6 | 24 | 25.0 |
| condition | n/a | ||
| subroutine | 11 | 13 | 84.6 |
| pod | 4 | 5 | 80.0 |
| total | 69 | 135 | 51.1 |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package CPAN::Search::Author; | ||||||
| 2 | |||||||
| 3 | $CPAN::Search::Author::VERSION = '0.04'; | ||||||
| 4 | $CPAN::Search::Author::AUTHORITY = 'cpan:MANWAR'; | ||||||
| 5 | |||||||
| 6 | =head1 NAME | ||||||
| 7 | |||||||
| 8 | CPAN::Search::Author - Interface to search CPAN module author. | ||||||
| 9 | |||||||
| 10 | =head1 VERSION | ||||||
| 11 | |||||||
| 12 | Version 0.04 | ||||||
| 13 | |||||||
| 14 | =cut | ||||||
| 15 | |||||||
| 16 | 2 | 2 | 31937 | use 5.006; | |||
| 2 | 6 | ||||||
| 2 | 65 | ||||||
| 17 | 2 | 2 | 9 | use strict; use warnings; | |||
| 2 | 2 | 2 | |||||
| 2 | 56 | ||||||
| 2 | 7 | ||||||
| 2 | 5 | ||||||
| 2 | 78 | ||||||
| 18 | 2 | 2 | 2221 | use overload q("") => \&as_string, fallback => 1; | |||
| 2 | 1681 | ||||||
| 2 | 13 | ||||||
| 19 | |||||||
| 20 | 2 | 2 | 1338 | use Data::Dumper; | |||
| 2 | 11416 | ||||||
| 2 | 171 | ||||||
| 21 | 2 | 2 | 979 | use HTTP::Request; | |||
| 2 | 33479 | ||||||
| 2 | 65 | ||||||
| 22 | 2 | 2 | 2347646 | use LWP::UserAgent; | |||
| 2 | 1199075 | ||||||
| 2 | 65 | ||||||
| 23 | 2 | 2 | 1015 | use HTML::Entities qw/decode_entities/; | |||
| 2 | 8980 | ||||||
| 2 | 2054 | ||||||
| 24 | |||||||
| 25 | our $DEBUG = 0; | ||||||
| 26 | |||||||
| 27 | =head1 DESCRIPTION | ||||||
| 28 | |||||||
| 29 | CPAN::Search::Author is an attempt to provide programmatical interface to CPAN | ||||||
| 30 | Search engine. CPAN Search is a search engine for the distributions,modules, docs, | ||||||
| 31 | and ID's on CPAN. It was conceived and built by Graham Barr as a way to make | ||||||
| 32 | things easier to navigate. Originally named TUCS [ The Ultimate CPAN Search ] it | ||||||
| 33 | was later named CPAN Search or Search DOT CPAN. | ||||||
| 34 | |||||||
| 35 | =cut | ||||||
| 36 | |||||||
| 37 | sub new { | ||||||
| 38 | 3 | 3 | 0 | 66 | my $class = shift; | ||
| 39 | 3 | 27 | my $self = { _browser => LWP::UserAgent->new() }; | ||||
| 40 | |||||||
| 41 | 3 | 3615 | bless $self, $class; | ||||
| 42 | 3 | 20 | return $self; | ||||
| 43 | } | ||||||
| 44 | |||||||
| 45 | =head1 METHODS | ||||||
| 46 | |||||||
| 47 | =head2 by_id() | ||||||
| 48 | |||||||
| 49 | This method accepts CPAN ID exactly as provided by CPAN. It does realtime search | ||||||
| 50 | on CPAN site and fetch the author name for the given CPAN ID. However it would | ||||||
| 51 | croak if it can't access the CPAN site / unable to get any response for the given | ||||||
| 52 | CPAN ID. | ||||||
| 53 | |||||||
| 54 | use strict; use warnings; | ||||||
| 55 | use CPAN::Search::Author; | ||||||
| 56 | |||||||
| 57 | my $result = CPAN::Search::Author->new->by_id('MANWAR'); | ||||||
| 58 | |||||||
| 59 | =cut | ||||||
| 60 | |||||||
| 61 | sub by_id | ||||||
| 62 | { | ||||||
| 63 | 2 | 2 | 1 | 3 | my $self = shift; | ||
| 64 | 2 | 5 | my $id = shift; | ||||
| 65 | |||||||
| 66 | 2 | 30 | my $browser = $self->{_browser}; | ||||
| 67 | 2 | 8 | $browser->env_proxy; | ||||
| 68 | 2 | 11068 | my $request = HTTP::Request->new(POST=>qq[http://search.cpan.org/search?query=$id&mode=author]); | ||||
| 69 | 2 | 6573 | my $response = $browser->request($request); | ||||
| 70 | 2 | 50 | 1856314 | print {*STDOUT} "Search By Id [$id] Status: " . $response->status_line . "\n" if $DEBUG; | |||
| 0 | 0 | ||||||
| 71 | 2 | 50 | 11 | die("ERROR: Couldn't connect to search.cpan.org.\n") unless $response->is_success; | |||
| 72 | |||||||
| 73 | 2 | 35 | my $contents = $response->content; | ||||
| 74 | 2 | 109 | my @contents = split(/\n/,$contents); | ||||
| 75 | 2 | 15 | foreach (@contents) { | ||||
| 76 | 116 | 129 | chomp; | ||||
| 77 | 116 | 242 | s/^\s+//g; | ||||
| 78 | 116 | 221 | s/\s+$//g; | ||||
| 79 | 116 | 100 | 270 | if (/\ \ \(.*)<\/b\>/) { |
|||
| 80 | 2 | 50 | 16 | if (uc($id) eq uc($1)) { | |||
| 81 | 2 | 23 | $self->{result} = decode_entities($2); | ||||
| 82 | 2 | 108 | return $self->{result}; | ||||
| 83 | } | ||||||
| 84 | } | ||||||
| 85 | } | ||||||
| 86 | |||||||
| 87 | 0 | 0 | $self->{result} = undef; | ||||
| 88 | 0 | 0 | return; | ||||
| 89 | } | ||||||
| 90 | |||||||
| 91 | =head2 where_id_starts_with() | ||||||
| 92 | |||||||
| 93 | This method accepts an alphabet (A-Z) and get the list of authors that start with | ||||||
| 94 | the given alphabet from CPAN site realtime. However it would croak if it can't | ||||||
| 95 | access the CPAN site or unable to get any response for the given CPAN ID. | ||||||
| 96 | |||||||
| 97 | use strict; use warnings; | ||||||
| 98 | use CPAN::Search::Author; | ||||||
| 99 | |||||||
| 100 | my $result = CPAN::Search::Author->new->where_id_starts_with('M'); | ||||||
| 101 | |||||||
| 102 | =cut | ||||||
| 103 | |||||||
| 104 | sub where_id_starts_with { | ||||||
| 105 | 1 | 1 | 1 | 4 | my ($self, $letter) = @_; | ||
| 106 | |||||||
| 107 | 1 | 50 | 20 | die("ERROR: Invalid letter [$letter].\n") unless ($letter =~ /[A-Z]/i); | |||
| 108 | |||||||
| 109 | 0 | my $browser = $self->{_browser}; | |||||
| 110 | 0 | $browser->env_proxy; | |||||
| 111 | 0 | my $request = HTTP::Request->new(POST=>qq[http://search.cpan.org/author/?$letter]); | |||||
| 112 | 0 | my $response = $browser->request($request); | |||||
| 113 | 0 | 0 | print {*STDOUT} "Search Id Starts With [$letter] Status: " . $response->status_line . "\n" if $DEBUG; | ||||
| 0 | |||||||
| 114 | 0 | 0 | die("ERROR: Couldn't connect to search.cpan.org.\n") unless $response->is_success; | ||||
| 115 | |||||||
| 116 | 0 | my $contents = $response->content; | |||||
| 117 | 0 | my @contents = split(/\n/,$contents); | |||||
| 118 | |||||||
| 119 | 0 | my @authors; | |||||
| 120 | 0 | foreach (@contents) { | |||||
| 121 | 0 | chomp; | |||||
| 122 | 0 | s/^\s+//g; | |||||
| 123 | 0 | s/\s+$//g; | |||||
| 124 | 0 | 0 | if (/ | ||||
| 125 | 0 | push @authors, $1; | |||||
| 126 | } | ||||||
| 127 | } | ||||||
| 128 | |||||||
| 129 | 0 | return @authors; | |||||
| 130 | } | ||||||
| 131 | |||||||
| 132 | =head2 where_name_contains() | ||||||
| 133 | |||||||
| 134 | This method accepts a search string and look for the string in the author's name | ||||||
| 135 | of all the CPAN modules realtime and returns the a reference to a hash containing | ||||||
| 136 | id,name pair containing the search string. It croaks if unable to access the | ||||||
| 137 | search.cpan.org. | ||||||
| 138 | |||||||
| 139 | use strict; use warnings; | ||||||
| 140 | use CPAN::Search::Author; | ||||||
| 141 | |||||||
| 142 | my $result = CPAN::Search::Author->new-search->where_name_contains('MAN'); | ||||||
| 143 | |||||||
| 144 | =cut | ||||||
| 145 | |||||||
| 146 | sub where_name_contains { | ||||||
| 147 | 0 | 0 | 1 | my ($self, $query) = @_; | |||
| 148 | |||||||
| 149 | 0 | my $browser = $self->{_browser}; | |||||
| 150 | 0 | $browser->env_proxy; | |||||
| 151 | 0 | my $request = HTTP::Request->new(POST=>qq[http://search.cpan.org/search?query=$query&mode=author]); | |||||
| 152 | 0 | my $response = $browser->request($request); | |||||
| 153 | 0 | 0 | print {*STDOUT} "Search By Name Contains [$query] Status: " . $response->status_line . "\n" if $DEBUG; | ||||
| 0 | |||||||
| 154 | 0 | 0 | die("ERROR: Couldn't connect to search.cpan.org.\n") unless $response->is_success; | ||||
| 155 | |||||||
| 156 | 0 | my $contents = $response->content; | |||||
| 157 | 0 | my @contents = split(/\n/,$contents); | |||||
| 158 | |||||||
| 159 | 0 | my $authors; | |||||
| 160 | 0 | foreach (@contents) { | |||||
| 161 | 0 | chomp; | |||||
| 162 | 0 | s/^\s+//g; | |||||
| 163 | 0 | s/\s+$//g; | |||||
| 164 | 0 | 0 | $authors->{$1} = decode_entities($2) | ||||
| 165 | if (/\ \ \(.*)<\/b\>/); |
||||||
| 166 | } | ||||||
| 167 | |||||||
| 168 | 0 | $self->{result} = $authors; | |||||
| 169 | 0 | return $authors; | |||||
| 170 | } | ||||||
| 171 | |||||||
| 172 | =head2 as_string() | ||||||
| 173 | |||||||
| 174 | Return the last search result in human readable format. | ||||||
| 175 | |||||||
| 176 | use strict; use warnings; | ||||||
| 177 | use CPAN::Search::Author; | ||||||
| 178 | |||||||
| 179 | my $result = CPAN::Search::Author->new->where_name_contains('MAN'); | ||||||
| 180 | |||||||
| 181 | =cut | ||||||
| 182 | |||||||
| 183 | sub as_string { | ||||||
| 184 | 0 | 0 | 1 | my ($self) = @_; | |||
| 185 | 0 | 0 | return $self->{result} unless ref($self->{result}); | ||||
| 186 | |||||||
| 187 | 0 | my $string; | |||||
| 188 | 0 | foreach (keys %{$self->{result}}) { | |||||
| 0 | |||||||
| 189 | 0 | $string .= sprintf("%s: %s\n", $_, $self->{result}->{$_}); | |||||
| 190 | } | ||||||
| 191 | 0 | return $string; | |||||
| 192 | } | ||||||
| 193 | |||||||
| 194 | =head1 AUTHOR | ||||||
| 195 | |||||||
| 196 | Mohammad S Anwar, C<< |
||||||
| 197 | |||||||
| 198 | =head1 REPOSITORY | ||||||
| 199 | |||||||
| 200 | L |
||||||
| 201 | |||||||
| 202 | =head1 BUGS | ||||||
| 203 | |||||||
| 204 | Please report any bugs or feature requests to C |
||||||
| 205 | or through the web interface at L |
||||||
| 206 | I will be notified, and then you'll automatically be notified of progress on | ||||||
| 207 | your bug as I make changes. | ||||||
| 208 | |||||||
| 209 | =head1 SUPPORT | ||||||
| 210 | |||||||
| 211 | You can find documentation for this module with the perldoc command. | ||||||
| 212 | |||||||
| 213 | perldoc CPAN::Search::Author | ||||||
| 214 | |||||||
| 215 | You can also look for information at: | ||||||
| 216 | |||||||
| 217 | =over 4 | ||||||
| 218 | |||||||
| 219 | =item * RT: CPAN's request tracker | ||||||
| 220 | |||||||
| 221 | L |
||||||
| 222 | |||||||
| 223 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||
| 224 | |||||||
| 225 | L |
||||||
| 226 | |||||||
| 227 | =item * CPAN Ratings | ||||||
| 228 | |||||||
| 229 | L |
||||||
| 230 | |||||||
| 231 | =item * Search CPAN | ||||||
| 232 | |||||||
| 233 | L |
||||||
| 234 | |||||||
| 235 | =back | ||||||
| 236 | |||||||
| 237 | =head1 LICENSE AND COPYRIGHT | ||||||
| 238 | |||||||
| 239 | Copyright (C) 2011 - 2015 Mohammad S Anwar. | ||||||
| 240 | |||||||
| 241 | This program is free software; you can redistribute it and/or modify it under | ||||||
| 242 | the terms of the the Artistic License (2.0). You may obtain a copy of the full | ||||||
| 243 | license at: | ||||||
| 244 | |||||||
| 245 | L |
||||||
| 246 | |||||||
| 247 | Any use, modification, and distribution of the Standard or Modified Versions is | ||||||
| 248 | governed by this Artistic License.By using, modifying or distributing the Package, | ||||||
| 249 | you accept this license. Do not use, modify, or distribute the Package, if you do | ||||||
| 250 | not accept this license. | ||||||
| 251 | |||||||
| 252 | If your Modified Version has been derived from a Modified Version made by someone | ||||||
| 253 | other than you,you are nevertheless required to ensure that your Modified Version | ||||||
| 254 | complies with the requirements of this license. | ||||||
| 255 | |||||||
| 256 | This license does not grant you the right to use any trademark, service mark, | ||||||
| 257 | tradename, or logo of the Copyright Holder. | ||||||
| 258 | |||||||
| 259 | This license includes the non-exclusive, worldwide, free-of-charge patent license | ||||||
| 260 | to make, have made, use, offer to sell, sell, import and otherwise transfer the | ||||||
| 261 | Package with respect to any patent claims licensable by the Copyright Holder that | ||||||
| 262 | are necessarily infringed by the Package. If you institute patent litigation | ||||||
| 263 | (including a cross-claim or counterclaim) against any party alleging that the | ||||||
| 264 | Package constitutes direct or contributory patent infringement,then this Artistic | ||||||
| 265 | License to you shall terminate on the date that such litigation is filed. | ||||||
| 266 | |||||||
| 267 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND | ||||||
| 268 | CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED | ||||||
| 269 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR | ||||||
| 270 | NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS | ||||||
| 271 | REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, | ||||||
| 272 | INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE | ||||||
| 273 | OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||
| 274 | |||||||
| 275 | =cut | ||||||
| 276 | |||||||
| 277 | 1; # End of CPAN::Search::Author |