Packages

An Elixir client for the DuckDuckGo Instant Answer API.

Current section

Files

Jump to
duckduckgo lib ddg query.ex
Raw

lib/ddg/query.ex

defmodule DDG.Query do
@moduledoc """
Functionality for querying for the DuckDuckGo Instant Answer API.
"""
@api_url "https://api.duckduckgo.com/?q="
@fields ["Abstract", "AbstractSource", "AbstractText", "AbstractURL", "Answer",
"AnswerType", "Definition", "DefinitionSource", "DefinitionURL", "Entity",
"Heading", "Image", "ImageHeight", "ImageIsLogo", "ImageWidth", "Infobox",
"Redirect", "RelatedTopics", "Results", "Type", "meta"]
def query(query, safe \\ true, html \\ false) do
get_json(query)
|> Poison.decode!
|> Map.take(@fields)
|> Enum.map(fn({k, v}) -> {String.to_atom(k), v} end)
end
defp get_json(query) do
url = @api_url <> query <> "&format=json"
|> HTTPoison.get!
url.body
end
end