Skip to content

Commit

Permalink
added ex_ussd_simulator module
Browse files Browse the repository at this point in the history
  • Loading branch information
lenileiro committed Feb 24, 2021
1 parent d20fe92 commit d988b64
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 47 deletions.
31 changes: 31 additions & 0 deletions lib/ex_ussd_simulator.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule ExUssdSimulator do

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__)}"

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
59 changes: 12 additions & 47 deletions lib/live/page_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,59 +32,24 @@ defmodule ExUssdSimulator.PageLive do

@impl true
def handle_event("call", _params, socket) do
{:noreply, execute(socket)}
{:noreply, build_menu(socket)}
end

def build_menu(socket) do
prompt = ExUssd.Utils.navigate(socket.assigns.ussd_code, socket.assigns.menu, socket.assigns.session_id, "*544#")
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 = socket
|> assign(session_id: random_session_id)
|> show_home_prompt()
end

defp execute(socket) do
result = execute_ussd_code(socket)
set_prompt(socket, result)
|> assign(menu: opts[:menu])
|> assign(ussd_code: "")
|> build_menu()
end

defp show_home_prompt(socket) do
socket
|> reset_ussd_code()
|> execute()
end

defp execute_ussd_code(%{assigns: %{ussd_code: ussd_code, session_id: session_id}}) do
callback_url = Config.callback_url()
service_code = Config.service_code()
headers = [{"Content-Type", "application/json"}]

body =
%{
text: ussd_code,
sessionId: session_id,
serviceCode: service_code
}
|> Jason.encode!()

case HTTPoison.post(callback_url, body, headers) do
{:ok, %{body: prompt}} ->
prompt

error ->
Logger.error(inspect(error))

@callback_url_unavailable_error <> callback_url
end
end

defp reset_ussd_code(socket) do
assign(socket, ussd_code: "")
end

defp set_prompt(socket, prompt) do
socket
|> assign(prompt: prompt)
|> reset_ussd_code()
end
end

0 comments on commit d988b64

Please sign in to comment.