Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ First, add Excountries to your `mix.exs` dependencies:

```elixir
def deps do
[{:excountries, "~> 0.0.1"}]
[{:excountries, "~> 0.0.4"}]
end
```

and run `$ mix deps.get`.
and run `$ mix deps.get`.

## Usage

Expand Down Expand Up @@ -74,15 +74,15 @@ Searches for the country by it's calling code:
Excountries.Radar.by_calling_code("01")
```

### Region
### Region

Searches for the country by it's region:

```elixir
Excountries.Radar.by_region("Oceania")
```

### Subregion
### Subregion

Searches for the country by it's subregion:

Expand All @@ -98,25 +98,47 @@ Searches for the country by it's country code:
Excountries.Radar.by_country_code("MKD")
```

### Regional bloc

Searches for the country by it's regional bloc:

```elixir
Excountries.Radar.by_regional_bloc("eu")
```

## ```Country```

```%Excountries.Country``` is a struct containing multiple properties:

- name
- topLevelDomain
- alpha2Code
- alpha3Code
- callingCodes
- capital
- relevance
- altSpellings
- region
- subregion
- population
- latitude
- longitude
- latlng
- demonym
- area
- gini
- timezones
- borders
- nativeName
- topLevelDomain
- numericCode
- currencies
- languages
- translations
- flag
- regionalBlocs
- cioc

## News

- **2017/10/23**
- Added support to API v2 (Thanks to Pedro Vieira)

## Contributing

Expand Down
26 changes: 23 additions & 3 deletions lib/excountries/api.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
defmodule Excountries.API do
@url "https://restcountries.eu/rest/v1"
@url "https://restcountries.eu/rest/v2"

def call path do
%{ body: body, headers: _headers, status_code: status_code } = HTTPoison.get! "#{@url}#{path}"
def call(path, fields) do
%{ body: body, headers: _headers, status_code: status_code } =
case fields == nil do
true ->
HTTPoison.get!("#{@url}#{path}")
false ->
HTTPoison.get!("#{@url}#{path}", [], [params: [{"fields", fields}]])
end
case status_code do
200 -> body
_ -> throw "Not found."
end
end

def call_fulltext(path, fields) do
%{ body: body, headers: _headers, status_code: status_code } =
case fields == nil do
true ->
HTTPoison.get!("#{@url}#{path}", [], [params: [{"fullText", true}]])
false ->
HTTPoison.get!("#{@url}#{path}", [], [params: [{"fullText", true}, {"fields", fields}]])
end
case status_code do
200 -> body
_ -> throw "Not found."
Expand Down
29 changes: 26 additions & 3 deletions lib/excountries/country.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
defmodule Excountries.Country do
@derive [Poison.Encoder]
defstruct [:name, :capital, :relevance, :region, :subregion, :population,
:latitude, :longitude, :demonym, :area, :timezones, :nativeName, :topLevelDomain,
:currencies, :languages]
defstruct [
:name,
:topLevelDomain,
:alpha2Code,
:alpha3Code,
:callingCodes,
:capital,
:altSpellings,
:region,
:subregion,
:population,
:latlng,
:demonym,
:area,
:gini,
:timezones,
:borders,
:nativeName,
:numericCode,
:currencies,
:languages,
:translations,
:flag,
:regionalBlocs,
:cioc
]
end

58 changes: 36 additions & 22 deletions lib/excountries/radar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ defmodule Excountries.Radar do
* by_country_code
* by_region
* by_subregion
* by_regional_bloc
"""

alias Excountries.API

@doc """
Returns all of the countries in a list.

```
Excountries.Radar.all()
```
"""
def all(client \\ Excountries.API) do
client.call("/all") |> Poison.decode!(as: [Excountries.Country])
def all(fields \\ nil) do
API.call("/all", fields) |> Poison.decode!(as: [Excountries.Country])
end

@doc """
Expand All @@ -34,21 +37,21 @@ defmodule Excountries.Radar do
Excountries.Radar.by_full_name("United States Of America")
```
"""
def by_full_name(name, client \\ Excountries.API) do
def by_full_name(name, fields \\ nil) do
name = String.downcase name
client.call("/name/#{name}?fullText=true") |> Poison.decode!(as: [Excountries.Country]) |> List.first
API.call_fulltext("/name/#{name}", fields) |> Poison.decode!(as: [Excountries.Country]) |> List.first
end

@doc """
Searches for a country by a substring of it's name or abbreviation:

```
Excountries.Radar.by_name("USA")
```
"""
def by_name(name, client \\ Excountries.API) do
def by_name(name, fields \\ nil) do
name = String.downcase name
client.call("/name/#{name}") |> Poison.decode!(as: [Excountries.Country])
API.call("/name/#{name}", fields) |> Poison.decode!(as: [Excountries.Country])
end

@doc """
Expand All @@ -58,9 +61,9 @@ defmodule Excountries.Radar do
Excountries.Radar.by_language("en")
```
"""
def by_language(lang, client \\ Excountries.API) do
Excountries.LanguageValidator.validate!(lang)
client.call("/lang/#{lang}") |> Poison.decode!(as: [Excountries.Country])
def by_language(lang, fields \\ nil) do
Excountries.LanguageValidator.validate!(lang)
API.call("/lang/#{lang}", fields) |> Poison.decode!(as: [Excountries.Country])
end

@doc """
Expand All @@ -70,8 +73,8 @@ defmodule Excountries.Radar do
Excountries.Radar.by_currency("USD")
```
"""
def by_currency(currency, client \\ Excountries.API) do
client.call("/currency/#{currency}") |> Poison.decode!(as: [Excountries.Country])
def by_currency(currency, fields \\ nil) do
API.call("/currency/#{currency}", fields) |> Poison.decode!(as: [Excountries.Country])
end

@doc """
Expand All @@ -81,8 +84,8 @@ defmodule Excountries.Radar do
Excountries.Radar.by_capital("USD")
```
"""
def by_capital(capital, client \\ Excountries.API) do
client.call("/capital/#{capital}") |> Poison.decode!(as: [Excountries.Country]) |> List.first
def by_capital(capital, fields \\ nil) do
API.call("/capital/#{capital}", fields) |> Poison.decode!(as: [Excountries.Country]) |> List.first
end

@doc """
Expand All @@ -92,8 +95,8 @@ defmodule Excountries.Radar do
Excountries.Radar.by_calling_code("01")
```
"""
def by_calling_code(code, client \\ Excountries.API) do
client.call("/calling_code/#{code}") |> Poison.decode!(as: [Excountries.Country])
def by_calling_code(code, fields \\ nil) do
API.call("/calling_code/#{code}", fields) |> Poison.decode!(as: [Excountries.Country])
end

@doc """
Expand All @@ -103,8 +106,8 @@ defmodule Excountries.Radar do
Excountries.Radar.by_region("Oceania")
```
"""
def by_region(region, client \\ Excountries.API) do
client.call("/region/#{region}") |> Poison.decode!(as: [Excountries.Country])
def by_region(region, fields \\ nil) do
API.call("/region/#{region}", fields) |> Poison.decode!(as: [Excountries.Country])
end

@doc """
Expand All @@ -114,8 +117,8 @@ defmodule Excountries.Radar do
Excountries.Radar.by_subregion("Polynesia")
```
"""
def by_subregion(subregion, client \\ Excountries.API) do
client.call("/subregion/#{subregion}") |> Poison.decode!(as: [Excountries.Country])
def by_subregion(subregion, fields \\ nil) do
API.call("/subregion/#{subregion}", fields) |> Poison.decode!(as: [Excountries.Country])
end

@doc """
Expand All @@ -125,7 +128,18 @@ defmodule Excountries.Radar do
Excountries.Radar.by_country_code("MKD")
```
"""
def by_country_code(code, client \\ Excountries.API) do
client.call("/alpha/#{code}") |> Poison.decode!(as: [Excountries.Country])
def by_country_code(code, fields \\ nil) do
API.call("/alpha/#{code}", fields) |> Poison.decode!(as: [Excountries.Country])
end

@doc """
Searches for the country by it's regional bloc:

```
Excountries.Radar.by_regional_bloc("eu")
```
"""
def by_regional_bloc(regionalbloc, fields \\ nil) do
API.call("/regionalbloc/#{regionalbloc}", fields) |> Poison.decode!(as: [Excountries.Country])
end
end
26 changes: 7 additions & 19 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,23 @@ defmodule Excountries.Mixfile do

def project do
[app: :excountries,
version: "0.0.3",
elixir: "~> 1.0",
version: "0.0.4",
elixir: "~> 1.5",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
description: description,
package: package,
deps: deps]
description: description(),
package: package(),
deps: deps()]
end

# Configuration for the OTP application
#
# Type `mix help compile.app` for more information
def application do
[applications: [:logger, :httpoison]]
end

# Dependencies can be Hex packages:
#
# {:mydep, "~> 0.3.0"}
#
# Or git/path repositories:
#
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
#
# Type `mix help deps` for more examples and options
defp deps do
[
{:httpoison, "~> 0.7.2"},
{:poison, "~> 1.5"}
{:httpoison, "~> 0.13.0"},
{:poison, "~> 3.1"}
]
end

Expand Down
14 changes: 9 additions & 5 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
%{"hackney": {:hex, :hackney, "1.3.2"},
"httpoison": {:hex, :httpoison, "0.7.4"},
"idna": {:hex, :idna, "1.0.2"},
"poison": {:hex, :poison, "1.5.0"},
"ssl_verify_hostname": {:hex, :ssl_verify_hostname, "1.0.5"}}
%{"certifi": {:hex, :certifi, "2.0.0", "a0c0e475107135f76b8c1d5bc7efb33cd3815cb3cf3dea7aefdd174dabead064", [], [], "hexpm"},
"hackney": {:hex, :hackney, "1.10.1", "c38d0ca52ea80254936a32c45bb7eb414e7a96a521b4ce76d00a69753b157f21", [:rebar3], [{:certifi, "2.0.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"httpoison": {:hex, :httpoison, "0.13.0", "bfaf44d9f133a6599886720f3937a7699466d23bb0cd7a88b6ba011f53c6f562", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [], [], "hexpm"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [], [], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.3.1", "a1f612a7b512638634a603c8f401892afbf99b8ce93a45041f8aaca99cadb85e", [], [], "hexpm"}}
Loading