@@ -2,8 +2,6 @@ defmodule Iconify.Fetcher do
22 @ moduledoc """
33 Fetches icon data from Iconify sources.
44
5- Requires the `req` dependency to be installed.
6-
75 ## Sources
86
97 * **NPM** - Fetches complete icon sets from `@iconify-json/{prefix}` packages
@@ -37,8 +35,6 @@ defmodule Iconify.Fetcher do
3735 """
3836 @ spec fetch_set ( String . t ( ) ) :: { :ok , Set . t ( ) } | { :error , term ( ) }
3937 def fetch_set ( prefix ) when is_binary ( prefix ) do
40- ensure_req! ( )
41-
4238 with { :ok , tarball_url } <- get_tarball_url ( prefix ) ,
4339 { :ok , json } <- download_and_extract_icons ( tarball_url ) do
4440 Set . parse ( json )
@@ -59,29 +55,12 @@ defmodule Iconify.Fetcher do
5955 @ spec fetch_icons ( String . t ( ) , [ String . t ( ) ] ) ::
6056 { :ok , % { String . t ( ) => Icon . t ( ) } } | { :error , term ( ) }
6157 def fetch_icons ( prefix , names ) when is_binary ( prefix ) and is_list ( names ) do
62- ensure_req! ( )
63-
6458 icons_param = Enum . join ( names , "," )
6559 url = "#{ @ iconify_api } /#{ prefix } .json?icons=#{ icons_param } "
6660
67- with { :ok , data } <- req_get_json ( url ) do
68- default_width = data [ "width" ] || 24
69- default_height = data [ "height" ] || 24
70-
71- defaults = [
72- width: default_width ,
73- height: default_height ,
74- left: data [ "left" ] || 0 ,
75- top: data [ "top" ] || 0
76- ]
77-
78- icons =
79- data
80- |> Map . get ( "icons" , % { } )
81- |> Map . new ( fn { name , icon_data } ->
82- { name , Icon . new ( name , icon_data , defaults ) }
83- end )
84-
61+ with { :ok , json } <- req_get_body ( url ) ,
62+ { :ok , set } <- Set . parse ( json ) do
63+ icons = Map . new ( names , fn name -> { name , Set . get! ( set , name ) } end )
8564 { :ok , icons }
8665 end
8766 end
@@ -127,7 +106,7 @@ defmodule Iconify.Fetcher do
127106 end
128107
129108 defp download_and_extract_icons ( tarball_url ) do
130- with { :ok , body } <- req_get_binary ( tarball_url ) do
109+ with { :ok , body } <- req_get_body ( tarball_url ) do
131110 extract_icons_json ( body )
132111 end
133112 end
@@ -145,34 +124,18 @@ defmodule Iconify.Fetcher do
145124 end
146125 end
147126
148- defp req_get_json ( url ) do
149- case Req . get ( url ) do
150- { :ok , % { status: 200 , body: body } } when is_map ( body ) -> { :ok , body }
151- { :ok , % { status: 200 , body: body } } when is_binary ( body ) -> Jason . decode ( body )
152- { :ok , % { status: 404 } } -> { :error , :not_found }
153- { :ok , % { status: status } } -> { :error , { :http_error , status } }
154- { :error , reason } -> { :error , reason }
127+ defp req_get_json ( url , opts \\ [ ] ) do
128+ with { :ok , body } <- req_get_body ( url ) do
129+ Jason . decode ( body , opts )
155130 end
156131 end
157132
158- defp req_get_binary ( url ) do
133+ defp req_get_body ( url ) do
159134 case Req . get ( url , decode_body: false ) do
160135 { :ok , % { status: 200 , body: body } } -> { :ok , body }
161136 { :ok , % { status: 404 } } -> { :error , :not_found }
162137 { :ok , % { status: status } } -> { :error , { :http_error , status } }
163138 { :error , reason } -> { :error , reason }
164139 end
165140 end
166-
167- defp ensure_req! do
168- unless Code . ensure_loaded? ( Req ) do
169- raise """
170- The :req dependency is required for fetching icons.
171-
172- Add it to your mix.exs:
173-
174- {:req, "~> 0.5"}
175- """
176- end
177- end
178141end
0 commit comments