| 1 | package IP::CountryFlag; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | $IP::CountryFlag::VERSION = '0.11'; | |||||||||
| 4 | 2 | $IP::CountryFlag::AUTHORITY = 'cpan:MANWAR'; | |||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | IP::CountryFlag - Interface to fetch country flag of an IP. | ||||||||||
| 9 | |||||||||||
| 10 | =head1 VERSION | ||||||||||
| 11 | |||||||||||
| 12 | Version 0.11 | ||||||||||
| 13 | |||||||||||
| 14 | =cut | ||||||||||
| 15 | |||||||||||
| 16 | 2 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 2 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 2 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 2 | ▶ | use IP::CountryFlag::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 2 | ▶ | use File::Spec::Functions qw(catfile); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 2 | ▶ | use IP::CountryFlag::Params qw(validate); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 2 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 2 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 2 | extends 'IP::CountryFlag::UserAgent'; | |||||||||
| 26 | |||||||||||
| 27 | 1 | ▶ | has 'base_url' => (is => 'ro', default => sub { return 'http://api.hostip.info/flag.php' }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||
| 30 | |||||||||||
| 31 | A very thin wrapper for the hostip.info API to get the country flag of an IP address. | ||||||||||
| 32 | |||||||||||
| 33 | =head1 METHOD | ||||||||||
| 34 | |||||||||||
| 35 | =head2 save() | ||||||||||
| 36 | |||||||||||
| 37 | Saves the country flag in the given location for the given IP address. It returns the location | ||||||||||
| 38 | of the country flag where it has been saved. | ||||||||||
| 39 | |||||||||||
| 40 | use strict; use warnings; | ||||||||||
| 41 | use IP::CountryFlag; | ||||||||||
| 42 | |||||||||||
| 43 | my $countryFlag = IP::CountryFlag->new; | ||||||||||
| 44 | print $countryFlag->save({ ip => '12.215.42.19', path => './' }); | ||||||||||
| 45 | |||||||||||
| 46 | =cut | ||||||||||
| 47 | |||||||||||
| 48 | sub save { | ||||||||||
| 49 | 4 | ▶ | my ($self, $params) = @_; | ||||||||
|
Subroutine
save: called CC=1 SCAR=0.6
Pod
documented
| |||||||||||
| 50 | |||||||||||
| 51 | 4 | my $fields = { 'ip' => 1, 'path' => 1 }; | |||||||||
| 52 | 4 | my $url = $self->_url($fields, $params); | |||||||||
| 53 | 0 | my $response = $self->get($url); | |||||||||
| 54 | |||||||||||
| 55 | 0 | return _save($params, $response->{content}); | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | # | ||||||||||
| 59 | # | ||||||||||
| 60 | # PRIVATE METHODS | ||||||||||
| 61 | |||||||||||
| 62 | sub _save { | ||||||||||
| 63 | 0 | ▶ | my ($params, $data) = @_; | ||||||||
|
Subroutine
_save: not called CC=2 SCAR=17.9
| |||||||||||
| 64 | |||||||||||
| 65 | 0 | my $flag = catfile($params->{path}, $params->{ip} . ".gif"); | |||||||||
| 66 | 0 | eval { | |||||||||
| 67 | 0 | open(my $FLAG, ">$flag"); | |||||||||
| 68 | 0 | binmode($FLAG); | |||||||||
| 69 | 0 | print $FLAG $data; | |||||||||
| 70 | 0 | close $FLAG; | |||||||||
| 71 | |||||||||||
| 72 | 0 | return $flag; | |||||||||
| 73 | }; | ||||||||||
| 74 | |||||||||||
| 75 | 0 | ▶ | die("ERROR: Couldn't save flag [$flag][$@].\n") if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | sub _url { | ||||||||||
| 79 | 4 | ▶ | my ($self, $fields, $params) = @_; | ||||||||
|
Subroutine
_url: called CC=1 SCAR=0.4
| |||||||||||
| 80 | |||||||||||
| 81 | 4 | validate($fields, $params); | |||||||||
| 82 | |||||||||||
| 83 | 0 | return sprintf("%s?ip=%s", $self->base_url, $params->{ip}); | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | =head1 AUTHOR | ||||||||||
| 87 | |||||||||||
| 88 | Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||||||||||
| 89 | |||||||||||
| 90 | =head1 REPOSITORY | ||||||||||
| 91 | |||||||||||
| 92 | L<https://github.com/manwar/IP-CountryFlag> | ||||||||||
| 93 | |||||||||||
| 94 | =head1 BUGS | ||||||||||
| 95 | |||||||||||
| 96 | Please report any bugs / feature requests to C<bug-ip-countryflag at rt.cpan.org>, | ||||||||||
| 97 | or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IP-CountryFlag>. | ||||||||||
| 98 | I will be notified & then you'll automatically be notified of progress on your bug | ||||||||||
| 99 | as I make changes. | ||||||||||
| 100 | |||||||||||
| 101 | =head1 SUPPORT | ||||||||||
| 102 | |||||||||||
| 103 | You can find documentation for this module with the perldoc command. | ||||||||||
| 104 | |||||||||||
| 105 | perldoc IP::CountryFlag | ||||||||||
| 106 | |||||||||||
| 107 | You can also look for information at: | ||||||||||
| 108 | |||||||||||
| 109 | =over 4 | ||||||||||
| 110 | |||||||||||
| 111 | =item * RT: CPAN's request tracker | ||||||||||
| 112 | |||||||||||
| 113 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=IP-CountryFlag> | ||||||||||
| 114 | |||||||||||
| 115 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 116 | |||||||||||
| 117 | L<http://annocpan.org/dist/IP-CountryFlag> | ||||||||||
| 118 | |||||||||||
| 119 | =item * CPAN Ratings | ||||||||||
| 120 | |||||||||||
| 121 | L<http://cpanratings.perl.org/d/IP-CountryFlag> | ||||||||||
| 122 | |||||||||||
| 123 | =item * Search CPAN | ||||||||||
| 124 | |||||||||||
| 125 | L<http://search.cpan.org/dist/IP-CountryFlag/> | ||||||||||
| 126 | |||||||||||
| 127 | =back | ||||||||||
| 128 | |||||||||||
| 129 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 130 | |||||||||||
| 131 | Copyright (C) 2011 - 2017 Mohammad S Anwar. | ||||||||||
| 132 | |||||||||||
| 133 | This program is free software; you can redistribute it and/or modify it under | ||||||||||
| 134 | the terms of the the Artistic License (2.0). You may obtain a copy of the full | ||||||||||
| 135 | license at: | ||||||||||
| 136 | |||||||||||
| 137 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||
| 138 | |||||||||||
| 139 | Any use, modification, and distribution of the Standard or Modified Versions is | ||||||||||
| 140 | governed by this Artistic License.By using, modifying or distributing the Package, | ||||||||||
| 141 | you accept this license. Do not use, modify, or distribute the Package, if you do | ||||||||||
| 142 | not accept this license. | ||||||||||
| 143 | |||||||||||
| 144 | If your Modified Version has been derived from a Modified Version made by someone | ||||||||||
| 145 | other than you,you are nevertheless required to ensure that your Modified Version | ||||||||||
| 146 | complies with the requirements of this license. | ||||||||||
| 147 | |||||||||||
| 148 | This license does not grant you the right to use any trademark, service mark, | ||||||||||
| 149 | tradename, or logo of the Copyright Holder. | ||||||||||
| 150 | |||||||||||
| 151 | This license includes the non-exclusive, worldwide, free-of-charge patent license | ||||||||||
| 152 | to make, have made, use, offer to sell, sell, import and otherwise transfer the | ||||||||||
| 153 | Package with respect to any patent claims licensable by the Copyright Holder that | ||||||||||
| 154 | are necessarily infringed by the Package. If you institute patent litigation | ||||||||||
| 155 | (including a cross-claim or counterclaim) against any party alleging that the | ||||||||||
| 156 | Package constitutes direct or contributory patent infringement,then this Artistic | ||||||||||
| 157 | License to you shall terminate on the date that such litigation is filed. | ||||||||||
| 158 | |||||||||||
| 159 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND | ||||||||||
| 160 | CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED | ||||||||||
| 161 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR | ||||||||||
| 162 | NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS | ||||||||||
| 163 | REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, | ||||||||||
| 164 | INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE | ||||||||||
| 165 | OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||
| 166 | |||||||||||
| 167 | DEAFilter API itself is distributed under the terms of the Gnu GPLv3 licence. | ||||||||||
| 168 | |||||||||||
| 169 | =cut | ||||||||||
| 170 | |||||||||||
| 171 | 2 | 1; # End of IP::CountryFlag | |||||||||