Packages

A plugin for Hedwig that will make your eyes roll

Current section

4 Versions

Jump to

Compare versions

6 files changed
+46 additions
-12 deletions
  @@ -1 +1,7 @@
1 1 CHANGELOG
2 +
3 + ### 1.1.0
4 + * Do not start gen_server outside of a supervisor (#4)
5 +
6 + ### 1.0.0
7 + * Initial release
  @@ -2,23 +2,30 @@
2 2
3 3 A plugin for [Hedwig](https://github.com/hedwig-im) that will make your eyes roll.
4 4
5 + ## Usage
6 + ```
7 + me> hedwig dad joke
8 + hedwig> Sore throats are a pain in the neck!
9 + https://icanhazdadjoke.com/j/jiVfq4MZDlb
10 + ```
11 +
12 + Also supported variants are: `dad joke`, `dadjoke`, `dad joke me`, and
13 + `dadjoke me`
14 +
5 15 ## Installation
6 16
7 - If [available in Hex](https://hex.pm/docs/publish), the package can be installed
8 - by adding `hedwig_dad_joke` to your list of dependencies in `mix.exs`:
17 + Add `hedwig_dad_joke` to your list of dependencies in `mix.exs`:
9 18
10 19 ```elixir
11 20 def deps do
12 21 [
13 - {:hedwig_dad_joke, "~> 0.1.0"}
22 + {:hedwig_dad_joke, "~> 1.1.0"}
14 23 ]
15 24 end
16 25 ```
17 26
18 - Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
19 - and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
20 - be found at [https://hexdocs.pm/hedwig_dad_joke](https://hexdocs.pm/hedwig_dad_joke).
21 -
27 + Docs can be found at
28 + [https://hexdocs.pm/hedwig_dad_joke](https://hexdocs.pm/hedwig_dad_joke).
22 29
23 30 ## Lint
24 31 ```
  @@ -36,4 +36,4 @@
36 36 {<<"optional">>,false},
37 37 {<<"repository">>,<<"hexpm">>},
38 38 {<<"requirement">>,<<"~> 1.2.0">>}]]}.
39 - {<<"version">>,<<"1.0.0">>}.
39 + {<<"version">>,<<"1.1.1">>}.
  @@ -23,7 +23,7 @@ defmodule HedwigDadJoke do
23 23 @impl JokeModule
24 24 def random do
25 25 unless Process.whereis(@name) do
26 - @name.start_link(%{})
26 + get_random()
27 27 end
28 28
29 29 GenServer.call(@name, :random)
  @@ -37,7 +37,25 @@ defmodule HedwigDadJoke do
37 37
38 38 @impl true
39 39 @spec handle_call(atom(), any(), Config.t()) :: {:reply, String.t(), Config.t()}
40 - def handle_call(:random, _from, %{sources: sources} = state) do
40 + def handle_call(:random, _from, state) do
41 + {:ok, reply} = get_random(state)
42 +
43 + {:reply, reply, state}
44 + end
45 +
46 + # When gen_server is running, use state as config
47 + defp get_random(%{sources: sources} = state) do
48 + source = Enum.random(sources)
49 +
50 + state
51 + |> source.client()
52 + |> source.random()
53 + |> source.format(state)
54 + end
55 +
56 + # When no gen_server is running, just go get the joke
57 + defp get_random do
58 + %{sources: sources} = state = Config.new()
41 59 source = Enum.random(sources)
42 60
43 61 {:ok, reply} =
  @@ -46,6 +64,6 @@ defmodule HedwigDadJoke do
46 64 |> source.random()
47 65 |> source.format(state)
48 66
49 - {:reply, reply, state}
67 + reply
50 68 end
51 69 end
  @@ -5,6 +5,9 @@ defmodule HedwigDadJoke.Responders do
5 5 use Hedwig.Responder
6 6 @joke_module Application.get_env(:hedwig_dad_joke, :joke_module, HedwigDadJoke)
7 7
8 + @usage """
9 + hedwig dad joke - Replies with a joke that will make your eyes roll
10 + """
8 11 respond ~r/(dadjoke|dad joke)(\s*me)?$/iu, msg do
9 12 response = @joke_module.random()
10 13 send(msg, response)
Loading more files…