Skip to content
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

Merged
merged 2 commits into from
Jul 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ jobs:
path: deps
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
restore-keys: ${{ runner.os }}-mix-
- name: Install Node.js Deps (JSON Lint)
Copy link
Member Author

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

run: npm install
- name: Run JSON Lint to confirm statuses.json is valid
run: npm run jsonlint
- name: Install Elixir dependencies
run: mix deps.get
- name: Run Tests
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ So we decided to
it
and make this single-feature micro-package
we can reuse.

# What?

[`statuses.json`](https://github.com/dwyl/statuses/blob/main/statuses.json)
is a maintainable `JSON` file
[`statuses.json`](https://github.com/dwyl/statuses/blob/main/lib/statuses.ex)
is a maintainable list of status structs
that anyone can read
to be informed of **`statuses`**
used in our App(s).
It makes it easier for us to keep **`statuses`**
in one place
and means
anyone can contribute.

# Who?

This package is for us by us.
Expand All @@ -52,15 +54,15 @@ in `mix.exs`:
```elixir
def deps do
[
{:statuses, "~> 1.0.1"},
{:statuses, "~> 1.1"},
]
end
```

## Usage

```elixir
statuses = Statuses.parse_json()
statuses = Statuses.get_statuses()
# use them how you see fit
```

Expand Down
10 changes: 10 additions & 0 deletions lib/status.ex
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]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all the keys are required

@derive Jason.Encoder
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you need to encode some struct that does not implement the protocol, if you own the struct, you can derive the implementation specifying which fields should be encoded to JSON

https://github.com/michalmuskala/jason#differences-to-poison


defstruct [:code, :desc, :text]
end
133 changes: 94 additions & 39 deletions lib/statuses.ex
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())
Copy link
Member Author

Choose a reason for hiding this comment

The 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
15 changes: 6 additions & 9 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Statuses.MixProject do
def project do
[
app: :statuses,
version: "1.0.1",
version: "1.1.0",
elixir: "~> 1.13",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down Expand Up @@ -35,32 +35,29 @@ defmodule Statuses.MixProject do
# Parse JSON file: github.com/michalmuskala/jason
{:jason, "~> 1.1"},

# https://github.com/dwyl/useful#atomize_map_keys1
{:useful, "~> 1.0.8"},

# Create docs for publishing Hex.docs: github.com/elixir-lang/ex_doc
{:ex_doc, "~> 0.28", only: :dev, runtime: false},

# Check test coverage: github.com/parroty/excoveralls
{:excoveralls, "~> 0.14.3", only: :test},
{:excoveralls, "~> 0.14.3", only: :test}
]
end

defp description() do
defp description() do
"a collection of inspiring statuses and methods to return them."
end

defp package() do
[
files: ["lib/statuses.ex", "mix.exs", "README.md", "statuses.json"],
files: ["lib/status.ex", "lib/statuses.ex", "mix.exs", "README.md"],
name: "statuses",
licenses: ["GPL-2.0-or-later"],
maintainers: ["dwyl"],
links: %{"GitHub" => "https://github.com/dwyl/statuses"}
]
end

# Aliases are shortcuts or tasks specific to the current project.
# Aliases are shortcuts or tasks specific to the current project.
# For example, to install project dependencies and perform other setup tasks, run:
#
# $ mix c
Expand All @@ -69,7 +66,7 @@ defmodule Statuses.MixProject do
defp aliases do
[
t: ["test"],
c: ["coveralls.html"],
c: ["coveralls.html"]
]
end
end
1 change: 0 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
"useful": {:hex, :useful, "1.0.8", "795a5bf94567e4a1b374621577acabc80ea80634b634095237f98e40e64e9d24", [:mix], [], "hexpm", "947ae0ba2b3c06bcfd8994e95e29f4cc13287aab81b481ae6abb9077fc9c1ad5"},
}
36 changes: 0 additions & 36 deletions package.json

This file was deleted.

Loading