|
| 1 | +package Geo::DetailsLite; |
| 2 | + |
| 3 | +use 5.006; |
| 4 | +use strict; |
| 5 | +use warnings; |
| 6 | + |
| 7 | +sub new { |
| 8 | + my $class = shift; |
| 9 | + my $data = shift; |
| 10 | + my $key = shift // ''; |
| 11 | + |
| 12 | + # If $data is a hash reference, directly bless it into the class and return. |
| 13 | + if ( ref($data) eq 'HASH' ) { |
| 14 | + bless $data, $class; |
| 15 | + return $data; |
| 16 | + } |
| 17 | + |
| 18 | + # If $data is a plain string, create a new hash reference and set the specified key to the string value. |
| 19 | + # Use the provided key or default to ''. |
| 20 | + my $self = { $key => $data }; |
| 21 | + bless $self, $class; |
| 22 | + return $self; |
| 23 | +} |
| 24 | + |
| 25 | +sub TO_JSON { |
| 26 | + my ($self) = @_; |
| 27 | + |
| 28 | + # Return a copy of the object as a hash reference for JSON encoding |
| 29 | + return {%$self}; |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +sub ip { |
| 34 | + return $_[0]->{ip}; |
| 35 | +} |
| 36 | + |
| 37 | +sub country { |
| 38 | + return $_[0]->{country}; |
| 39 | +} |
| 40 | + |
| 41 | +sub country_code { |
| 42 | + return $_[0]->{country_code}; |
| 43 | +} |
| 44 | + |
| 45 | +sub country_name { |
| 46 | + return $_[0]->{country_name}; |
| 47 | +} |
| 48 | + |
| 49 | +sub country_flag { |
| 50 | + return $_[0]->{country_flag}; |
| 51 | +} |
| 52 | + |
| 53 | +sub country_flag_url { |
| 54 | + return $_[0]->{country_flag_url}; |
| 55 | +} |
| 56 | + |
| 57 | +sub country_currency { |
| 58 | + return $_[0]->{country_currency}; |
| 59 | +} |
| 60 | + |
| 61 | +sub continent { |
| 62 | + return $_[0]->{continent}; |
| 63 | +} |
| 64 | + |
| 65 | +sub continent_code { |
| 66 | + return $_[0]->{continent_code}; |
| 67 | +} |
| 68 | + |
| 69 | +sub is_eu { |
| 70 | + return $_[0]->{is_eu}; |
| 71 | +} |
| 72 | + |
| 73 | +sub asn { |
| 74 | + return $_[0]->{asn}; |
| 75 | +} |
| 76 | + |
| 77 | +sub as_name { |
| 78 | + return $_[0]->{as_name}; |
| 79 | +} |
| 80 | + |
| 81 | +sub as_domain { |
| 82 | + return $_[0]->{as_domain}; |
| 83 | +} |
| 84 | + |
| 85 | +sub all { |
| 86 | + return $_[0]; |
| 87 | +} |
| 88 | + |
| 89 | +#------------------------------------------------------------------------------- |
| 90 | + |
| 91 | +1; |
| 92 | +__END__ |
| 93 | +
|
| 94 | +
|
| 95 | +=head1 NAME |
| 96 | +
|
| 97 | +Geo::Details - Module to represent details of an IP returned by the Lite API |
| 98 | +
|
| 99 | +=head1 SYNOPSIS |
| 100 | +
|
| 101 | + use Geo::Details; |
| 102 | +
|
| 103 | + my $data = { |
| 104 | + ip => '169.48.204.140', |
| 105 | + city => 'Dallas', |
| 106 | + country => 'United States', |
| 107 | + country_code => 'US', |
| 108 | + country_flag_url => 'https://example.com/us.png', # URL to the country flag image |
| 109 | + # ... (other attributes) |
| 110 | + }; |
| 111 | +
|
| 112 | + my $geo_details = Geo::Details->new($data); |
| 113 | +
|
| 114 | + print $geo_details->ip; # Output: 169.48.204.140 |
| 115 | + print $geo_details->country_name; # Output: United States |
| 116 | +
|
| 117 | +=head1 DESCRIPTION |
| 118 | +
|
| 119 | +Geo::Details is a simple module that represents details of an IP returned by the Lite API. |
| 120 | +
|
| 121 | +=head1 METHODS |
| 122 | +
|
| 123 | +=head2 new |
| 124 | +
|
| 125 | + my $geo_details = Geo::Details->new($data, $key); |
| 126 | +
|
| 127 | +Creates a new Geo::Details object. If C<$data> is a hash reference, it directly blesses it into the class and returns the object. If C<$data> is a plain string, it creates a new hash reference with the specified key and sets the string value. |
| 128 | +
|
| 129 | +C<$key> is an optional parameter used when C<$data> is a plain string. It defaults to an empty string if not provided. |
| 130 | +
|
| 131 | +=head2 TO_JSON |
| 132 | +
|
| 133 | +This method is used to convert the object to a JSON representation. |
| 134 | +
|
| 135 | +=head2 ip |
| 136 | +
|
| 137 | + my $ip_address = $geo_details->ip(); |
| 138 | +
|
| 139 | +Returns the IP address associated with the details. |
| 140 | +
|
| 141 | +=head2 country_code |
| 142 | +
|
| 143 | + my $country_code = $geo_details->country_code(); |
| 144 | +
|
| 145 | +Returns the ISO 3166-1 alpha-2 code of the country associated with the geographical location. |
| 146 | +
|
| 147 | +=head2 country and country_name |
| 148 | +
|
| 149 | + my $country = $geo_details->country(); |
| 150 | + my $country_name = $geo_details->country_name(); |
| 151 | +
|
| 152 | +Returns the full name of the country associated with the geographical location. |
| 153 | +
|
| 154 | +=head2 country_flag |
| 155 | +
|
| 156 | + my $country_flag_code = $geo_details->country_flag(); |
| 157 | +
|
| 158 | +Returns the ISO 3166-1 alpha-2 code for the country flag associated with the geographical location. |
| 159 | +
|
| 160 | +=head2 country_flag_url |
| 161 | +
|
| 162 | + my $flag_url = $geo_details->country_flag_url(); |
| 163 | +
|
| 164 | +Returns the URL to the country flag image associated with the geographical location. |
| 165 | +
|
| 166 | +=head2 country_currency |
| 167 | +
|
| 168 | + my $currency_code = $geo_details->country_currency(); |
| 169 | +
|
| 170 | +Returns the currency code used in the country associated with the geographical location. |
| 171 | +
|
| 172 | +=head2 continent |
| 173 | +
|
| 174 | + my $continent_name = $geo_details->continent(); |
| 175 | +
|
| 176 | +Returns the continent name of the geographical location. |
| 177 | +
|
| 178 | +=head2 continent_code |
| 179 | +
|
| 180 | + my $continent_code = $geo_details->continent(); |
| 181 | +
|
| 182 | +Returns the continent code of the geographical location. |
| 183 | +
|
| 184 | +=head2 is_eu |
| 185 | +
|
| 186 | + my $is_eu_country = $geo_details->is_eu(); |
| 187 | +
|
| 188 | +Returns true if the country associated with the geographical location is in the European Union (EU). |
| 189 | +
|
| 190 | +=head2 asn |
| 191 | +
|
| 192 | + my $asn_number = $geo_details->asn(); |
| 193 | +
|
| 194 | +Returns the Autonomous System Number (ASN) associated with the IP address. |
| 195 | +
|
| 196 | +=head2 as_name |
| 197 | +
|
| 198 | + my $as_name = $geo_details->as_name(); |
| 199 | +
|
| 200 | +Returns the name of Autonomous System associated with the IP address. |
| 201 | +
|
| 202 | +=head2 as_domain |
| 203 | +
|
| 204 | + my $as_domain = $geo_details->as_domain(); |
| 205 | +
|
| 206 | +Returns the domain of Autonomous System associated with the IP address. |
| 207 | +
|
| 208 | +=head2 meta |
| 209 | +
|
| 210 | + my $meta_data_ref = $geo_details->meta(); |
| 211 | +
|
| 212 | +Returns a reference to the meta-data hash containing additional information about the geographical location. |
| 213 | +
|
| 214 | +=head2 all |
| 215 | +
|
| 216 | + my $all_details_ref = $geo_details->all(); |
| 217 | +
|
| 218 | +Returns a reference to the hash containing all the details of the geographical location. |
| 219 | +
|
| 220 | +=head1 AUTHOR |
| 221 | +
|
| 222 | +Your Name <your.email@example.com> |
| 223 | +
|
| 224 | +=head1 LICENSE |
| 225 | +
|
| 226 | +This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. |
| 227 | +
|
| 228 | +=cut |
| 229 | +
|
| 230 | +# End of Geo::Details |
0 commit comments