Skip to content

Commit

Permalink
Merge pull request #15 from ipinfo/usama/add-country-flag-url
Browse files Browse the repository at this point in the history
Add country flag url
  • Loading branch information
UmanShahzad authored Aug 15, 2023
2 parents f4a0a49 + 556b913 commit 6ba95f5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 38 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ Add this line to your application's `rebar.config`:
<<"ZM">> =>
#{<<"emoji">> => <<240,159,135,191,240,159,135,178>>,
<<"unicode">> => <<"U+1F1FF U+1F1F2">>},...},
country_flag_base_url =>
<<"https://cdn.ipinfo.io/static/images/countries-flags/">>,
eu_countries =>
[<<"IE">>,<<"AT">>,<<"LT">>,<<"LU">>,<<"LV">>,<<"DE">>,
<<"DK">>,<<"SE">>,<<"SI">>,<<"SK">>,<<"CZ">>,<<"CY">>,
Expand All @@ -251,6 +253,8 @@ Add this line to your application's `rebar.config`:
country_flag =>
#{<<"emoji">> => <<240,159,135,186,240,159,135,184>>,
<<"unicode">> => <<"U+1F1FA U+1F1F8">>},
country_flag_url =>
<<"https:/cdn.ipinfo.io/static/images/countries-flags/US.svg">>,
country_name => <<"United States">>,
hostname => <<"dns.google">>,ip => <<"8.8.8.8">>,
is_eu => false,latitude => <<"37.4056">>,
Expand Down
88 changes: 50 additions & 38 deletions src/ipinfo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,42 @@
-define(DEFAULT_COUNTRY_FLAG_FILE, "flags.json").
-define(DEFAULT_COUNTRY_CURRENCY_FILE, "currency.json").
-define(DEFAULT_CONTINENT_FILE, "continent.json").
-define(DEFAULT_COUNTRY_FLAG_BASE_URL, <<"https://cdn.ipinfo.io/static/images/countries-flags/">>).
-define(DEFAULT_BASE_URL, <<"https://ipinfo.io">>).
-define(DEFAULT_TIMEOUT, timer:seconds(5)).
-define(DEFAULT_CACHE_TTL_SECONDS, (24 * 60 * 60)).

-export_type([t/0]).

-type t() :: #{
'__struct__' := ?MODULE,
access_token := nil | binary(),
base_url := nil | binary(),
timeout := nil | timeout(),
cache := nil | pid(),
countries := map(),
countries_flags := map(),
countries_currencies := map(),
continents := map(),
eu_countries := list()
'__struct__' := ?MODULE,
access_token := nil | binary(),
base_url := nil | binary(),
timeout := nil | timeout(),
cache := nil | pid(),
countries := map(),
countries_flags := map(),
country_flag_base_url := nil | binary(),
countries_currencies := map(),
continents := map(),
eu_countries := list()
}.

-spec new() -> t().
%% @private
new() ->
#{
'__struct__' => ?MODULE,
access_token => nil,
base_url => nil,
timeout => nil,
cache => nil,
countries => #{},
countries_currencies => #{},
countries_flags => #{},
continents => #{},
eu_countries => []
'__struct__' => ?MODULE,
access_token => nil,
base_url => nil,
timeout => nil,
cache => nil,
countries => #{},
countries_currencies => #{},
countries_flags => #{},
country_flag_base_url => nil,
continents => #{},
eu_countries => []
}.

-spec '__struct__'() -> t().
Expand Down Expand Up @@ -94,6 +97,7 @@ create(AccessToken, Settings) ->
filename:join(code:priv_dir(ipinfo), ?DEFAULT_COUNTRY_CURRENCY_FILE)),
ContinentsFile = get_config(continents, Settings,
filename:join(code:priv_dir(ipinfo), ?DEFAULT_CONTINENT_FILE)),
CountryFlagBaseUrl = get_config(country_flag_base_url, Settings, ?DEFAULT_COUNTRY_FLAG_BASE_URL),
BaseUrl = get_config(base_url, Settings, ?DEFAULT_BASE_URL),
Timeout = get_config(timeout, Settings, ?DEFAULT_TIMEOUT),
CacheTtl = get_config(cache_ttl, Settings, ?DEFAULT_CACHE_TTL_SECONDS),
Expand All @@ -109,27 +113,28 @@ create(AccessToken, Settings) ->
{ok, Continents} ->
{ok, Cache} = ipinfo_cache:create(CacheTtl),
{ok, new(#{
access_token => AccessToken,
base_url => BaseUrl,
timeout => Timeout,
cache => Cache,
countries => Countries,
eu_countries => EuCountries,
countries_flags => CountriesFlags,
countries_currencies => CountriesCurrencies,
continents => Continents
access_token => AccessToken,
base_url => BaseUrl,
timeout => Timeout,
cache => Cache,
countries => Countries,
eu_countries => EuCountries,
countries_flags => CountriesFlags,
country_flag_base_url => CountryFlagBaseUrl,
countries_currencies => CountriesCurrencies,
continents => Continents
})};
{error, Error} ->
{error, Error}
end;
end;
{error, Error} ->
{error, Error}
end;
end;
{error, Error} ->
{error, Error}
end;
{error, Error} ->
{error, Error}
{error, Error}
end;
{error, Reason} ->
{error, Reason}
Expand All @@ -138,21 +143,22 @@ create(AccessToken, Settings) ->
details(IpInfo) ->
details(IpInfo, nil).

details(#{cache := Cache,
countries := Countries,
eu_countries := EuCountries,
countries_flags := CountriesFlags,
details(#{cache := Cache,
countries := Countries,
eu_countries := EuCountries,
countries_flags := CountriesFlags,
country_flag_base_url:= CountryFlagBaseUrl,
countries_currencies := CountriesCurrencies,
continents := Continents
} = IpInfo, IpAddress) ->
case ipinfo_cache:get(Cache, IpAddress) of
{ok, Details} ->
{ok, put_geo(put_country_name(put_is_eu(put_country_flag(put_country_currency(put_continent(Details, Continents),CountriesCurrencies), CountriesFlags), EuCountries), Countries))};
{ok, put_geo(put_country_name(put_is_eu(put_country_flag_url(put_country_flag(put_country_currency(put_continent(Details, Continents),CountriesCurrencies), CountriesFlags),CountryFlagBaseUrl), EuCountries), Countries))};
error ->
case ipinfo_http:request_details(IpInfo, IpAddress) of
{ok, Details} ->
ok = ipinfo_cache:add(Cache, IpAddress, Details),
{ok, put_geo(put_country_name(put_is_eu(put_country_flag(put_country_currency(put_continent(Details, Continents),CountriesCurrencies), CountriesFlags), EuCountries), Countries))};
{ok, put_geo(put_country_name(put_is_eu(put_country_flag_url(put_country_flag(put_country_currency(put_continent(Details, Continents),CountriesCurrencies), CountriesFlags),CountryFlagBaseUrl), EuCountries), Countries))};
{error, Reason} ->
{error, Reason}
end
Expand All @@ -178,6 +184,12 @@ put_country_flag(#{country := Country} = Details, CountriesFlags) ->
put_country_flag(Details, _CountriesFlags) ->
Details.

put_country_flag_url(#{country := Country} = Details, CountryFlagBaseUrl) ->
CountryFlagUrl = filename:join(CountryFlagBaseUrl, binary_to_list(Country) ++ ".svg"),
maps:put(country_flag_url, CountryFlagUrl, Details);
put_country_flag_url(Details, _CountryFlagBaseUrl) ->
Details.

put_country_currency(#{country := Country} = Details, CountriesCurrencies) ->
case maps:find(Country, CountriesCurrencies) of
{ok, CountryCurrency} ->
Expand Down

0 comments on commit 6ba95f5

Please sign in to comment.