|
| 1 | +defmodule ElixirPopularity.HackernewsApi do |
| 2 | + @moduledoc """ |
| 3 | + Fetches data from the Hackernews API, extracts the necessary fields, |
| 4 | + and massages some data |
| 5 | + """ |
| 6 | + |
| 7 | + require Logger |
| 8 | + |
| 9 | + alias ElixirPopularity.HackernewsItem |
| 10 | + |
| 11 | + def get_hn_item(resource_id) do |
| 12 | + get_hn_item(resource_id, 4) |
| 13 | + end |
| 14 | + |
| 15 | + defp get_hn_item(resource_id, retry) do |
| 16 | + response = |
| 17 | + resource_id |
| 18 | + |> gen_api_url() |
| 19 | + |> HTTPoison.get([], hackney: [pool: :hn_id_pool]) |
| 20 | + |
| 21 | + with {_, {:ok, body}} <- {"hn_api", handle_response(response)}, |
| 22 | + {_, {:ok, decoded_payload}} <- {"payload_decode", Jason.decode(body)}, |
| 23 | + {_, {:ok, data}} <- {"massage_data", massage_data(decoded_payload)} do |
| 24 | + data |
| 25 | + else |
| 26 | + {stage, error} -> |
| 27 | + Logger.warn( |
| 28 | + "Failed attempt #{5 - retry} at stage \"#{stage}\" with Hackernews ID of #{resource_id}. Error details: #{ |
| 29 | + inspect(error) |
| 30 | + }" |
| 31 | + ) |
| 32 | + |
| 33 | + if retry > 0 do |
| 34 | + get_hn_item(resource_id, retry - 1) |
| 35 | + else |
| 36 | + Logger.warn("Failed to retrieve Hackernews ID of #{resource_id}.") |
| 37 | + :error |
| 38 | + end |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + defp handle_response({:ok, %HTTPoison.Response{status_code: 200, body: body}}) do |
| 43 | + {:ok, body} |
| 44 | + end |
| 45 | + |
| 46 | + defp handle_response({_, invalid_response}) do |
| 47 | + {:error, invalid_response} |
| 48 | + end |
| 49 | + |
| 50 | + defp gen_api_url(resource_id) do |
| 51 | + "https://hacker-news.firebaseio.com/v0/item/#{resource_id}.json" |
| 52 | + end |
| 53 | + |
| 54 | + defp massage_data(data) do |
| 55 | + {:ok, |
| 56 | + HackernewsItem.create( |
| 57 | + data["text"], |
| 58 | + data["type"], |
| 59 | + data["title"], |
| 60 | + data["time"] |
| 61 | + )} |
| 62 | + end |
| 63 | +end |
0 commit comments