-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from johannesE/master
Update dependencies and add an hourly forecast
- Loading branch information
Showing
20 changed files
with
458 additions
and
78 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# To get started with Dependabot version updates, you'll need to specify which | ||
# package ecosystems to update and where the package manifests are located. | ||
# Please see the documentation for all configuration options: | ||
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates | ||
|
||
version: 2 | ||
updates: | ||
- package-ecosystem: "mix" # See documentation for possible values | ||
directory: "/" # Location of package manifests | ||
schedule: | ||
interval: "weekly" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: Reformat elixir | ||
on: push | ||
|
||
jobs: | ||
run: | ||
name: Reformat elixir | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repo | ||
uses: actions/checkout@v2 | ||
- uses: actions/cache@v3 | ||
with: | ||
path: | | ||
deps | ||
_build | ||
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }} | ||
restore-keys: | | ||
${{ runner.os }}-mix- | ||
- name: Setup elixir | ||
uses: erlef/setup-beam@v1 | ||
with: | ||
otp-version: 24.0 | ||
elixir-version: 1.13.4 | ||
- name: Install mix dependecies | ||
run: mix deps.get | ||
|
||
- name: Compile app (required for the formatter plugin) | ||
run: mix compile | ||
|
||
- name: Format with mix format | ||
run: mix format | ||
|
||
- name: Commit mix format code changes | ||
uses: EndBug/add-and-commit@v9 | ||
with: | ||
author_name: "${{ github.event.pusher.name }}" | ||
author_email: "${{ github.event.pusher.email }}" | ||
message: 'Fix formatting of elixir code with mix format' | ||
add: 'lib' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: Test elixir | ||
on: push | ||
|
||
jobs: | ||
run: | ||
name: Test elixir | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repo | ||
uses: actions/checkout@v2 | ||
- uses: actions/cache@v3 | ||
with: | ||
path: | | ||
deps | ||
_build | ||
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }} | ||
restore-keys: | | ||
${{ runner.os }}-mix- | ||
- name: Setup elixir | ||
uses: erlef/setup-beam@v1 | ||
with: | ||
otp-version: 24.0 | ||
elixir-version: 1.13.4 | ||
- name: Install mix dependecies | ||
run: mix deps.get --only test | ||
|
||
- name: mix test | ||
run: mix test | ||
env: | ||
OWM_API_KEY: ${{ secrets.OWM_API_KEY }} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
elixir 1.9 | ||
elixir 1.12.1 |
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule ExOwm.HourlyForecast.Coordinator do | ||
@moduledoc """ | ||
This module is a GenServer implementation created for handling concurrent | ||
worker task coordination. | ||
""" | ||
use GenServer | ||
alias ExOwm.HourlyForecast.Worker | ||
|
||
## Client API | ||
def start_link(options \\ []) do | ||
GenServer.start_link(__MODULE__, %{}, options ++ [name: :hourly_forecast_coordinator]) | ||
end | ||
|
||
def get_weather(locations, opts) do | ||
GenServer.call(:hourly_forecast_coordinator, {:get_hourly_forecast, locations, opts}) | ||
end | ||
|
||
## Server implementation | ||
def init(_) do | ||
{:ok, %{}} | ||
end | ||
|
||
def handle_call({:get_state}, _from, state) do | ||
{:reply, state, state} | ||
end | ||
|
||
def handle_call({:get_hourly_forecast, locations, opts}, _from, _state) do | ||
spawn_worker_tasks(locations, opts) | ||
end | ||
|
||
defp spawn_worker_tasks(locations, opts) do | ||
worker_tasks = | ||
Enum.map(locations, fn location -> | ||
Task.async(Worker, :get_hourly_forecast, [location, opts]) | ||
end) | ||
|
||
results = Enum.map(worker_tasks, fn task -> Task.await(task) end) | ||
{:reply, results, results} | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
defmodule ExOwm.HourlyForecast.Worker do | ||
@moduledoc """ | ||
Five Day Forecast Worker task implementation. | ||
""" | ||
alias ExOwm.Api | ||
alias ExOwm.Cache | ||
Check warning on line 6 in lib/ex_owm/hourly_forecast/worker.ex
|
||
|
||
@doc """ | ||
Returns five day weather forecast for a specific location and given options. | ||
Checks whether request has been already cached, if not it sends the request to | ||
OWM API and caches it with specific TTL. | ||
""" | ||
@spec get_hourly_forecast(map, key: atom) :: map | ||
def get_hourly_forecast(location, opts) do | ||
ExOwm.WorkerHelper.get_from_cache_or_call("hourly_forecast: #{inspect(location)}", fn -> | ||
Api.send_and_parse_request(:get_hourly_forecast, location, opts) | ||
end) | ||
end | ||
end |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule ExOwm.WorkerHelper do | ||
@moduledoc false | ||
alias ExOwm.Cache | ||
|
||
@spec get_from_cache_or_call(String.t(), fun(), pos_integer()) :: | ||
{:ok, map()} | {:error, map()} | {:error, map(), map()} | ||
def get_from_cache_or_call(cache_key, api_fun, ttl \\ :timer.minutes(10)) do | ||
case Cache.get(cache_key) do | ||
# If location wasn't cached within last ttl minutes, call OWM API | ||
nil -> | ||
result = api_fun.() | ||
Cache.put(cache_key, result, ttl: ttl) | ||
result | ||
|
||
# If location was cached, return it | ||
location -> | ||
location | ||
end | ||
end | ||
end |
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
Oops, something went wrong.