Skip to content

Commit

Permalink
Merge pull request #42 from ipinfo/saad/inline-default-country-data
Browse files Browse the repository at this point in the history
Inlined default countries data.
  • Loading branch information
UmanShahzad authored Nov 21, 2023
2 parents e6dd259 + da8b724 commit 5e8fd9b
Show file tree
Hide file tree
Showing 11 changed files with 1,070 additions and 806 deletions.
32 changes: 14 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ hostname = details.hostname # cpe-104-175-221-247.socal.res.rr.com

##### Country Name

`details.country_name` will return the country name, as supplied by the
`countries.json` file. See below for instructions on changing that file for use
`details.country_name` will return the country name, as defined by `DEFAULT_COUNTRY_LIST`
within `countriesData.rb`. See below for instructions on changing that file for use
with non-English languages. `details.country` will still return the country code.

```ruby
Expand All @@ -107,19 +107,19 @@ country_name = details.country_name # United States

##### European Union (EU) Country

`details.is_eu` will return `true` if the country is a member of the European Union (EU), as supplied by the `eu.json` file.
`details.is_eu` will return `true` if the country is a member of the European Union (EU)
, as defined by `DEFAULT_EU_COUNTRIES_LIST` within `countriesData.rb`.

```ruby
is_eu = details.is_eu # false
```

It is possible to change the file by setting the `eu_countries` setting when creating the `IPinfo` object.

The file must be a `.json` file with the [following structure](lib/ipinfo/eu.json).

##### Country Flag

`details.country_flag` will return `emoji` and `unicode` of a country's flag, as supplied by the `eu.json` file.
`details.country_flag` will return `emoji` and `unicode` of a country's flag, as defined by
`DEFAULT_COUNTRIES_FLAG_LIST` within `countriesData.rb`.

```ruby
country_flag = details.country_flag # {"emoji"=>"🇺🇸", "unicode"=>"U+1F1FA U+1F1F8"}
Expand All @@ -137,7 +137,8 @@ country_flag = details.country_flag_url # {"https://cdn.ipinfo.io/static/images/

##### Country Currency

`details.country_currency` will return `code` and `symbol` of a country's currency, as supplied by the `currency.json` file.
`details.country_currency` will return `code` and `symbol` of a country's currency, as defined by
`DEFAULT_COUNTRIES_CURRENCIES_LIST` within `countriesData.rb`.

```ruby
country_currency = details.country_currency # {"code"=>"USD", "symbol"=>"$"}
Expand All @@ -147,11 +148,10 @@ country_currency_symbol = details.country_currency['symbol'] # $

It is possible to change the file by setting the `countries_currencies` setting when creating the `IPinfo` object.

The file must be a `.json` file with the [following structure](lib/ipinfo/currency.json).

##### Continent

`details.continent` will return `code` and `name` of the continent, as supplied by the `continent.json` file.
`details.continent` will return `code` and `name` of the continent, as defined by
`DEFAULT_CONTINENT_LIST` within `countriesData.rb`.

```ruby
continent = details.continent # {"code"=>"NA", "name"=>"North America"}
Expand All @@ -161,8 +161,6 @@ continent_name = details.continent['name'] # North America

It is possible to change the file by setting the `continents` setting when creating the `IPinfo` object.

The file must be a `.json` file with the [following structure](lib/ipinfo/continent.json).

#### IP Address

`details.ip_address` will return the `IPAddr` object from the
Expand Down Expand Up @@ -273,14 +271,12 @@ When looking up an IP address, the response object includes a
American English. It is possible to return the country name in other languages
by setting the `countries` setting when creating the `IPinfo` object.

The file must be a `.json` file with the following structure:

```
{
"BD": "Bangladesh",
"BE": "Belgium",
"BF": "Burkina Faso",
"BG": "Bulgaria"
"BD" => "Bangladesh",
"BE" => "Belgium",
"BF" => "Burkina Faso",
"BG" => "Bulgaria"
...
}
```
Expand Down
33 changes: 7 additions & 26 deletions lib/ipinfo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,18 @@
require 'ipinfo/version'
require 'json'
require_relative 'ipinfo/ipAddressMatcher'
require_relative 'ipinfo/countriesData'

module IPinfo
include CountriesData
DEFAULT_CACHE_MAXSIZE = 4096
DEFAULT_CACHE_TTL = 60 * 60 * 24
DEFAULT_COUNTRY_FILE = File.join(File.dirname(__FILE__),
'ipinfo/countries.json')
DEFAULT_EU_COUNTRIES_FILE = File.join(File.dirname(__FILE__),
'ipinfo/eu.json')
DEFAULT_COUNTRIES_FLAG_FILE = File.join(File.dirname(__FILE__),
'ipinfo/flags.json')
DEFAULT_COUNTRIES_CURRENCIES_FILE = File.join(File.dirname(__FILE__),
'ipinfo/currency.json')
DEFAULT_CONTINENT_FILE = File.join(File.dirname(__FILE__),
'ipinfo/continent.json')
RATE_LIMIT_MESSAGE = 'To increase your limits, please review our ' \
'paid plans at https://ipinfo.io/pricing'
# Base URL to get country flag image link.
# "PK" -> "https://cdn.ipinfo.io/static/images/countries-flags/PK.svg"
COUNTRY_FLAGS_URL = "https://cdn.ipinfo.io/static/images/countries-flags/"


class << self
def create(access_token = nil, settings = {})
IPinfo.new(access_token, settings)
Expand All @@ -48,16 +39,11 @@ def initialize(access_token = nil, settings = {})
maxsize = settings.fetch('maxsize', DEFAULT_CACHE_MAXSIZE)
ttl = settings.fetch('ttl', DEFAULT_CACHE_TTL)
@cache = settings.fetch('cache', DefaultCache.new(ttl, maxsize))
@countries = prepare_json(settings.fetch('countries',
DEFAULT_COUNTRY_FILE))
@eu_countries = prepare_json(settings.fetch('eu_countries',
DEFAULT_EU_COUNTRIES_FILE))
@countries_flags = prepare_json(settings.fetch('countries_flags',
DEFAULT_COUNTRIES_FLAG_FILE))
@countries_currencies = prepare_json(settings.fetch('countries_currencies',
DEFAULT_COUNTRIES_CURRENCIES_FILE))
@continents = prepare_json(settings.fetch('continents',
DEFAULT_CONTINENT_FILE))
@countries = settings.fetch('countries', DEFAULT_COUNTRY_LIST)
@eu_countries = settings.fetch('eu_countries', DEFAULT_EU_COUNTRIES_LIST)
@countries_flags = settings.fetch('countries_flags', DEFAULT_COUNTRIES_FLAG_LIST)
@countries_currencies = settings.fetch('countries_currencies', DEFAULT_COUNTRIES_CURRENCIES_LIST)
@continents = settings.fetch('continents', DEFAULT_CONTINENT_LIST)
end

def details(ip_address = nil)
Expand Down Expand Up @@ -179,11 +165,6 @@ def prepare_http_client(httpc = nil)
end
end

def prepare_json(filename)
file = File.read(filename)
JSON.parse(file)
end

private

def isBogon(ip)
Expand Down
2 changes: 1 addition & 1 deletion lib/ipinfo/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def connection(adapter)

def default_headers
headers = {
'User-Agent' => 'IPinfoClient/Ruby/1.1.0',
'User-Agent' => 'IPinfoClient/Ruby/2.0.0',
'Accept' => 'application/json'
}
headers['Authorization'] = "Bearer #{CGI.escape(token)}" if token
Expand Down
Loading

0 comments on commit 5e8fd9b

Please sign in to comment.