Current section

7 Versions

Jump to

Compare versions

7 files changed
+66 additions
-22 deletions
  @@ -0,0 +1,21 @@
1 + MIT License
2 +
3 + Copyright (c) 2019 Bruno Ukita
4 +
5 + Permission is hereby granted, free of charge, to any person obtaining a copy
6 + of this software and associated documentation files (the "Software"), to deal
7 + in the Software without restriction, including without limitation the rights
8 + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 + copies of the Software, and to permit persons to whom the Software is
10 + furnished to do so, subject to the following conditions:
11 +
12 + The above copyright notice and this permission notice shall be included in all
13 + copies or substantial portions of the Software.
14 +
15 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 + SOFTWARE.
  @@ -11,7 +11,7 @@ Add it to your deps list in your mix.exs
11 11 ```elixir
12 12 def deps do
13 13 [
14 - {:emojix, "~> 0.2.0"}
14 + {:emojix, "~> 0.3.0"}
15 15 ]
16 16 end
17 17 ```
  @@ -5,10 +5,11 @@
5 5 32,102,111,114,32,69,108,105,120,105,114,46,32,240,159,146,169>>}.
6 6 {<<"elixir">>,<<"~> 1.9">>}.
7 7 {<<"files">>,
8 - [<<"lib">>,<<"lib/emojix.ex">>,<<"lib/emojix">>,<<"lib/emojix/emoji.ex">>,
9 - <<"lib/emojix/data_loader.ex">>,<<"lib/emojix/repo.ex">>,
10 - <<"lib/emojix/app.ex">>,<<"priv">>,<<"priv/dataset.ets">>,
11 - <<".formatter.exs">>,<<"mix.exs">>,<<"README.md">>]}.
8 + [<<"lib">>,<<"lib/emojix">>,<<"lib/emojix/repo.ex">>,
9 + <<"lib/emojix/data_loader.ex">>,<<"lib/emojix/app.ex">>,
10 + <<"lib/emojix/emoji.ex">>,<<"lib/emojix.ex">>,<<"priv">>,
11 + <<"priv/dataset.ets">>,<<".formatter.exs">>,<<"mix.exs">>,<<"README.md">>,
12 + <<"LICENSE">>]}.
12 13 {<<"licenses">>,[<<"MIT">>]}.
13 14 {<<"links">>,[{<<"GitHub">>,<<"https://github.com/ukita/emojix">>}]}.
14 15 {<<"name">>,<<"emojix">>}.
  @@ -28,4 +29,4 @@
28 29 {<<"optional">>,false},
29 30 {<<"repository">>,<<"hexpm">>},
30 31 {<<"requirement">>,<<"~> 0.4.0">>}]]}.
31 - {<<"version">>,<<"0.2.0">>}.
32 + {<<"version">>,<<"0.3.0">>}.
  @@ -58,7 +58,7 @@ defmodule Emojix do
58 58 @doc """
59 59 Find a emoji by unicode
60 60 ## Examples
61 - iex> Emojix.find_by_shortcode("😼")
61 + iex> Emojix.find_by_unicode("😼")
62 62 %Emojix.Emoji{
63 63 description: "cat with wry smile",
64 64 hexcode: "1F63C",
  @@ -7,7 +7,9 @@ defmodule Emojix.DataLoader do
7 7
8 8 @ets_table_path Application.app_dir(:emojix, "priv/dataset.ets")
9 9 @download_host "cdn.jsdelivr.net"
10 - @download_path "/npm/emojibase-data@4.0.2/en/compact.json"
10 + @download_path "/npm/emojibase-data@6.0.0/en/compact.json"
11 + @download_shortcodes "/npm/emojibase-data@6.0.0/en/shortcodes/iamcal.json"
12 + @download_legacy_shortcodes "/npm/emojibase-data@6.0.0/en/shortcodes/emojibase-legacy.json"
11 13
12 14 @spec load_table :: :emoji_table
13 15 def load_table do
  @@ -23,26 +25,42 @@ defmodule Emojix.DataLoader do
23 25 @spec download_and_populate :: :emoji_table
24 26 def download_and_populate do
25 27 Logger.debug("Downloading emoji dataset")
28 + json = Jason.decode!(download_file(@download_path), keys: :atoms)
29 + json_shortcodes = Jason.decode!(download_file(@download_shortcodes), keys: :strings)
30 +
31 + json_legacy_shortcodes =
32 + Jason.decode!(download_file(@download_legacy_shortcodes), keys: :strings)
33 +
34 + shortcodes = merge_shortcodes(json_shortcodes, json_legacy_shortcodes)
35 +
36 + create_table(json, shortcodes)
37 + end
38 +
39 + defp download_file(path) do
26 40 {:ok, conn} = HTTP.connect(:https, @download_host, 443)
27 41
28 42 {:ok, conn, request_ref} =
29 - HTTP.request(conn, "GET", @download_path, [{"content-type", "application/json"}], "")
43 + HTTP.request(conn, "GET", path, [{"content-type", "application/json"}], "")
30 44
31 45 {:ok, conn, body} = stream_request(conn, request_ref)
32 46
33 47 Mint.HTTP.close(conn)
34 -
35 - json = Jason.decode!(body, keys: :atoms)
36 -
37 - create_table(json)
48 + body
38 49 end
39 50
40 - defp create_table(json) do
51 + defp merge_shortcodes(shortcodes, legacy_shortcodes) do
52 + Map.merge(shortcodes, legacy_shortcodes, fn _k, v1, v2 ->
53 + (List.wrap(v1 || []) ++ List.wrap(v2 || []))
54 + |> Enum.uniq()
55 + end)
56 + end
57 +
58 + defp create_table(json, json_shortcodes) do
41 59 Logger.debug("Building emoji ets table")
42 60 table = :ets.new(:emoji_table, [:named_table])
43 61
44 62 emoji_list =
45 - parse_json(json)
63 + parse_json(json, json_shortcodes)
46 64 |> Enum.reduce([], fn e, acc ->
47 65 e.variations ++ [e | acc]
48 66 end)
  @@ -62,21 +80,25 @@ defmodule Emojix.DataLoader do
62 80 :emoji_table
63 81 end
64 82
65 - defp parse_json(json) do
66 - Enum.reduce(json, [], fn emoji, list ->
67 - [build_emoji_struct(emoji) | list]
83 + defp parse_json(json, json_shortcodes) do
84 + json
85 + |> Stream.filter(&Map.has_key?(&1, :order))
86 + |> Enum.reduce([], fn emoji, list ->
87 + [build_emoji_struct(emoji, json_shortcodes) | list]
68 88 end)
69 89 end
70 90
71 - defp build_emoji_struct(emoji) do
91 + defp build_emoji_struct(emoji, json_shortcodes) do
92 + shortcodes = Map.get(json_shortcodes, emoji.hexcode, [])
93 +
72 94 %Emojix.Emoji{
73 95 id: emoji.order,
74 96 hexcode: emoji.hexcode,
75 97 description: emoji.annotation,
76 - shortcodes: emoji.shortcodes,
98 + shortcodes: List.wrap(shortcodes),
77 99 unicode: emoji.unicode,
78 100 tags: Map.get(emoji, :tags, []),
79 - variations: Map.get(emoji, :skins, []) |> Enum.map(&build_emoji_struct/1)
101 + variations: Map.get(emoji, :skins, []) |> Enum.map(&build_emoji_struct(&1, json_shortcodes))
80 102 }
81 103 end
Loading more files…