Skip to content

Commit b2c8fe0

Browse files
authored
Merge pull request #1 from elixir-volt/fix/unknown-icon-names
Leave unknown names out of fetch_icons instead of raising
2 parents 06897bc + d452e92 commit b2c8fe0

8 files changed

Lines changed: 39 additions & 8 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- Leave unknown names out of `Iconify.Fetcher.fetch_icons/2` instead of raising, so `fetch_icon/2` returns `{:error, :not_found}` for them as documented.
6+
- Parse icon sets and icons with JSONCodec 0.3.
7+
38
## v0.3.0
49

510
- Replace SVG IDs during rendering with Erlang's `:xmerl` to avoid duplicate ID collisions

‎lib/iconify/fetcher.ex‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ defmodule Iconify.Fetcher do
4444
@doc """
4545
Fetches specific icons from the Iconify API.
4646
47-
Returns a map of icon name to Icon struct.
47+
Returns a map of icon name to Icon struct. Names the set doesn't have are left out.
4848
4949
## Examples
5050
@@ -60,8 +60,7 @@ defmodule Iconify.Fetcher do
6060

6161
with {:ok, json} <- req_get_body(url),
6262
{:ok, set} <- Set.parse(json) do
63-
icons = Map.new(names, fn name -> {name, Set.get!(set, name)} end)
64-
{:ok, icons}
63+
{:ok, for(name <- names, {:ok, icon} <- [Set.get(set, name)], into: %{}, do: {name, icon})}
6564
end
6665
end
6766

‎lib/iconify/icon.ex‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule Iconify.Icon do
33
Represents a single normalized Iconify icon.
44
"""
55

6-
use JSONCodec, case: :camel, fast_path: :json
6+
use JSONCodec, case: :camel
77

88
@derive Jason.Encoder
99
defstruct [

‎lib/iconify/set.ex‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule Iconify.Set do
33
Represents an Iconify icon set (collection of icons with a common prefix).
44
"""
55

6-
use JSONCodec, case: :camel, fast_path: :json
6+
use JSONCodec, case: :camel
77

88
alias Iconify.Icon
99

‎mix.exs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ defmodule Iconify.MixProject do
2828
defp deps do
2929
[
3030
{:jason, "~> 1.4"},
31-
{:json_codec, path: "../json_codec"},
31+
{:json_codec, "~> 0.3"},
3232
{:req, "~> 0.5"},
3333
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},
3434
{:reach, "~> 2.0", only: [:dev, :test], runtime: false},
@@ -38,11 +38,12 @@ defmodule Iconify.MixProject do
3838
end
3939

4040
def cli do
41-
[preferred_envs: [ci: :test]]
41+
[preferred_envs: [ci: :test, "test.e2e": :test]]
4242
end
4343

4444
defp aliases do
4545
[
46+
"test.e2e": ["test --only e2e"],
4647
ci: [
4748
"compile --warnings-as-errors",
4849
"format --check-formatted",

‎mix.lock‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"finch": {:hex, :finch, "0.21.0", "b1c3b2d48af02d0c66d2a9ebfb5622be5c5ecd62937cf79a88a7f98d48a8290c", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.6.2 or ~> 1.7", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.1", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "87dc6e169794cb2570f75841a19da99cfde834249568f2a5b121b809588a4377"},
1010
"hpax": {:hex, :hpax, "1.0.3", "ed67ef51ad4df91e75cc6a1494f851850c0bd98ebc0be6e81b026e765ee535aa", [:mix], [], "hexpm", "8eab6e1cfa8d5918c2ce4ba43588e894af35dbd8e91e6e55c817bca5847df34a"},
1111
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
12+
"json_codec": {:hex, :json_codec, "0.3.1", "81f738e212308c85344e11da00af6d8b52bd486224ee24c1f51b95df0604aeae", [:mix], [{:jason, "~> 1.4", [hex: :jason, repo: "hexpm", optional: true]}, {:json_spec, "~> 1.2", [hex: :json_spec, repo: "hexpm", optional: false]}], "hexpm", "dffea3198d3f1fe9a39aa176ab0d6988b8c80dd85a67e913b323d4ec7c57ca9d"},
13+
"json_spec": {:hex, :json_spec, "1.2.0", "c67f73cdc4166e955b2f1095d3f44dd78c197337038f1a5301fb526b6c87ab04", [:mix], [], "hexpm", "3639961babad7d6bc45f3a2d96a49c18ec70614761bb84ce58b64e9fad9354de"},
1214
"libgraph": {:hex, :libgraph, "0.16.0", "3936f3eca6ef826e08880230f806bfea13193e49bf153f93edcf0239d4fd1d07", [:mix], [], "hexpm", "41ca92240e8a4138c30a7e06466acc709b0cbb795c643e9e17174a178982d6bf"},
1315
"makeup": {:hex, :makeup, "1.2.1", "e90ac1c65589ef354378def3ba19d401e739ee7ee06fb47f94c687016e3713d1", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d36484867b0bae0fea568d10131197a4c2e47056a6fbe84922bf6ba71c8d17ce"},
1416
"makeup_elixir": {:hex, :makeup_elixir, "1.0.1", "e928a4f984e795e41e3abd27bfc09f51db16ab8ba1aebdba2b3a575437efafc2", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "7284900d412a3e5cfd97fdaed4f5ed389b8f2b4cb49efc0eb3bd10e2febf9507"},

‎test/e2e/iconify/fetcher_test.exs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
defmodule Iconify.E2E.FetcherTest do
2+
# Talks to the Iconify API. Run with `mix test.e2e`.
3+
use ExUnit.Case, async: true
4+
5+
alias Iconify.Fetcher
6+
7+
@moduletag :e2e
8+
9+
setup do
10+
{:ok, _} = Application.ensure_all_started(:req)
11+
:ok
12+
end
13+
14+
test "fetch_icons/2 leaves out names the set doesn't have" do
15+
assert {:ok, icons} = Fetcher.fetch_icons("lucide", ["mail", "no-such-icon-anywhere"])
16+
assert Map.keys(icons) == ["mail"]
17+
assert icons["mail"].body =~ "<"
18+
end
19+
20+
test "fetch_icon/2 reports an unknown name as not found" do
21+
assert {:ok, %Iconify.Icon{}} = Fetcher.fetch_icon("lucide", "layer-arrow-up")
22+
assert Fetcher.fetch_icon("lucide", "no-such-icon-anywhere") == {:error, :not_found}
23+
end
24+
end

‎test/test_helper.exs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ExUnit.start()
1+
ExUnit.start(exclude: [:e2e])

0 commit comments

Comments
 (0)