Hi all
I wrote two modules, that looks pretty much the same:
defmodule Busiket.Repo.Seeders.CountryCode do
alias Busiket.Repo
alias Busiket.CountryCode
alias Poison.Parser
def insert(lc) when is_list(lc) do
lc
|> Enum.each(fn(ele) -> Repo.insert!(ele) end)
end
def delete() do
Repo.delete_all()
end
def convert_json_struct(filename) when is_binary(filename) do
filename
|> query_json_file!
|> convert_json_language_code
end
defp query_json_file!(filename) do
case File.read(filename) do
{:ok, values} ->
values
{:error, reason} ->
raise reason
end
end
defp convert_json_language_code(json) when is_bitstring(json) do
json
|> Parser.parse!
|> Enum.map(fn(ele) -> %CountryCode{alpha2: ele["alpha-2"], alpha3: ele["alpha-3"]} end)
end
end
and
defmodule Busiket.Repo.Seeders.LanguageCode do
alias Busiket.Repo
alias Busiket.LanguageCode
alias Poison.Parser
def insert(lc) when is_list(lc) do
lc
|> Enum.each(fn(ele) -> Repo.insert!(ele) end)
end
def delete() do
Repo.delete_all()
end
def convert_json_struct(filename) when is_binary(filename) do
filename
|> query_json_file!
|> convert_json_language_code
end
defp query_json_file!(filename) do
case File.read(filename) do
{:ok, values} ->
values
{:error, reason} ->
raise reason
end
end
defp convert_json_language_code(json) when is_bitstring(json) do
json
|> Parser.parse!
|> Enum.map(fn(ele) -> %LanguageCode{code: ele["alpha2"], text: ele["English"]} end)
end
end
I feel like, the codes look pretty much so same. How can I make more clean?
Thanks