File Coverage

blib/lib/IP/CountryFlag.pm
Criterion Covered Total %
statement 28 40 70.0
branch 0 2 0.0
condition n/a
subroutine 10 11 90.9
pod 1 1 100.0
total 39 54 72.2


line stmt bran cond sub pod time code
1             package IP::CountryFlag;
2              
3             $IP::CountryFlag::VERSION = '0.11';
4             $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     2   48872 use 5.006;
  2         11  
17 2     2   503 use autodie;
  2         22178  
  2         6  
18 2     2   12433 use Data::Dumper;
  2         9550  
  2         100  
19 2     2   630 use IP::CountryFlag::UserAgent;
  2         8  
  2         74  
20 2     2   728 use File::Spec::Functions qw(catfile);
  2         1377  
  2         140  
21 2     2   590 use IP::CountryFlag::Params qw(validate);
  2         8  
  2         106  
22              
23 2     2   27 use Moo;
  2         3  
  2         17  
24 2     2   956 use namespace::clean;
  2         4  
  2         17  
25             extends 'IP::CountryFlag::UserAgent';
26              
27             has 'base_url' => (is => 'ro', default => sub { return 'http://api.hostip.info/flag.php' });
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     4 1 1732 my ($self, $params) = @_;
50              
51 4         10 my $fields = { 'ip' => 1, 'path' => 1 };
52 4         9 my $url = $self->_url($fields, $params);
53 0         0 my $response = $self->get($url);
54              
55 0         0 return _save($params, $response->{content});
56             }
57              
58             #
59             #
60             # PRIVATE METHODS
61              
62             sub _save {
63 0     0   0 my ($params, $data) = @_;
64              
65 0         0 my $flag = catfile($params->{path}, $params->{ip} . ".gif");
66 0         0 eval {
67 0         0 open(my $FLAG, ">$flag");
68 0         0 binmode($FLAG);
69 0         0 print $FLAG $data;
70 0         0 close $FLAG;
71              
72 0         0 return $flag;
73             };
74              
75 0 0       0 die("ERROR: Couldn't save flag [$flag][$@].\n") if $@;
76             }
77              
78             sub _url {
79 4     4   8 my ($self, $fields, $params) = @_;
80              
81 4         13 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<< >>
89              
90             =head1 REPOSITORY
91              
92             L
93              
94             =head1 BUGS
95              
96             Please report any bugs / feature requests to C,
97             or through the web interface at L.
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
114              
115             =item * AnnoCPAN: Annotated CPAN documentation
116              
117             L
118              
119             =item * CPAN Ratings
120              
121             L
122              
123             =item * Search CPAN
124              
125             L
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
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             1; # End of IP::CountryFlag