diff --git a/lib/app/star.ex b/lib/app/star.ex index 3bdfb52..4b2e0eb 100644 --- a/lib/app/star.ex +++ b/lib/app/star.ex @@ -40,7 +40,7 @@ defmodule App.Star do # We have multiple repos over 1k stars # Therefore issuing all these requests at once # would instantly hit the 5k/h GitHub API Request Limit - App.User.get_user_from_api(user) + App.User.create_user_with_hex(user) {:ok, star} = create(%{ user_id: user.id, repo_id: repo_id }) diff --git a/lib/app/user.ex b/lib/app/user.ex index ab468cc..0ed9e44 100644 --- a/lib/app/user.ex +++ b/lib/app/user.ex @@ -46,36 +46,37 @@ defmodule App.User do data = App.GitHub.user(user.login) # Not super happy about this crude error handling ... feel free to refactor. if Map.has_key?(data, :status) && data.status == "404" do - # IO.inspect(" - - - - - - - - - - - - - - - - - #{user.login}") - # dbg(user) - # IO.inspect(" - - - - - - - - - - - - - - - - - - - - - - - ") {:ok, user} = dummy_data(user) |> create() user else - {:ok, user} = + create_user_with_hex(data) + end + end + + def create_user_with_hex(data) do + {:ok, user} = map_github_user_fields_to_table(data) |> Map.put(:hex, App.Img.get_avatar_color(data.avatar_url)) |> create() - user - end + user end # tidy data before insertion def map_github_user_fields_to_table(u) do Map.merge(u, %{ avatar_url: String.split(u.avatar_url, "?") |> List.first, - company: clean_company(u.company), + company: clean_company(u), }) end - def clean_company(company) do + def clean_company(u) do # avoid `String.replace(nil, "@", "", [])` error - if company == nil do + if not Map.has_key?(u, :company) or u.company == nil do "" else - String.replace(company, "@", "") + String.replace(u.company, "@", "") end end diff --git a/lib/app_web/live/app_live.ex b/lib/app_web/live/app_live.ex index 80fb131..8f9adc2 100644 --- a/lib/app_web/live/app_live.ex +++ b/lib/app_web/live/app_live.ex @@ -48,7 +48,7 @@ defmodule AppWeb.AppLive do App.Repository.get_org_repos(org) # get all stargazers for a given repo |> Enum.map(fn repo -> - # dbg(repo) + dbg(repo) # App.Star.get_stargazers_for_repo(owner, repo) repo end)