aboutsummaryrefslogtreecommitdiff
path: root/lib/zendex/search.ex
blob: 09b6c13ec78e11665309ec24c5dd57a20867fc39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
defmodule Zendex.Search do
  @moduledoc  """
  Allows use of the Zendex search API functionality.
  """

  @url "/api/v2/search.json?query="
  @http_client Application.get_env(:zendex, :http_client)

  @spec query(Zendex.Connection.t, map, String.t, String.t)
    :: HTTPoison.Response.t
  def query(connection, query, sort_by \\ "", sort_order \\ "asc") do
    search_string = create_search_string(query)
    sort_string = create_sort_string(sort_by, sort_order)

    full_uri = connection.base_url <> @url <> search_string <> sort_string

    @http_client.get!(full_uri,
                      [{"Authorization", "Basic #{connection.authentication}"}])
  end

  defp create_search_string(query) do
    query
    |> Enum.map(&convert(&1))
    |> Enum.join(" ")
    |> URI.encode_www_form
  end

  defp convert({:type, type}), do: "type:#{type}"
  defp convert({:requester, requester}), do: "requester:#{requester}"

  defp create_sort_string("", _sort_order), do: ""
  defp create_sort_string(sort_by, sort_order) do
    "&sort_by=#{sort_by}&sort_order=#{sort_order}"
  end

end