Packages

pify: some helpers to use gleam in your shopify project.

Current section

Files

Jump to
pify src storefront@utils.erl
Raw

src/storefront@utils.erl

-module(storefront@utils).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/storefront/utils.gleam").
-export([remove_edges_and_nodes/1, image_to_json/1, image_decoder/0, money_to_json/1, price_range_to_json/1, money_decoder/0, price_range_decoder/0, seo_to_json/1, seo_decoder/0, image_edges_decoder/0, image_connection_decoder/0, product_option_decoder/0, product_variant_to_json/1, selected_option_decoder/0, product_variant_decoder/0, cost_decoder/0, reshape_images/2]).
-export_type(['maybe'/1, connection/1, edge/1, image/0, images/0, price_range/0, money/0, product_variant/0, seo/0, product_option/0, selected_option/0, cost/0]).
-type 'maybe'(JEX) :: {'maybe', gleam@option:option(JEX)}.
-type connection(JEY) :: {connection, list(edge(JEY))}.
-type edge(JEZ) :: {edge, JEZ}.
-type image() :: {image, binary(), binary(), binary(), binary()}.
-type images() :: {images, list(image())}.
-type price_range() :: {price_range, money(), money()}.
-type money() :: {money, binary(), binary()}.
-type product_variant() :: {product_variant,
binary(),
binary(),
boolean(),
selected_option(),
money()}.
-type seo() :: {seo, binary(), binary()}.
-type product_option() :: {product_option, binary(), binary(), list(binary())}.
-type selected_option() :: {selected_option, binary(), binary()}.
-type cost() :: {cost, money()}.
-file("src/storefront/utils.gleam", 16).
-spec remove_edges_and_nodes(connection(JFA)) -> list(JFA).
remove_edges_and_nodes(Array) ->
_pipe = erlang:element(2, Array),
gleam@list:map(_pipe, fun(Nodes) -> erlang:element(2, Nodes) end).
-file("src/storefront/utils.gleam", 41).
-spec image_to_json(image()) -> gleam@json:json().
image_to_json(Image) ->
{image, Url, Alt_text, Width, Height} = Image,
gleam@json:object(
[{<<"url"/utf8>>, gleam@json:string(Url)},
{<<"altText"/utf8>>, gleam@json:string(Alt_text)},
{<<"width"/utf8>>, gleam@json:string(Width)},
{<<"height"/utf8>>, gleam@json:string(Height)}]
).
-file("src/storefront/utils.gleam", 51).
-spec image_decoder() -> gleam@dynamic@decode:decoder(image()).
image_decoder() ->
gleam@dynamic@decode:field(
<<"url"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Url) ->
gleam@dynamic@decode:field(
<<"altText"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Alt_text) ->
gleam@dynamic@decode:field(
<<"width"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Width) ->
gleam@dynamic@decode:field(
<<"height"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Height) ->
gleam@dynamic@decode:success(
{image, Url, Alt_text, Width, Height}
)
end
)
end
)
end
)
end
).
-file("src/storefront/utils.gleam", 81).
-spec money_to_json(money()) -> gleam@json:json().
money_to_json(Money) ->
{money, Amount, Currency_code} = Money,
gleam@json:object(
[{<<"amount"/utf8>>, gleam@json:string(Amount)},
{<<"currencyCode"/utf8>>, gleam@json:string(Currency_code)}]
).
-file("src/storefront/utils.gleam", 63).
-spec price_range_to_json(price_range()) -> gleam@json:json().
price_range_to_json(Price_range) ->
{price_range, Max_variant_price, Min_variant_price} = Price_range,
gleam@json:object(
[{<<"maxVariantPrice"/utf8>>, money_to_json(Max_variant_price)},
{<<"minVariantPrice"/utf8>>, money_to_json(Min_variant_price)}]
).
-file("src/storefront/utils.gleam", 89).
-spec money_decoder() -> gleam@dynamic@decode:decoder(money()).
money_decoder() ->
gleam@dynamic@decode:field(
<<"amount"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Amount) ->
gleam@dynamic@decode:field(
<<"currencyCode"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Currency_code) ->
gleam@dynamic@decode:success({money, Amount, Currency_code})
end
)
end
).
-file("src/storefront/utils.gleam", 71).
-spec price_range_decoder() -> gleam@dynamic@decode:decoder(price_range()).
price_range_decoder() ->
gleam@dynamic@decode:field(
<<"maxVariantPrice"/utf8>>,
money_decoder(),
fun(Max_variant_price) ->
gleam@dynamic@decode:field(
<<"minVariantPrice"/utf8>>,
money_decoder(),
fun(Min_variant_price) ->
gleam@dynamic@decode:success(
{price_range, Max_variant_price, Min_variant_price}
)
end
)
end
).
-file("src/storefront/utils.gleam", 144).
-spec seo_to_json(seo()) -> gleam@json:json().
seo_to_json(Seo) ->
{seo, Title, Description} = Seo,
gleam@json:object(
[{<<"title"/utf8>>, gleam@json:string(Title)},
{<<"description"/utf8>>, gleam@json:string(Description)}]
).
-file("src/storefront/utils.gleam", 152).
-spec seo_decoder() -> gleam@dynamic@decode:decoder(seo()).
seo_decoder() ->
gleam@dynamic@decode:field(
<<"title"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Title) ->
gleam@dynamic@decode:field(
<<"description"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Description) ->
gleam@dynamic@decode:success({seo, Title, Description})
end
)
end
).
-file("src/storefront/utils.gleam", 163).
-spec image_edges_decoder() -> gleam@dynamic@decode:decoder(edge(image())).
image_edges_decoder() ->
gleam@dynamic@decode:field(
<<"node"/utf8>>,
image_decoder(),
fun(Node) -> gleam@dynamic@decode:success({edge, Node}) end
).
-file("src/storefront/utils.gleam", 158).
-spec image_connection_decoder() -> gleam@dynamic@decode:decoder(connection(image())).
image_connection_decoder() ->
gleam@dynamic@decode:field(
<<"edges"/utf8>>,
gleam@dynamic@decode:list(image_edges_decoder()),
fun(Edges) -> gleam@dynamic@decode:success({connection, Edges}) end
).
-file("src/storefront/utils.gleam", 190).
-spec product_option_decoder() -> gleam@dynamic@decode:decoder(product_option()).
product_option_decoder() ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:field(
<<"name"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Name) ->
gleam@dynamic@decode:field(
<<"values"/utf8>>,
gleam@dynamic@decode:list(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Values) ->
gleam@dynamic@decode:success(
{product_option, Id, Name, Values}
)
end
)
end
)
end
).
-file("src/storefront/utils.gleam", 201).
-spec selected_option_to_json(selected_option()) -> gleam@json:json().
selected_option_to_json(Selected_option) ->
{selected_option, Name, Value} = Selected_option,
gleam@json:object(
[{<<"name"/utf8>>, gleam@json:string(Name)},
{<<"value"/utf8>>, gleam@json:string(Value)}]
).
-file("src/storefront/utils.gleam", 105).
-spec product_variant_to_json(product_variant()) -> gleam@json:json().
product_variant_to_json(Product_variant) ->
{product_variant, Id, Title, Available_for_sale, Selected_options, Price} = Product_variant,
gleam@json:object(
[{<<"id"/utf8>>, gleam@json:string(Id)},
{<<"title"/utf8>>, gleam@json:string(Title)},
{<<"availableForSale"/utf8>>, gleam@json:bool(Available_for_sale)},
{<<"selectedOptions"/utf8>>,
selected_option_to_json(Selected_options)},
{<<"price"/utf8>>, money_to_json(Price)}]
).
-file("src/storefront/utils.gleam", 209).
-spec selected_option_decoder() -> gleam@dynamic@decode:decoder(selected_option()).
selected_option_decoder() ->
gleam@dynamic@decode:field(
<<"name"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Name) ->
gleam@dynamic@decode:field(
<<"value"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Value) ->
gleam@dynamic@decode:success({selected_option, Name, Value})
end
)
end
).
-file("src/storefront/utils.gleam", 122).
-spec product_variant_decoder() -> gleam@dynamic@decode:decoder(product_variant()).
product_variant_decoder() ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Id) ->
gleam@dynamic@decode:field(
<<"title"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Title) ->
gleam@dynamic@decode:field(
<<"availableForSale"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_bool/1},
fun(Available_for_sale) ->
gleam@dynamic@decode:field(
<<"selectedOptions"/utf8>>,
selected_option_decoder(),
fun(Selected_options) ->
gleam@dynamic@decode:field(
<<"price"/utf8>>,
money_decoder(),
fun(Price) ->
gleam@dynamic@decode:success(
{product_variant,
Id,
Title,
Available_for_sale,
Selected_options,
Price}
)
end
)
end
)
end
)
end
)
end
).
-file("src/storefront/utils.gleam", 219).
-spec cost_decoder() -> gleam@dynamic@decode:decoder(cost()).
cost_decoder() ->
gleam@dynamic@decode:field(
<<"totalAmount"/utf8>>,
money_decoder(),
fun(Total_amount) ->
gleam@dynamic@decode:success({cost, Total_amount})
end
).
-file("src/storefront/utils.gleam", 236).
-spec last_split(binary(), binary()) -> {ok, {binary(), binary()}} |
{error, nil}.
last_split(Str, Separator) ->
Reversed_str = gleam@string:reverse(Str),
Reversed_separator = gleam@string:reverse(Separator),
case gleam@string:split_once(Reversed_str, Reversed_separator) of
{ok, {After_reversed, Before_reversed}} ->
Before = gleam@string:reverse(Before_reversed),
After = gleam@string:reverse(After_reversed),
{ok, {Before, After}};
{error, _} ->
{error, nil}
end.
-file("src/storefront/utils.gleam", 224).
-spec get_filename(binary()) -> binary().
get_filename(Url) ->
Path_segment = case last_split(Url, <<"/"/utf8>>) of
{ok, {_, After}} ->
After;
{error, _} ->
Url
end,
case last_split(Path_segment, <<"."/utf8>>) of
{ok, {Before, _}} ->
Before;
{error, _} ->
Path_segment
end.
-file("src/storefront/utils.gleam", 168).
-spec reshape_images(connection(image()), binary()) -> list(image()).
reshape_images(Images, Product_title) ->
Flattened = remove_edges_and_nodes(Images),
_pipe = Flattened,
gleam@list:map(
_pipe,
fun(Image) ->
New_alt_text = case erlang:element(3, Image) of
<<""/utf8>> ->
Filename = get_filename(erlang:element(2, Image)),
<<<<Product_title/binary, "-"/utf8>>/binary,
Filename/binary>>;
Text ->
Text
end,
{image,
erlang:element(2, Image),
New_alt_text,
erlang:element(4, Image),
erlang:element(5, Image)}
end
).