Skip to content

Commit

Permalink
Merge pull request #5 from KittyHeaven/group-by-path
Browse files Browse the repository at this point in the history
group resources by path
  • Loading branch information
woylie authored Jun 14, 2017
2 parents 4129768 + 03bc904 commit 3f2fe3b
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 39 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog BlueBird

## v0.3.1

- group actions by resource to avoid aglio warnings

## v0.3.0

This release is a complete rewrite of the library.
Expand Down
32 changes: 26 additions & 6 deletions lib/blue_bird/writer/blueprint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,29 @@ defmodule BlueBird.Writer.Blueprint do
end

@spec process_group({String.t | nil, [Route.t]}) :: String.t
defp process_group({nil, routes}), do: process_routes(routes)
defp process_group({nil, routes}) do
routes
|> group_routes(:path)
|> process_resources
end
defp process_group({group_name, routes}) do
grouped_routes = group_routes(routes, :path)

"# Group #{group_name}\n\n"
<> process_resources(grouped_routes)
end

## Resources

@spec process_resources([{String.t, [{String.t, String.t, [Route.t]}]}]) ::
String.t
defp process_resources(resources) do
Enum.map_join(resources, "\n", &process_resource(&1))
end

@spec process_resource({String.t | nil, [Route.t]}) :: String.t
defp process_resource({path, routes}) do
"## #{routes |> Enum.at(0) |> display_path}\n\n"
<> process_routes(routes)
end

Expand Down Expand Up @@ -201,14 +221,14 @@ defmodule BlueBird.Writer.Blueprint do
defp print_route_definition(route) do
path = display_path(route)

print_route_header(route.method, path, route.title)
print_route_header(route.method, route.title)
<> print_route_description(route.description)
end

@spec print_route_header(String.t, String.t, String.t | nil) :: String.t
defp print_route_header(method, path, nil), do: "## #{method} #{path}\n"
defp print_route_header(method, path, title) do
"## #{title} [#{method} #{path}]\n"
@spec print_route_header(String.t, String.t | nil) :: String.t
defp print_route_header(method, nil), do: "### #{method}\n"
defp print_route_header(method, title) do
"### #{title} [#{method}]\n"
end

@spec print_route_description(String.t | nil) :: String.t
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule BlueBird.Mixfile do
defp deps do
[
# Static code analysis
{:credo, "~> 0.8.0-rc7", only: [:dev, :test]},
{:credo, "~> 0.8.0", only: [:dev, :test]},
{:dialyxir, "~> 0.5.0", only: [:dev, :test], runtime: false},

# Coverage
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%{"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], []},
"certifi": {:hex, :certifi, "1.2.1", "c3904f192bd5284e5b13f20db3ceac9626e14eeacfbb492e19583cf0e37b22be", [:rebar3], []},
"credo": {:hex, :credo, "0.8.0-rc7", "8cd92113a5cb37487102e5551240bdc6e4d93c19bdf7fc902f865ff0b4887256", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, optional: false]}]},
"credo": {:hex, :credo, "0.8.0", "187ce36098afef67baa503d0bb3ec047953c88e4c662591371d15ae9ce150119", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, optional: false]}]},
"dialyxir": {:hex, :dialyxir, "0.5.0", "5bc543f9c28ecd51b99cc1a685a3c2a1a93216990347f259406a910cf048d1d7", [:mix], []},
"earmark": {:hex, :earmark, "1.2.2", "f718159d6b65068e8daeef709ccddae5f7fdc770707d82e7d126f584cd925b74", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.16.1", "b4b8a23602b4ce0e9a5a960a81260d1f7b29635b9652c67e95b0c2f7ccee5e81", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, optional: false]}]},
Expand Down
28 changes: 18 additions & 10 deletions test/support/examples/grouping.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,35 @@ defmodule BlueBird.Test.Support.Examples.Grouping do
# Group Airplanes
## GET /airplanes
## /airplanes
## POST /airplanes
### GET
## DELETE /airplanes/{id}
### POST
## GET /airplanes/{id}
## /airplanes/{id}
## PUT /airplanes/{id}
### DELETE
### GET
### PUT
# Group Cats
## GET /cats
## /cats
### GET
### POST
## POST /cats
## /cats/{id}
## DELETE /cats/{id}
### DELETE
## GET /cats/{id}
### GET
## PUT /cats/{id}
### PUT
"""
end
end
8 changes: 6 additions & 2 deletions test/support/examples/notes_warnings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ defmodule BlueBird.Test.Support.Examples.NotesWarnings do
# Heavenly API
## GET /route-with-description-and-note
## /route-with-description-and-note
### GET
This is my route. My route is not my enemy.
::: note
This is my route.
:::
## GET /route-with-note-and-warning
## /route-with-note-and-warning
### GET
::: note
This is my route.
Expand Down
4 changes: 3 additions & 1 deletion test/support/examples/parameters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ defmodule BlueBird.Test.Support.Examples.Parameters do
# Pastry API
## GET /pastry/{id}/{type}{?page,query}
## /pastry/{id}/{type}{?page,query}
### GET
+ Parameters
Expand Down
6 changes: 4 additions & 2 deletions test/support/examples/requests.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule BlueBird.Test.Support.Examples.Requests do
path: "/request-headers",
requests: [%Request{
method: "GET",
path: "/plain-with-line-breaks",
path: "/request-headers",
body_params: %{"peter" => "paul", "mary" => "peter"},
headers: [
{"accept", "application/json"},
Expand All @@ -39,7 +39,9 @@ defmodule BlueBird.Test.Support.Examples.Requests do
# Trendy API
## GET /request-headers
## /request-headers
### GET
+ Request
Expand Down
12 changes: 9 additions & 3 deletions test/support/examples/responses.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ defmodule BlueBird.Test.Support.Examples.Responses do
# Lavish API
## GET /multiple-headers
## /multiple-headers
### GET
+ Response 200 (text/plain)
Expand All @@ -79,15 +81,19 @@ defmodule BlueBird.Test.Support.Examples.Responses do
Multiple headers.
## GET /plain-response
## /plain-response
### GET
+ Response 200 (text/plain)
+ Body
Plain response.
## GET /plain-with-line-breaks
## /plain-with-line-breaks
### GET
+ Response 200 (text/plain)
Expand Down
8 changes: 6 additions & 2 deletions test/support/examples/route_titles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ defmodule BlueBird.Test.Support.Examples.RouteTitles do
# Group Pony
## Ride [GET /route-with-title]
## /route-with-title
### Ride [GET]
+ Response 204
## GET /route-without-title
## /route-without-title
### GET
+ Response 204
"""
Expand Down
12 changes: 9 additions & 3 deletions test/support/examples/simple.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,25 @@ defmodule BlueBird.Test.Support.Examples.Simple do
It may be helpful. Or not.
## GET /route-with-204-response
## /route-with-204-response
### GET
+ Response 204
## GET /route-with-simple-response
## /route-with-simple-response
### GET
+ Response 200 (text/plain)
+ Body
Simple response.
## GET /route-without-info-or-response
## /route-without-info-or-response
### GET
"""
end
end
16 changes: 8 additions & 8 deletions test/writer/blueprint_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule BlueBird.Test.Writer.BlueprintTest do
end

describe "process_route/1" do
test "prints header with method, title, path, and description" do
test "prints header with method, title, and description" do
result = process_route(%Route{
method: "POST",
path: "/path",
Expand All @@ -60,17 +60,17 @@ defmodule BlueBird.Test.Writer.BlueprintTest do
})

assert result == """
## Get all [POST /path]
### Get all [POST]
This route gets all things.
Really.
"""
end

test "prints header with method and path, without title and description" do
test "prints header with method, without title and description" do
result = process_route(%Route{method: "POST", path: "/path"})

assert result == "## POST /path\n"
assert result == "### POST\n"
end

test "prints note" do
Expand All @@ -80,7 +80,7 @@ defmodule BlueBird.Test.Writer.BlueprintTest do
note: "This is important.\n\nVery."
})
assert result == """
## POST /path
### POST
::: note
This is important.
Expand All @@ -97,7 +97,7 @@ defmodule BlueBird.Test.Writer.BlueprintTest do
warning: "This is important.\n\nEven more."
})
assert result == """
## POST /path
### POST
::: warning
This is important.
Expand Down Expand Up @@ -126,7 +126,7 @@ defmodule BlueBird.Test.Writer.BlueprintTest do
})

assert result == """
## POST /path
### POST
+ Parameters
Expand Down Expand Up @@ -174,7 +174,7 @@ defmodule BlueBird.Test.Writer.BlueprintTest do
})

assert result == """
## POST /users/{id}/pets{?q}
### POST
+ Request (application/json)
Expand Down

0 comments on commit 3f2fe3b

Please sign in to comment.