defmodule OpenMirego.Repo.Fetcher do
  alias Poison, as: JSON
  alias OpenMirego.Repo.Serializer

  @user_agent {:"User-Agent", "Open Mirego Website"}
  @api_key Application.get_env(:github, OpenMirego.Router)[:api_key]
  @auth "#{@api_key}:x-oauth-basic"
  @org "mirego"
  @url "https://#{@auth}@api.github.com/orgs/#{@org}/repos?type=public&per_page=100"

  def fetch do
    cached_body
    |> JSON.decode!
    |> Serializer.serialize
  end

  defp cached_body do
    ConCache.get_or_store(:github, "#{@org}:repos", &body/0)
  end

  defp body do
    case HTTPotion.get(@url, [@user_agent]) do
      %HTTPotion.Response{status_code: 200, body: body} ->
        body
      _ ->
        "{}"
    end
  end
end

defmodule OpenMirego.Repo.Fetcher do
  alias Poison, as: JSON
  alias OpenMirego.Repo.Serializer

  @user_agent {:"User-Agent", "Open Mirego Website"}
  @org "mirego"

  def fetch do
    cached_body
    |> JSON.decode!
    |> Serializer.serialize
  end

  defp cached_body do
    ConCache.get_or_store(:github, "#{@org}:repos", &body/0)
  end

  defp body do
    case HTTPotion.get(url, [@user_agent]) do
      %HTTPotion.Response{status_code: 200, body: body} ->
        body
      _error ->
        "[]"
    end
  end

  defp url, do: "https://#{auth}@api.github.com/orgs/#{@org}/repos?type=public&per_page=100"
  defp auth, do: "#{Application.get_env(:github, OpenMirego.Router)[:api_key]}:x-oauth-basic"
end

Move Application.get_env out of module variable evaluated before env
Move Application.get_env out of module variable evaluated before env

mirego/mirego-open-web,mirego/mirego-open-web