View Source ExDuck (ExDuck v0.1.0)

ExDuck is an Elixir Client for the DuckDuckGo Instant Answer API

Link to this section Summary

Functions

Query the DuckDuckGo Instant Answer API into a normalized answer

Query the DuckDuckGo Instant Answer API

Translate a normalized answer or raw query results to markdown

Normalize a response from the DuckDuckGo Instant Answer API

Link to this section Functions

Query the DuckDuckGo Instant Answer API into a normalized answer

Will return a map of normalized results from the API

Will raise in case of errors

This is exactly the same as calling

ExDuck.query!("elixir language") |> ExDuck.understand()

examples

Examples

iex> ExDuck.answer!("Elixir Language").heading
"Elixir (programming language)"

Query the DuckDuckGo Instant Answer API

Will return the raw (JSON-decoded) results from the API

Will raise in case of errors

examples

Examples

iex> ExDuck.query!("Elixir Language") |> Map.get("Heading")
"Elixir (programming language)"

Translate a normalized answer or raw query results to markdown

examples

Examples

iex> ExDuck.query!("Elixir Language")
...> |> ExDuck.to_markdown()
...> |> String.starts_with?("# Elixir (programming language)")

Normalize a response from the DuckDuckGo Instant Answer API

This is the real deal of this library. Querying the API is simply HTTP GET and JSON parsing. This function is where DuckDuckGo's several types of answers are parsed into consistent forms and non-responses are recognized (e.g. calculation answers). I'm not sold on my current state of normalization. But it does suffice.

Calling understand/1 with an already normalized answer is idempotent.

examples

Examples

# a direct answer
iex> elixir = ExDuck.query!("Elixir Language") |> ExDuck.understand()
iex> elixir |> Map.keys()
[:answer, :entity, :heading, :image, :image_caption, :information, :related, :source, :type, :url]

# a category answer
iex> simpsons_characters = ExDuck.query!("Simpsons Characters") |> ExDuck.understand()
iex> simpsons_characters |> Map.keys()
[:entries, :heading, :type]
iex> simpsons_characters.type == "category"
true

# understanding is idempotent
iex> elixir = ExDuck.query!("Elixir Language") |> ExDuck.understand()
iex> elixir == ExDuck.understand(elixir)