Skip to content

Instantly share code, notes, and snippets.

@exlee
Created November 20, 2017 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exlee/9be02098ae6a1507fb9c8cbc55e32b52 to your computer and use it in GitHub Desktop.
Save exlee/9be02098ae6a1507fb9c8cbc55e32b52 to your computer and use it in GitHub Desktop.
defmodule WWTK.Collector.ReportProcessor do
@moduledoc """
ReportProcessor module.
It is the main "logic processor" for processing data
given by the client.
"""
require Logger
require Plug.Conn
alias WWTK.Collector.Unpacker
alias WWTK.Collector.Validator
alias WWTK.Collector.Assigner
alias WWTK.Collector.Considerator
alias WWTK.Collector.Report
alias WWTK.Collector.Errors.EndOfProcessing
import WWTK.Collector.Report, only: [log: 2]
@spec process(String.t, Plug.Conn.t, {:ok, binary, Plug.Conn.t} | {:error, term}) :: Report.t
def process(xri, body, _conn) do
report = body |> process_body(xri)
report
|> log(:read)
|> Unpacker.unpack()
|> log(:unpack)
|> Validator.validate()
|> log(:validate)
|> Assigner.assign()
|> log(:assign)
|> Considerator.consider()
|> log(:consider)
rescue
EndOfProcessing -> nil
end
defp process_body({:ok, data, _}, xri) do
%Report{id: xri, data: data, state: :ok}
end
defp process_body({:error, _}, xri) do
%Report{id: xri, data: nil, state: :error}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment