Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the get function to let user find country by name #56

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
30 changes: 26 additions & 4 deletions lib/countries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,40 @@ defmodule Countries do
end

@doc """
Returns one country given is alpha2 country code.
Returns one country by given name

## Examples

iex> %Countries.Country{name: name} = Countries.get("PL")
iex> %Countries.Country{alpha2: alpha2} = Countries.get("Poland")
iex> alpha2
"PL"
"""

def get(attrs) when is_bitstring(attrs) do
country = filter_by(:name, attrs)

case length(country) do
0 ->
[]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The returned value is not consistent and breaking the current API.

We could update it to adopt the following pattern:

{:ok, country}
:not_found pattern

And also add a get! function that breaks if country does not exist.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I will do ASAP

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, you can change the whole case in this way:

case country do
  [] -> []
  [country_info | _] -> country_info
end


1 ->
List.first(country)
end
end

@doc """
Returns one country by given alpha2 country code

## Examples

iex> %Countries.Country{name: name} = Countries.get_by_alpha2("PL")
iex> name
"Poland"

"""

def get(country_code) do
[country] = filter_by(:alpha2, country_code)
def get_by_alpha2(attrs) when bit_size(attrs) == 16 do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because you're using a strong match, you should add a ! indicating that if the country isn't found it's going to fail.

def get_by_alpha2!(<<_ :: binary-size(16)>> = attrs) do
  hd(filter_by(:alpha2, attrs))
end

def get_by_alpha2(<<_ :: binary-size(16)>> = attrs) do
  case filter_by(:alpha2, attrs) do
    [] -> nil
    [country] -> country
  end
end

[country] = filter_by(:alpha2, attrs)
country
end

Expand Down
14 changes: 12 additions & 2 deletions test/countries_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ defmodule CountriesTest do
end

describe "get/1" do
test "gets one country" do
%{alpha2: "GB"} = Countries.get("GB")
test "gets one country by country name" do
%{alpha2: "BR"} = Countries.get("Brazil")
end

test "gets empty string with invalid country name" do
[] = Countries.get("Bra")
end
end

describe "get_by_alpha2/1" do
test "gets one country by country code" do
%{alpha2: "GB"} = Countries.get_by_alpha2("GB")
end
end

Expand Down