Skip to content

Instantly share code, notes, and snippets.

@ijunaid8989
Created May 18, 2016 11:04
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 ijunaid8989/1ec6ae9cb1c066f439207864958756d8 to your computer and use it in GitHub Desktop.
Save ijunaid8989/1ec6ae9cb1c066f439207864958756d8 to your computer and use it in GitHub Desktop.
defmodule EvercamMedia.Validation.CloudRecording do
def validate_params(params) do
with :ok <- validate(:frequency, params["frequency"]),
:ok <- validate(:storage_duration, params["storage_duration"]),
:ok <- validate(:status, params["status"]),
:ok <- validate(:schedule, params["schedule"]),
do: :ok
end
defp validate(key, value) when value in [nil, ""], do: invalid(key)
defp validate(:status, "on"), do: :ok
defp validate(:status, "off"), do: :ok
defp validate(:status, "on-scheduled"), do: :ok
defp validate(:status = key, _value), do: invalid(key)
defp validate(:schedule = key, value) do
case Poison.decode(value) do
{:ok , json} -> valid_schedule(key, json)
{:error, _error} -> invalid(key)
end
end
defp validate(_key, value) when is_integer(value), do: :ok
defp validate(key, value) when is_bitstring(value), do: validate(key, to_integer(value))
defp validate(key, _value), do: invalid(key)
defp invalid(key), do: {:invalid, "The parameter '#{key}' isn't valid."}
defp to_integer(value) do
case Integer.parse(value) do
{number, _} -> number
_ -> :error
end
end
defp valid_schedule(key, json) do
days = ["Friday", "Monday", "Saturday", "Sunday", "Thursday", "Tuesday", "Wednesday"]
compare_keys = Map.keys(json) |> Enum.sort == days
case compare_keys do
true -> :ok
false -> invalid(key)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment