Current section
Files
Jump to
Current section
Files
lib/intercom/api/conversation.ex
defmodule ExIntercom.Conversation do
@moduledoc """
Wrapper around ExIntercom Conversations endpoint
"""
import ExIntercom.Base
@endpoint "https://api.intercom.io/conversations/"
@doc """
Returns map of all conversations
"""
@spec find_all :: [%{}, ...]
def find_all do
total_pages |> through_pages([])
end
@doc """
Returns single conversation
"""
@spec find([{:atom, integer}]) :: %{}
def find(id: id) do
fetch(@endpoint <> to_string(id))
end
defp fetch(url) do
request(url) |> Poison.decode!
end
defp through_pages(0, tasks) do
Enum.reduce(tasks, &(Task.await(&1) ++ &2))
end
defp through_pages(pages, tasks) do
task = Task.async(fn ->
page_body = paginate_params(@endpoint, pages) |> fetch
page_body["conversations"]
end)
through_pages(pages - 1, tasks ++ [task])
end
defp total_pages do
page_body = fetch(@endpoint)
page_body["pages"]["total_pages"]
end
end