defmodule Quotes do @moduledoc """ Documentation for Quotes. """ @doc """ `parse_json` returns a list of maps containing quotes.
The format returned is: ```elixir [ %{ "author" => "Albert Einstein", "text" => "Once we accept our limits, we go beyond them." }, %{ "author" => "Peter Drucker", "source" => "https://www.goodreads.com/quotes/784267", "tags" => "time, management", "text" => "Until we can manage time, we can manage nothing else." } %{...}, ... ] ``` All quotes MUST have an `author` and `text` field.
Some quotes have `tags` and `source`, please help to expand/verify others.
see: https://github.com/nelsonic/quotes#contributing """ def parse_json do {:ok, cwd} = File.cwd File.read!(cwd <> "/quotes.json") |> Jason.decode!() end @doc """ `random` returns a random quote.
e.g: ```elixir %{ "author" => "Peter Drucker", "source" => "https://www.goodreads.com/quotes/784267", "tags" => "time, management", "text" => "Until we can manage time, we can manage nothing else." } ``` """ def random do parse_json() |> Enum.random() end @doc """ `random_by_tag` returns a random quote for the chosen tag/keyword. e.g: ```elixir iex> Quotes.random_by_tag("time") %{ "author" => "Leo Tolstoy", "text" => "The two most powerful warriors are patience and time." } ``` """ def random_by_tag(tag) do # IO.inspect(tag) parse_json() |> Enum.map(fn q -> # IO.inspect(q) if ( !is_nil(q["text"]) && q["text"] =~ tag ) || ( !is_nil(q["tags"]) && q["tags"] =~ tag ) do q end end) |> Enum.filter(& !is_nil(&1)) |> Enum.random() end end