Skip to content

Commit

Permalink
finalized first version of sign_writings
Browse files Browse the repository at this point in the history
  • Loading branch information
bitboxer committed Mar 3, 2019
1 parent 1c65b57 commit 2046443
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 12 deletions.
3 changes: 2 additions & 1 deletion lib/sign_dict/models/sign_writing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ defmodule SignDict.SignWriting do
:word,
:width,
:deleges_id,
:entry_id
:entry_id,
:state
])
|> cast_attachments(params, [:image])
|> validate_required([
Expand Down
28 changes: 21 additions & 7 deletions lib/sign_dict/services/fetch_deleges_data_for_entry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ defmodule SignDict.Services.FetchDelegesDataForEntry do
alias SignDict.Entry
alias SignDict.Repo

def fetch_for(entry) do
import Ecto.Query

def fetch_for(entry, http_poison \\ HTTPoison) do
result =
HTTPoison.get!(
http_poison.get!(
"https://server.delegs.de/delegseditor/signwritingeditor/signitems?word=#{
URI.encode(entry.text)
}"
Expand All @@ -14,7 +16,8 @@ defmodule SignDict.Services.FetchDelegesDataForEntry do
if result.status_code == 200 do
entry
|> parse_data(result.body)
|> fetch_images()
|> delete_outdated_items(entry)
|> fetch_images(http_poison)

entry
|> Entry.deleges_updated_at_changeset(%{deleges_updated_at: DateTime.utc_now()})
Expand Down Expand Up @@ -58,17 +61,17 @@ defmodule SignDict.Services.FetchDelegesDataForEntry do
|> SignDict.Repo.preload(:entry)
end

defp fetch_images(sign_writings) do
defp fetch_images(sign_writings, http_poison) do
sign_writings
|> Enum.filter(fn writing -> writing.state == "active" end)
|> Enum.sort_by(& &1.deleges_id)
|> Enum.take(5)
|> Enum.each(fn writing -> fetch_image(writing) end)
|> Enum.each(fn writing -> fetch_image(writing, http_poison) end)
end

defp fetch_image(sign_writing) do
defp fetch_image(sign_writing, http_poison) do
result =
HTTPoison.get!(
http_poison.get!(
"https://server.delegs.de/delegseditor/signwritingeditor/signimages?upperId=#{
sign_writing.deleges_id
}&lowerId=#{URI.encode(sign_writing.word)}&signlocale=DGS",
Expand All @@ -92,6 +95,17 @@ defmodule SignDict.Services.FetchDelegesDataForEntry do
end
end

defp delete_outdated_items(sign_writings, entry) do
deleges_ids = Enum.map(sign_writings, & &1.deleges_id)
entry_id = entry.id

SignWriting
|> where([p], p.entry_id == ^entry_id and p.deleges_id not in ^deleges_ids)
|> Repo.delete_all()

sign_writings
end

defp last_modified_of(%SignWriting{image: nil}) do
nil
end
Expand Down
138 changes: 134 additions & 4 deletions test/sign_dict/services/fetch_deleges_data_for_entry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,155 @@ defmodule SignDict.Services.FetchDelegesDataForEntryTest do
use SignDict.ModelCase, async: true
import SignDict.Factory
alias SignDict.Entry
alias SignDict.SignWriting
alias SignDict.Services.FetchDelegesDataForEntry

defmodule MockPoison do
def get!("https://server.delegs.de/delegseditor/signwritingeditor/signitems?word=Hallo") do
%{
status_code: 200,
body:
"([{\"id\":\"388\",\"word\":\"Hallo\",\"width\":\"78\"},{\"id\":\"921\",\"word\":\"hallo\",\"width\":\"77\"}])"
}
end

def get!("https://server.delegs.de/delegseditor/signwritingeditor/signitems?word=NotActive") do
%{
status_code: 200,
body:
"([{\"id\":\"388\",\"word\":\"Hallo\",\"width\":\"78\"},{\"id\":\"331\",\"word\":\"NotActive\",\"width\":\"77\"}])"
}
end

def get!("https://server.delegs.de/delegseditor/signwritingeditor/signitems?word=NotModified") do
%{
status_code: 200,
body: "([{\"id\":\"999\",\"word\":\"NotModified\",\"width\":\"78\"}])"
}
end

def get!(
"https://server.delegs.de/delegseditor/signwritingeditor/signimages?upperId=388&lowerId=Hallo&signlocale=DGS",
_opts
) do
%{
status_code: 200,
body: "IMAGE"
}
end

def get!(
"https://server.delegs.de/delegseditor/signwritingeditor/signimages?upperId=921&lowerId=hallo&signlocale=DGS",
_opts
) do
%{
status_code: 200,
body: "IMAGE"
}
end

def get!(
"https://server.delegs.de/delegseditor/signwritingeditor/signimages?upperId=999&lowerId=NotModified&signlocale=DGS",
_opts
) do
%{
status_code: 302
}
end
end

describe "fetch_for/1" do
test "it will load all datasets into the database and updates the images" do
entry = insert(:entry, text: "Hallo")
FetchDelegesDataForEntry.fetch_for(entry)
FetchDelegesDataForEntry.fetch_for(entry, MockPoison)

# TODO: test result
entry = Repo.get(Entry, entry.id) |> Repo.preload(:sign_writings)

assert entry.deleges_updated_at != nil
assert Enum.count(entry.sign_writings) == 2

[first, second] = Enum.sort_by(entry.sign_writings, & &1.deleges_id)

assert first.word == "Hallo"
assert first.deleges_id == 388
assert first.state == "active"
assert second.word == "hallo"
assert second.deleges_id == 921
assert second.state == "active"
end

test "does not update image if image is not modified" do
# todo: implement
entry = insert(:entry, text: "NotModified")
FetchDelegesDataForEntry.fetch_for(entry, MockPoison)

entry = Repo.get(Entry, entry.id) |> Repo.preload(:sign_writings)

assert entry.deleges_updated_at != nil
assert Enum.count(entry.sign_writings) == 1

first = List.first(entry.sign_writings)

assert first.word == "NotModified"
assert first.deleges_id == 999
assert first.image == nil
assert first.state == "active"
end

test "only update images that are active" do
entry = insert(:entry, text: "NotActive")

%SignWriting{}
|> SignWriting.changeset(%{
deleges_id: 331,
width: 100,
word: "NotActive",
entry_id: entry.id,
state: "wrong"
})
|> Repo.insert!()

FetchDelegesDataForEntry.fetch_for(entry, MockPoison)

entry = Repo.get(Entry, entry.id) |> Repo.preload(:sign_writings)

assert entry.deleges_updated_at != nil
assert Enum.count(entry.sign_writings) == 2

[first, second] = Enum.sort_by(entry.sign_writings, & &1.deleges_id)

assert first.word == "NotActive"
assert first.deleges_id == 331
assert first.state == "wrong"
assert first.image == nil
assert second.word == "Hallo"
assert second.deleges_id == 388
assert second.state == "active"
assert second.image != nil
end

test "delete items that are no longer returend by the deleges api" do
# todo: implement
entry = insert(:entry, text: "Hallo")

%SignWriting{}
|> SignWriting.changeset(%{
deleges_id: 0,
width: 100,
word: "Hallo",
entry_id: entry.id
})
|> Repo.insert!()

FetchDelegesDataForEntry.fetch_for(entry, MockPoison)

entry = Repo.get(Entry, entry.id) |> Repo.preload(:sign_writings)

assert entry.deleges_updated_at != nil
assert Enum.count(entry.sign_writings) == 2

[first, second] = Enum.sort_by(entry.sign_writings, & &1.deleges_id)

assert first.deleges_id == 388
assert second.deleges_id == 921
end
end
end

0 comments on commit 2046443

Please sign in to comment.