Skip to content

Commit

Permalink
better menu handler
Browse files Browse the repository at this point in the history
  • Loading branch information
lenileiro committed Feb 24, 2021
1 parent d988b64 commit f8255a0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 34 deletions.
52 changes: 25 additions & 27 deletions lib/ex_ussd_simulator.ex
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
defmodule ExUssdSimulator do
use Agent

use Agent
@doc """
### Example
children = [
{ExUssdSimulator, menu: ExUssd.Menu.render(name: "Home", handler: MyHomeHandler)}
]
"""
def start_link(opts) do
menu =
opts[:menu] ||
raise ArgumentError, "the :menu option is required by #{inspect(__MODULE__)}"

@doc """
### Example
children = [
{ExUssdSimulator, menu: ExUssd.Menu.render(name: "Home", handler: MyHomeHandler)}
]
"""
def start_link(opts) do
menu =
opts[:menu] ||
raise ArgumentError, "the :menu option is required by #{inspect(__MODULE__)}"
Agent.start_link(fn -> opts end, name: __MODULE__)
end

Agent.start_link(fn -> opts end, name: __MODULE__)
end

def init(opts) do
_name =
opts[:menu] ||
raise ArgumentError, "the :menu option is a required by #{inspect(__MODULE__)}.init/1"

opts
end

def value do
Agent.get(__MODULE__, & &1)
end

end
def init(opts) do
_name =
opts[:menu] ||
raise ArgumentError, "the :menu option is a required by #{inspect(__MODULE__)}.init/1"

opts
end

def value do
Agent.get(__MODULE__, & &1)
end
end
39 changes: 32 additions & 7 deletions lib/live/page_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,45 @@ defmodule ExUssdSimulator.PageLive do
end

def build_menu(socket) do
prompt = ExUssd.Utils.navigate(socket.assigns.ussd_code, socket.assigns.menu, socket.assigns.session_id, "*544#")
prompt = ussd_response(socket)
socket |> assign(prompt: prompt)
end

defp new_session(socket) do
random_session_id = Enum.random(123_123_123..999_999_999)
opts = ExUssdSimulator.value()
IO.inspect opts

socket = socket
|> assign(session_id: random_session_id)
|> assign(menu: opts[:menu])
|> assign(ussd_code: "")
|> build_menu()
socket =
socket
|> assign(session_id: random_session_id)
|> assign(menu: opts[:menu])
|> assign(ussd_code: "")
|> build_menu()
end

def ussd_response(socket) do
internal_routing = %{
text: socket.assigns.ussd_code,
session_id: socket.assigns.session_id,
service_code: "*234#"
}

api_parameters = %{"text" => internal_routing.text, "phone_number" => "254722000000"}

route =
ExUssd.Routes.get_route(%{
text: internal_routing.text,
service_code: internal_routing.service_code
})

{:ok, %{menu_string: menu_string, should_close: _}} =
EXUssd.Common.goto(
internal_routing: internal_routing,
menu: socket.assigns.menu,
api_parameters: api_parameters,
route: route
)

menu_string
end
end

0 comments on commit f8255a0

Please sign in to comment.