Last active
September 20, 2016 04:44
-
-
Save bryansray/6ad00d0792374fea68f7e64ccc7c1636 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Questions: | |
# 1. How can I specify that path for the cached item by something other than the URL? | |
# 2. I'd like to use pieces in the Response to generate the cache location, but then, how do I check if the cached file exists before making the API call? | |
defmodule Base | |
use HTTPoison.Base | |
# ... Basic HTTPoison Implementation ... | |
end | |
defmodule API | |
def get!(url) | |
Base.API.get!(url) | |
|> Map.get(:body) | |
|> Map.new | |
end | |
end | |
defmodule Character | |
def get!(url), do: API.get!(url) | |
def get(realm, character), do: get_url(realm, name) |> get! | |
end | |
defmodule Guild | |
def get!(url), do: API.get!(url) | |
def get(realm, name), do: get_url(realm, name) |> get! | |
end | |
# Caching Implementation ... ? | |
defmodule Exarmory.Cache do | |
defstruct [:region, :realm, :name] | |
def get!(module, url, %Exarmory.Cache{ } = cache) do | |
location = module.storage(cache) <> ".json" |> String.downcase | |
# OR use URL if module.storage/1 is not provided | |
case File.read(location) do | |
{ :ok, content } -> | |
IO.puts("Found cache ...") | |
content | |
{ :error, _ } -> | |
IO.puts("Making API call ...") | |
# Exarmory.API.get!(url) | |
end | |
end | |
end | |
Cache.get!(url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use
Cache.get!
as your primary interface instead ofAPI.get!
: