Skip to content

Commit 95d6c51

Browse files
committed
Add searching for RFCs
1 parent 240b168 commit 95d6c51

File tree

3 files changed

+119
-78
lines changed

3 files changed

+119
-78
lines changed
Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
defmodule ComponentsGuide.Research.Static do
22
@http_statuses_list [
33
{101, "Switching Protocols", ""},
4-
54
{200, "OK", ""},
65
{201, "Created", ""},
76
{202, "Accepted", ""},
87
{204, "No Content", ""},
9-
108
{301, "Moved Permanently", ""},
119
{302, "Found", ""},
1210
{303, "See Other", ""},
1311
{304, "Not Modified", ""},
1412
{307, "Temporary Redirect", ""},
15-
#{308, "Permanent Redirect", ""},
16-
13+
# {308, "Permanent Redirect", ""},
14+
1715
{400, "Bad Request", ""},
1816
{401, "Unauthorized", ""},
1917
{402, "Payment Required", ""},
@@ -26,34 +24,63 @@ defmodule ComponentsGuide.Research.Static do
2624
{412, "Precondition Failed", ""},
2725
{422, "Unprocessable Entity", ""},
2826
{429, "Too Many Requests", ""},
29-
3027
{500, "Internal Server Error", ""},
3128
{501, "Not Implemented", ""},
3229
{502, "Bad Gateway", ""},
3330
{503, "Service Unavailable", ""},
34-
{504, "Gateway Timeout", ""},
31+
{504, "Gateway Timeout", ""}
3532
]
36-
37-
@http_statuses_map Map.new(@http_statuses_list, fn {status, name, description} -> {"#{status}", {name, description}} end)
38-
33+
34+
@http_statuses_map Map.new(@http_statuses_list, fn {status, name, description} ->
35+
{"#{status}", {name, description}}
36+
end)
37+
3938
@rfc_list [
40-
{"json", ["rfc8259", "rfc7159", "rfc4627"], "https://tools.ietf.org/html/rfc8259"},
39+
{"json", ["rfc8259", "rfc7159", "rfc4627"],
40+
[
41+
url: "https://tools.ietf.org/html/rfc8259",
42+
media_type: "application/json"
43+
]},
44+
{"csv", ["rfc4180"],
45+
[
46+
url: "https://tools.ietf.org/html/rfc4180",
47+
media_type: "text/csv"
48+
]}
4149
]
42-
50+
4351
def search_for(query) when is_binary(query) do
4452
[
45-
search_for(:http_status, query)
53+
search_for(:http_status, query),
54+
search_for(:rfc, query)
4655
]
56+
|> List.flatten()
4757
end
4858

4959
defp search_for(:http_status, query) when is_binary(query) do
5060
query = query |> String.trim()
61+
5162
case Map.get(@http_statuses_map, query) do
5263
nil ->
53-
nil
54-
64+
[]
65+
5566
{name, description} ->
56-
{:ok, name, description}
67+
[{:http_status, {name, description}}]
5768
end
5869
end
70+
71+
defp search_for(:rfc, query) when is_binary(query) do
72+
query = query |> String.downcase() |> String.trim()
73+
74+
matches? = fn
75+
{^query, _, _} ->
76+
true
77+
78+
{_, rfcs, metadata} ->
79+
Enum.member?(rfcs, query) || Keyword.get(metadata, :media_type) == query
80+
end
81+
82+
@rfc_list
83+
|> Stream.filter(matches?)
84+
|> Enum.map(fn item -> {:rfc, item} end)
85+
end
5986
end

apps/components_guide_web/lib/components_guide_web/controllers/research_controller.ex

Lines changed: 77 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule ComponentsGuideWeb.ResearchController do
55
alias ComponentsGuide.Research.Spec
66
alias ComponentsGuide.Research.Static
77
alias ComponentsGuideWeb.ResearchView, as: View
8+
alias ComponentsGuideWeb.ResearchView.Section, as: Section
89

910
def index(conn, %{"q" => query, "turbo" => _}) do
1011
query = query |> String.trim()
@@ -64,17 +65,6 @@ defmodule ComponentsGuideWeb.ResearchController do
6465
content_tag(:ul, items)
6566
end
6667

67-
defmodule Section do
68-
def card(children) do
69-
content_tag(
70-
:article,
71-
children,
72-
class:
73-
"mb-4 text-xl spacing-y-4 p-4 text-white bg-blue-900 border border-blue-700 rounded shadow-lg"
74-
)
75-
end
76-
end
77-
7868
defp bundlephobia(query) do
7969
case Spec.search_for(:bundlephobia, query) do
8070
# %{"assets" => [%{"gzip" => 3920, "name" => "main", "size" => 10047, "type" => "js"}], "dependencyCount" => 0, "dependencySizes" => [%{"approximateSize" => 9537, "name" => "preact"}], "description" => "Fast 3kb React-compatible Virtual DOM library.", "gzip" => 3920, "hasJSModule" => "dist/preact.module.js", "hasJSNext" => false, "hasSideEffects" => true, "name" => "preact", "repository" => "https://github.com/preactjs/preact.git", "scoped" => false, "size" => 10047, "version" => "10.4.1"}
@@ -261,20 +251,9 @@ defmodule ComponentsGuideWeb.ResearchController do
261251
])
262252
end
263253
end
264-
254+
265255
defp static(query) do
266-
for result <- Static.search_for(query) do
267-
case result do
268-
{:ok, name, description} ->
269-
Section.card([
270-
content_tag(:h3, name, class: "text-2xl"),
271-
content_tag(:p, description)
272-
])
273-
274-
_ ->
275-
""
276-
end
277-
end
256+
Enum.map(Static.search_for(query), &View.render_static/1)
278257
end
279258

280259
defp load_results(query) when is_binary(query) do
@@ -290,3 +269,77 @@ defmodule ComponentsGuideWeb.ResearchController do
290269
]
291270
end
292271
end
272+
273+
defmodule ComponentsGuideWeb.ResearchView do
274+
use ComponentsGuideWeb, :view
275+
276+
def results(_query) do
277+
~E"""
278+
Content!
279+
"""
280+
end
281+
282+
defdelegate float_to_string(f, options), to: :erlang, as: :float_to_binary
283+
284+
def humanize_bytes(count) when count >= 1024 * 1024 do
285+
megabytes = count / (1024 * 1024)
286+
"#{float_to_string(megabytes, decimals: 1)} mB"
287+
end
288+
289+
def humanize_bytes(count) when count >= 1024 do
290+
kilobytes = count / 1024
291+
"#{float_to_string(kilobytes, decimals: 1)} kB"
292+
end
293+
294+
def humanize_bytes(count) when is_integer(count) do
295+
"#{count} B"
296+
end
297+
298+
def humanize_count(count) when count >= 1_000_000 do
299+
formatted = count / 1_000_000
300+
"#{float_to_string(formatted, decimals: 1)}M"
301+
end
302+
303+
def humanize_count(count) when count >= 1000 do
304+
formatted = count / 1000
305+
"#{float_to_string(formatted, decimals: 1)}K"
306+
end
307+
308+
def humanize_count(count) when is_integer(count) do
309+
"#{count}"
310+
end
311+
312+
defmodule Section do
313+
def card(children) do
314+
content_tag(
315+
:article,
316+
children,
317+
class:
318+
"mb-4 text-xl spacing-y-4 p-4 text-white bg-indigo-900 border border-indigo-800 rounded-lg shadow-lg"
319+
)
320+
end
321+
end
322+
323+
def render_static({type, info}) do
324+
render_static(type, info)
325+
end
326+
327+
def render_static(:http_status, {name, description}) do
328+
Section.card([
329+
content_tag(:h3, "HTTP Status: #{name}", class: "text-2xl"),
330+
content_tag(:p, description)
331+
])
332+
end
333+
334+
def render_static(:rfc, {name, rfcs, metadata}) do
335+
Section.card([
336+
content_tag(:h3, "RFC: #{name}", class: "text-2xl"),
337+
content_tag(:p, inspect(rfcs)),
338+
content_tag(:p, inspect(metadata)),
339+
content_tag(:dl, [
340+
content_tag(:dt, "Media (MIME) Type"),
341+
content_tag(:dd, Keyword.get(metadata, :media_type))
342+
])
343+
])
344+
end
345+
end

apps/components_guide_web/lib/components_guide_web/views/research_view.ex

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)