Skip to content

Commit fcb999b

Browse files
committed
Fix search
1 parent f51eaaa commit fcb999b

File tree

3 files changed

+103
-73
lines changed

3 files changed

+103
-73
lines changed

apps/components_guide/lib/components_guide/research/source.ex

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,40 +79,53 @@ defmodule ComponentsGuide.Research.Source do
7979
# {:ok, html}
8080
# end
8181

82-
Fetch.get(url)
82+
# Fetch.get(url)
8383

84-
# {:ok, response} = Mojito.request(method: :get, url: url, timeout: 50000)
85-
# {:ok, response.body}
84+
with {:ok, response} <- Mojito.request(method: :get, url: url, timeout: 50000) do
85+
{:ok, response.body}
86+
else
87+
_ -> :err
88+
end
8689
end
8790

8891
defp body({:html_document, url}) do
89-
{:ok, html} = read({:fetch, url})
90-
tuple = {:ok, _document} = Floki.parse_document(html)
91-
tuple
92+
with {:ok, html} <- read({:fetch, url}),
93+
{:ok, document} <- Floki.parse_document(html) do
94+
{:ok, document}
95+
else
96+
_ -> :err
97+
end
9298
end
9399

94100
defp body({:fetch_json, url}) do
95-
{:ok, encoded} = read({:fetch, url})
96-
tuple = {:ok, data} = Jason.decode(encoded)
97-
tuple
101+
with {:ok, encoded} <- read({:fetch, url}),
102+
{:ok, data} <- Jason.decode(encoded) do
103+
{:ok, data}
104+
else
105+
_ -> :err
106+
end
98107
end
99108

100109
defp run(key) do
101-
tuple = {:ok, value} = body(key)
102-
write_cache(key, value)
103-
tuple
110+
with {:ok, value} <- body(key) do
111+
write_cache(key, value)
112+
{:ok, value}
113+
else
114+
_ -> write_cache(key, :err)
115+
:err
116+
end
104117
end
105118

106119
defp read(key) do
107120
case read_cache(key) do
108121
{:ok, nil} ->
109122
run(key)
110123

124+
{:ok, :err} ->
125+
:err
126+
111127
{:ok, value} ->
112128
{:ok, value}
113-
114-
other ->
115-
other
116129
end
117130
end
118131

apps/components_guide/lib/components_guide/research/spec.ex

Lines changed: 72 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,60 @@ defmodule ComponentsGuide.Research.Spec do
33

44
def search_for(:caniuse, query) when is_binary(query) do
55
url = "https://cdn.jsdelivr.net/npm/[email protected]/data.json"
6-
{:ok, data} = Source.json_at(url)
6+
result = Source.json_at(url)
7+
process_search_for(:caniuse, query, result)
8+
end
9+
10+
def search_for(:npm_downloads_last_month, query) when is_binary(query) do
11+
query = String.trim(query)
12+
url = "https://api.npmjs.org/downloads/point/last-month/#{query}"
13+
case Source.json_at(url) do
14+
{:ok, %{"downloads" => downloads_count, "package" => name}} ->
15+
%{downloads_count: downloads_count, name: name}
16+
17+
{:ok, %{"error" => _error_message}} ->
18+
nil
19+
20+
_ ->
21+
nil
22+
end
23+
end
24+
25+
def search_for(:whatwg_html_spec, query) when is_binary(query) do
26+
# url = "https://html.spec.whatwg.org/"
27+
url = "https://html.spec.whatwg.org/dev/"
28+
29+
case Source.html_document_at(url) do
30+
{:ok, document} ->
31+
document
32+
|> Floki.find("body")
33+
|> Floki.find("a:fl-contains('#{query}')")
34+
|> Floki.raw_html()
35+
36+
_ ->
37+
""
38+
end
39+
end
40+
41+
def search_for(:html_aria_spec, query) when is_binary(query) do
42+
url = "https://www.w3.org/TR/html-aria/"
43+
result = Source.html_document_at(url)
44+
process_search_for(:html_aria_spec, query, result)
45+
end
46+
47+
def search_for(:wai_aria_practices, query) when is_binary(query) do
48+
url = "https://www.w3.org/TR/wai-aria-practices/"
49+
result = Source.html_document_at(url)
50+
process_search_for(:wai_aria_practices, query, result)
51+
end
52+
53+
def search_for(:bundlephobia, query) when is_binary(query) do
54+
{:ok, data} = Source.json_at("https://bundlephobia.com/api/size?package=#{query}")
55+
56+
data
57+
end
58+
59+
defp process_search_for(:caniuse, query, {:ok, data}) when is_binary(query) do
760
table = data["data"]
861

962
if false do
@@ -24,39 +77,29 @@ defmodule ComponentsGuide.Research.Spec do
2477
end
2578
end
2679

27-
def search_for(:npm_downloads_last_month, query) when is_binary(query) do
28-
query = String.trim(query)
29-
url = "https://api.npmjs.org/downloads/point/last-month/#{query}"
30-
{:ok, data} = Source.json_at(url)
31-
32-
case data do
33-
%{"downloads" => downloads_count, "package" => name} ->
34-
%{downloads_count: downloads_count, name: name}
35-
36-
%{"error" => _error_message} ->
37-
nil
80+
defp process_search_for(:wai_aria_practices, query, {:ok, document}) when is_binary(query) do
81+
html_elements = document |> Floki.find("[id*='#{query}']")
3882

39-
_ ->
40-
nil
41-
end
42-
end
83+
results =
84+
Enum.map(html_elements, fn el ->
85+
heading = Floki.find(el, "h1, h2, h3, h4")
4386

44-
def search_for(:whatwg_html_spec, query) when is_binary(query) do
45-
# url = "https://html.spec.whatwg.org/"
46-
url = "https://html.spec.whatwg.org/dev/"
87+
%{
88+
heading: heading |> Floki.text(),
89+
html: el
90+
}
91+
end)
4792

48-
{:ok, document} = Source.html_document_at(url)
93+
results
4994

50-
document
51-
|> Floki.find("body")
52-
|> Floki.find("a:fl-contains('#{query}')")
53-
|> Floki.raw_html()
95+
# document
96+
# # |> Floki.find("#toc li a")
97+
# # |> Floki.find("[href*='#{query}']")
98+
# |> Floki.find("[id*='#{query}']")
99+
# |> Floki.raw_html()
54100
end
55101

56-
def search_for(:html_aria_spec, query) when is_binary(query) do
57-
url = "https://www.w3.org/TR/html-aria/"
58-
{:ok, document} = Source.html_document_at(url)
59-
102+
defp process_search_for(:html_aria_spec, query, {:ok, document}) do
60103
html_elements =
61104
document
62105
|> Floki.find(
@@ -101,34 +144,5 @@ defmodule ComponentsGuide.Research.Spec do
101144
end)
102145
end
103146

104-
def search_for(:wai_aria_practices, query) when is_binary(query) do
105-
url = "https://www.w3.org/TR/wai-aria-practices/"
106-
{:ok, document} = Source.html_document_at(url)
107-
108-
html_elements = document |> Floki.find("[id*='#{query}']")
109-
110-
results =
111-
Enum.map(html_elements, fn el ->
112-
heading = Floki.find(el, "h1, h2, h3, h4")
113-
114-
%{
115-
heading: heading |> Floki.text(),
116-
html: el
117-
}
118-
end)
119-
120-
results
121-
122-
# document
123-
# # |> Floki.find("#toc li a")
124-
# # |> Floki.find("[href*='#{query}']")
125-
# |> Floki.find("[id*='#{query}']")
126-
# |> Floki.raw_html()
127-
end
128-
129-
def search_for(:bundlephobia, query) when is_binary(query) do
130-
{:ok, data} = Source.json_at("https://bundlephobia.com/api/size?package=#{query}")
131-
132-
data
133-
end
147+
defp process_search_for(_id, _query, _), do: []
134148
end

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ defmodule ComponentsGuideWeb.ResearchController do
182182
[] ->
183183
[]
184184

185+
:err ->
186+
[]
187+
185188
results ->
186189
content_tag(:article, [
187190
h2("HTML spec"),

0 commit comments

Comments
 (0)