defmodule ExVideoFactory do @moduledoc """ Documentation for ExVideoFactory. """ @doc """ Get the list of videos. """ def videos(query \\ %{}) do page = Map.get(query, "page", 1) |> force_integer page_size = Map.get(query, "per_page", 20) |> force_integer query = format_channels(query) response = ExVideoFactory.Client.get("videos", [query: query]) total = get_total_number_of_elements(response) list = response.body %{ videos: list, size: length(list), offset: (page - 1) * page_size + 1, total: total } end @doc """ Get the list of regions. """ def regions do response = ExVideoFactory.Client.get("regions") list = response.body total = get_total_number_of_elements(response) %{ regions: list, size: length(list), total: total } end @doc """ Get the list of types. """ def types do response = ExVideoFactory.Client.get("types") list = response.body total = get_total_number_of_elements(response) %{ types: list, size: length(list), total: total } end @doc """ Get the list of channels. """ def channels do response = ExVideoFactory.Client.get("channels") list = response.body total = get_total_number_of_elements(response) %{ channels: list, size: length(list), total: total } end defp get_total_number_of_elements(response) do response.headers['X-Total-Count'] end defp force_integer(param) when is_bitstring(param) do param |> String.to_integer end defp force_integer(param) do param end defp format_channels(%{"channels" => channels} = query) do joined_channels = Enum.reduce(channels, fn(x, acc) -> x <> "," <> acc end) query |> Map.put("channel.id[in]", joined_channels) |> Map.delete("channels") end defp format_channels(query) do query end end