| 1 | package WWW::Yandex::BarTIC; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use base 'Object::Accessor'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | our @EXPORT_OK = qw(get_tic); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Carp qw/carp croak/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Defaults | ||||||||||||||||||||||||||||
| 17 | 2 | my $DEF_URL_TEMPLATE = 'http://bar-navig.yandex.ru/u?url=%s&show=1'; | |||||||||||||||||||||||||||
| 18 | 2 | my $DEF_UA_AGENT = 'Mozilla/5.0 (Ubuntu; X11; Linux i686; rv:9.0.1) Gecko/20100101 Firefox/9.0.1 YB/6.5.0-en'; | |||||||||||||||||||||||||||
| 19 | 2 | my $TIC_RE = qr#<tcy rang="\d+" value="(\d+)"/>#; | |||||||||||||||||||||||||||
| 20 | 2 | my @ATTRS = qw/ua url_template/; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 1 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | my $self = $class->SUPER::new(@ATTRS); | |||||||||||||||||||||||||||
| 26 | 1 | ▶ | $self->ua($args{ua} || LWP::UserAgent->new(agent => $DEF_UA_AGENT)); | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'ua'} || 'LWP::UserAgent'->new('agent', $DEF_UA_AGENT)
MC/DC0%0/2
$args{'ua'} || 'LWP::UserAgent'->new('agent', $DEF_UA_AGENT)
$args{'ua'}
'LWP::UserAgent'->new('agent', $DEF_UA_AGENT)
Truth table33%1/3
A || B
A= $args{'ua'}B= 'LWP::UserAgent'->new('agent', $DEF_UA_AGENT)
| |||||||||||||||||||||||||||||
| 27 | 1 | $self->url_template($DEF_URL_TEMPLATE); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | return $self; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=7 SCAR=31.6
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 1 | my ($self, $url) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | croak 'I am waiting for url param' unless defined $url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | unless ($url =~ m[^https?://]i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 1 | carp 'use "http://some.domain" format for url'; | |||||||||||||||||||||||||||
| 39 | 1 | return; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | my $query = sprintf($self->url_template, uri_escape($url)); | |||||||||||||||||||||||||||
| 43 | 0 | my $resp = $self->ua->get($query); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if ($resp->is_success and $resp->content =~ $TIC_RE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$resp->is_success and $resp->content =~ /$TIC_RE/
MC/DC0%0/2
$resp->is_success and $resp->content =~ /$TIC_RE/
$resp->is_success
$resp->content =~ /$TIC_RE/
Truth table0%0/3
A and B
A= $resp->is_successB= $resp->content =~ /$TIC_RE/
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | return wantarray ? ($1, $resp) : $1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | else { | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | return wantarray ? (undef, $resp) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub get_tic { | ||||||||||||||||||||||||||
|
Subroutine
get_tic: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 0 | my ($url) = @_; | |||||||||||||||||||||||||||
| 56 | 0 | return __PACKAGE__->new()->get($url); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =head1 NAME | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | WWW::Yandex::BarTIC - Query Yandex citation index (Яндекс ТИЦ in russian) | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =head1 VERSION | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Version 0.04 | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | use WWW::Yandex::BarTIC 'get_tic'; | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # OO Style | ||||||||||||||||||||||||||||
| 76 | my $yb = WWW::Yandex::BarTIC->new(); | ||||||||||||||||||||||||||||
| 77 | my ($tic, $resp) = $yb->get('http://cpan.org'); | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Function | ||||||||||||||||||||||||||||
| 80 | my ($tic, $resp) = get_tic('http://cpan.org'); | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | The C<WWW::Yandex::BarTIC> is a class implementing a interface for | ||||||||||||||||||||||||||||
| 87 | querying yandex citation index. | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | It uses L<LWP::UserAgent> for making request to Yandex. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head2 C<get_tic> | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | You can use C<get_tic> function, but you must import it before | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | use WWW::Yandex::BarTIC 'get_tic'; | ||||||||||||||||||||||||||||
| 98 | my ($tic, $resp) = get_tic('http://mail.ru'); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | See L</"get"> method for description | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head1 METHODS | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | C<WWW::Yandex::BarTIC> implements the following methods. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head2 C<new> | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | my $yb = WWW::Yandex::BarTIC->new; | ||||||||||||||||||||||||||||
| 109 | my $yb = WWW::Yandex::BarTIC->new(ua => LWP::UserAgent->new); | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Creates a new object. If C<ua> attribute is empty, it will be created automatically with following defaults: | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | KEY DEFAULT | ||||||||||||||||||||||||||||
| 114 | ----------- -------------------- | ||||||||||||||||||||||||||||
| 115 | agent "Mozilla/5.0 (Ubuntu; X11; Linux i686; rv:9.0.1) Gecko/20100101 Firefox/9.0.1 YB/6.5.0-en" | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head2 C<get> | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | my ($tic, $resp) = $yb->get('http://cpan.org'); | ||||||||||||||||||||||||||||
| 121 | my $tic = $yb->get('http://cpan.org'); | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | Queries Yandex for a specified URL and returns TIC. If | ||||||||||||||||||||||||||||
| 124 | query successfull, integer value > 0 returned. If query fails | ||||||||||||||||||||||||||||
| 125 | for some reason (yandex unreachable, url does not begin from | ||||||||||||||||||||||||||||
| 126 | 'http://', undefined url passed) it returns C<undef>. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | In list context this function returns list from two elements where | ||||||||||||||||||||||||||||
| 129 | first is the result as in scalar context and the second is the | ||||||||||||||||||||||||||||
| 130 | C<HTTP::Response> object (returned by C<LWP::UserAgent::get>). This | ||||||||||||||||||||||||||||
| 131 | can be usefull for debugging purposes and for querying failure | ||||||||||||||||||||||||||||
| 132 | details. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head2 C<ua> | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | $yb->ua(LWP::UserAgent->new); | ||||||||||||||||||||||||||||
| 140 | $yb->ua->agent('MyAgent'); | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Get/Set L<LWP::UserAgent> object for making request to Yandex | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Alex, C<< <alexbyk at cpan.org> >> | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head1 BUGS | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Please report any bugs or feature requests to C<bug-www-yandex-bartic at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 151 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Yandex-BarTIC>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 152 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | perldoc WWW::Yandex::BarTIC | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | You can also look for information at: | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =over 4 | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Yandex-BarTIC> | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | L<http://annocpan.org/dist/WWW-Yandex-BarTIC> | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | L<http://cpanratings.perl.org/d/WWW-Yandex-BarTIC> | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | L<http://search.cpan.org/dist/WWW-Yandex-BarTIC/> | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =back | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | Copyright 2012 Alex. | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 195 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 196 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =cut | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 2 | 1; # End of WWW::Yandex::BarTIC | |||||||||||||||||||||||||||