Browse Source

Autotagging in worker \o/

pull/26/head
Inhji Y. 1 year ago
parent
commit
cc5eac8840
  1. 6
      apps/bookmarks/lib/bookmarks/worker.ex
  2. 1
      apps/bookmarks/mix.exs
  3. 23
      apps/bookmarks/test/worker_test.exs
  4. 2
      apps/tomie_web/lib/tomie_web/controllers/tag_controller.ex
  5. 2
      mix.lock

6
apps/bookmarks/lib/bookmarks/worker.ex

@ -4,6 +4,12 @@ defmodule Bookmarks.Worker do
def perform(%Bookmarks.Bookmark{source: source} = bookmark) do
{:ok, html} = Scraper.get_html(source)
{:ok, result} = Scraper.parse(html)
Tags.list_tags()
|> Enum.filter(fn %{rules: rules} -> not is_nil(rules) end)
|> Tags.Rules.parse(bookmark)
|> Bookmarks.update_tags(bookmark)
Bookmarks.update_bookmark(bookmark, %{title: result.title})
end
end

1
apps/bookmarks/mix.exs

@ -28,6 +28,7 @@ defmodule Bookmarks.MixProject do
[
{:que, "~> 0.10.1"},
{:ecto_sql, "~> 3.1"},
{:mock, "~> 0.3.0", only: :test},
{:excoveralls, "~> 0.12.3", only: :test},
{:db, in_umbrella: true},
{:tags, in_umbrella: true},

23
apps/bookmarks/test/worker_test.exs

@ -0,0 +1,23 @@
defmodule WorkerTest do
use ExUnit.Case
import Mock
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Db.Repo)
end
@github_response {:ok, %{status_code: 200, body: "<html></html>"}}
test "perform/1" do
with_mock HTTPoison, get: fn _url, _headers -> @github_response end do
{:ok, tag} = Tags.create_tag(%{name: "github", rules: "source::contains::github"})
{:ok, bookmark} = Bookmarks.create_bookmark(%{source: "https://github.com"})
Bookmarks.Worker.perform(bookmark)
updated_bookmark = Bookmarks.get_bookmark!(bookmark.id)
assert [tag] == updated_bookmark.tags
end
end
end

2
apps/tomie_web/lib/tomie_web/controllers/tag_controller.ex

@ -17,7 +17,7 @@ defmodule TomieWeb.TagController do
def create(conn, %{"tag" => tag_params}) do
case Tags.create_tag(tag_params) do
{:ok, tag} ->
{:ok, _tag} ->
conn
|> put_flash(:info, @tag_created)
|> redirect(to: Routes.tag_path(conn, :index))

2
mix.lock

@ -25,10 +25,12 @@
"jason": {:hex, :jason, "1.2.0", "10043418c42d2493d0ee212d3fddd25d7ffe484380afad769a0a38795938e448", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "116747dbe057794c3a3e4e143b7c8390b29f634e16c78a7f59ba75bfa6852e7f"},
"makeup": {:hex, :makeup, "1.0.1", "82f332e461dc6c79dbd82fbe2a9c10d48ed07146f0a478286e590c83c52010b5", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "49736fe5b66a08d8575bf5321d716bac5da20c8e6b97714fec2bcd6febcfa1f8"},
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "d4b316c7222a85bbaa2fd7c6e90e37e953257ad196dc229505137c5e505e9eff"},
"meck": {:hex, :meck, "0.8.13", "ffedb39f99b0b99703b8601c6f17c7f76313ee12de6b646e671e3188401f7866", [:rebar3], [], "hexpm", "d34f013c156db51ad57cc556891b9720e6a1c1df5fe2e15af999c84d6cebeb1a"},
"memento": {:hex, :memento, "0.3.1", "b2909390820550d8b90b68ec96f9e15ff8a45a28b6f97fa4a62ef50e87c2f9d9", [:mix], [], "hexpm", "ff8fc66255d21dcd539c5d77a0b5458715bf3efec91b389dd06017bbb4e2e916"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm", "6cbe761d6a0ca5a31a0931bf4c63204bceb64538e664a8ecf784a9a6f3b875f1"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"mock": {:hex, :mock, "0.3.4", "c5862eb3b8c64237f45f586cf00c9d892ba07bb48305a43319d428ce3c2897dd", [:mix], [{:meck, "~> 0.8.13", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm", "e6d886252f1a41f4ba06ecf2b4c8d38760b34b1c08a11c28f7397b2e03995964"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.3", "def21c10a9ed70ce22754fdeea0810dafd53c2db3219a0cd54cf5526377af1c6", [:mix], [], "hexpm", "589b5af56f4afca65217a1f3eb3fee7e79b09c40c742fddc1c312b3ac0b3399f"},
"open_graph_extended": {:git, "https://git.inhji.de/inhji/open_graph.git", "bed84f89524ec5ba12ab85018ff6f166cfff7a3d", []},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},

Loading…
Cancel
Save