-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Define status as struct #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
defmodule Status do | ||
@moduledoc """ | ||
Define the Status struct with desc, id and text key required | ||
""" | ||
@type t :: %Status{code: integer(), desc: String.t(), text: atom()} | ||
@enforce_keys [:code, :desc, :text] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all the keys are required |
||
@derive Jason.Encoder | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
https://github.com/michalmuskala/jason#differences-to-poison |
||
|
||
defstruct [:code, :desc, :text] | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,102 @@ | ||
defmodule Statuses do | ||
@moduledoc """ | ||
Documentation for `Statuses`. | ||
The `Statuses` module create a list of | ||
statuses to be used in Elixir projects | ||
""" | ||
|
||
@doc """ | ||
`parse_json/0` returns a list of maps containing statuses. e.g: | ||
|
||
```elixir | ||
[ | ||
%{ | ||
id: "1", | ||
text: "verified", | ||
"desc": "A person verified by a 3rd party OAuth provider" | ||
}, | ||
... | ||
] | ||
``` | ||
|
||
All statuses MUST have an `id` and `text` field. | ||
Ideally they should also have a `desc` (description), | ||
please help to expand/refine the description any status | ||
that is not well defined but avoid changing the `text` or `id`. | ||
Returns list of the statuses | ||
""" | ||
def parse_json do | ||
{:ok, cwd} = File.cwd | ||
# we need this cd to locate the file in /deps | ||
case cwd =~ "/statuses" do | ||
true -> | ||
read_decode() | ||
# coveralls-ignore-start | ||
false -> # temporarily cd into deps/quotes dir and read quotes.json file: | ||
File.cd!("deps/statuses") | ||
data = read_decode() | ||
# chnge back into the root directory | ||
File.cd!("../..") | ||
data # return the decoded (parsed) JSON data | ||
# coveralls-ignore-stop | ||
end | ||
end | ||
|
||
defp read_decode do | ||
File.read!("statuses.json") | ||
|> Jason.decode!() | ||
|> Enum.map(fn(m) -> Useful.atomize_map_keys(m) end) | ||
@spec get_statuses() :: list(Status.t()) | ||
def get_statuses() do | ||
[ | ||
%Status{ | ||
code: 1, | ||
desc: | ||
"A person verified by a 3rd party OAuth provider or by confirming their email address", | ||
text: :verified | ||
}, | ||
%Status{ | ||
code: 2, | ||
desc: "All items are uncategorized when they are first created. (Yes, US spelling)", | ||
text: :uncategorized | ||
}, | ||
%Status{ | ||
code: 3, | ||
desc: "An App, Item or Person can be active; this is the default state for an App", | ||
text: :active | ||
}, | ||
%Status{code: 4, desc: "Items marked as done are complete", text: "done"}, | ||
%Status{ | ||
code: 5, | ||
desc: "A flagged App, Item or Person requires admin attention", | ||
text: :flagged | ||
}, | ||
%Status{ | ||
code: 6, | ||
desc: | ||
"Soft-deleted items that no longer appear in UI but are kept for audit trail purposes", | ||
text: :deleted | ||
}, | ||
%Status{ | ||
code: 7, | ||
desc: "When an email or item is ready to be started/sent is still pending", | ||
text: :pending | ||
}, | ||
%Status{ | ||
code: 8, | ||
desc: "An email that has been sent but not yet opened", | ||
text: :sent | ||
}, | ||
%Status{code: 9, desc: "When an email is opened by the recipient", text: :opened}, | ||
%Status{ | ||
code: 10, | ||
desc: "Temporary email bounce e.g. because inbox is full", | ||
text: :bounce_transient | ||
}, | ||
%Status{ | ||
code: 11, | ||
desc: "Permanent email bounce e.g. when inbox doesn't exist", | ||
text: :bounce_permanent | ||
}, | ||
%Status{code: 200, desc: "successful HTTP request", text: :ok}, | ||
%Status{ | ||
code: 307, | ||
desc: "the request should be repeated with another URI", | ||
text: :temporary_redirect | ||
}, | ||
%Status{ | ||
code: 308, | ||
desc: "all future requests should be directed to the given URI", | ||
text: :permanent_redirect | ||
}, | ||
%Status{ | ||
code: 400, | ||
desc: "server cannot or will not process the request due to an apparent client error", | ||
text: :bad_request | ||
}, | ||
%Status{ | ||
code: 401, | ||
desc: "when authentication is required and has failed", | ||
text: :unauthorized | ||
}, | ||
%Status{code: 403, desc: "request forbidden", text: :forbidden}, | ||
%Status{code: 404, desc: "requested resource could not be found", text: :not_found}, | ||
%Status{ | ||
code: 429, | ||
desc: "has sent too many requests in a given amount of time", | ||
text: :too_many_requests | ||
}, | ||
%Status{ | ||
code: 500, | ||
desc: "an unexpected condition was encountered", | ||
text: :internal_server_error | ||
} | ||
] | ||
end | ||
|
||
@doc """ | ||
Returns the list of statuses as json | ||
""" | ||
def statuses_to_json(), do: Jason.encode!(get_statuses()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Create a function to return json, I don't think we'll use it but I can see how it can be useful |
||
end |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the PR doesn't have the json file, we don't need to install npm pacakage