Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit8b7fda4

Browse files
author
mydearxym
committed
chore(editor-parser): parse link with open-graph support
1 parentc6c8124 commit8b7fda4

File tree

5 files changed

+166
-26
lines changed

5 files changed

+166
-26
lines changed

‎lib/groupher_server/application.ex‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ defmodule GroupherServer.Application do
44

55
# See https://hexdocs.pm/elixir/Application.html
66
# for more information on OTP Applications
7+
@specstart(any,any)::{:error,any}|{:ok,pid}
78
defstart(_type,_args)do
89
importSupervisor.Spec
910
importCachex.Spec
Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,93 @@
11
defmoduleGroupherServerWeb.Controller.OGdo
2+
@moduledoc"""
3+
handle open-graph info
4+
"""
25
useGroupherServerWeb,:controller
36
# alias Todos.Todo
47
# plug(:action)
58

6-
defindex(conn,_params)do
7-
# todos = Repo.all(Todo)
8-
json(conn,%{hello:"world"})
9-
# render(conn, %{hello: "world"})
10-
# text(conn, "BusiApi!")
9+
defindex(conn,%{"url"=>url})do
10+
fetch_opengraph_info(conn,url)
11+
end
12+
13+
# return editor-js flavor fmt
14+
# see https://github.com/editor-js/link
15+
defpfetch_opengraph_info(conn,url)do
16+
caseOpenGraph.fetch(url)do
17+
{:ok,info}->
18+
ok_response(conn,url,info)
19+
20+
{:error,reason}->
21+
error_response(conn,url,reason)
22+
end
23+
end
24+
25+
defpok_response(conn,url,%OpenGraph{title:nil})do
26+
error_response(conn,url)
27+
end
28+
29+
defpok_response(conn,_url,info)do
30+
json(conn,%{
31+
success:1,
32+
meta:%{
33+
title:info.title,
34+
description:info.description,
35+
image:%{
36+
url:info.image
37+
}
38+
}
39+
})
40+
end
41+
42+
defperror_response(conn,url)do
43+
json(conn,%{
44+
success:1,
45+
meta:%{
46+
title:url,
47+
description:url,
48+
image:%{
49+
url:nil
50+
}
51+
}
52+
})
53+
end
54+
55+
defperror_response(conn,_url,:nxdomain)do
56+
json(conn,%{
57+
success:0,
58+
meta:%{
59+
title:"domain-not-exsit",
60+
description:"--",
61+
image:%{
62+
url:nil
63+
}
64+
}
65+
})
66+
end
67+
68+
defperror_response(conn,_url,:timeout)do
69+
json(conn,%{
70+
success:0,
71+
meta:%{
72+
title:"timeout",
73+
description:"--",
74+
image:%{
75+
url:nil
76+
}
77+
}
78+
})
79+
end
80+
81+
defperror_response(conn,_url,_reason)do
82+
json(conn,%{
83+
success:0,
84+
meta:%{
85+
title:"unknown-error",
86+
description:nil,
87+
image:%{
88+
url:nil
89+
}
90+
}
91+
})
1192
end
1293
end

‎mix.exs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ defmodule GroupherServer.Mixfile do
2929
[
3030
mod:{GroupherServer.Application,[]},
3131
extra_applications:[
32+
:open_graph,
3233
:corsica,
3334
:ex_unit,
3435
:logger,
@@ -101,7 +102,8 @@ defmodule GroupherServer.Mixfile do
101102
{:rihanna,"1.3.5"},
102103
# cron-like scheduler job
103104
{:quantum,"~> 2.3"},
104-
{:html_sanitize_ex,"~> 1.3"}
105+
{:html_sanitize_ex,"~> 1.3"},
106+
{:open_graph,"~> 0.0.3"}
105107
]
106108
end
107109

‎mix.lock‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"guardian":{:hex,:guardian,"2.0.0","5d3e537832b7cf35c8674da92457b7be671666a2eff4bf0f2ccfcfb3a8c67a0b",[:mix],[{:jose,"~> 1.8",[hex::jose,repo:"hexpm",optional:false]},{:plug,"~> 1.3.3 or ~> 1.4",[hex::plug,repo:"hexpm",optional:true]}],"hexpm"},
3939
"hackney":{:hex,:hackney,"1.15.1","9f8f471c844b8ce395f7b6d8398139e26ddca9ebc171a8b91342ee15a19963f4",[:rebar3],[{:certifi,"2.5.1",[hex::certifi,repo:"hexpm",optional:false]},{:idna,"6.0.0",[hex::idna,repo:"hexpm",optional:false]},{:metrics,"1.0.1",[hex::metrics,repo:"hexpm",optional:false]},{:mimerl,"~>1.1",[hex::mimerl,repo:"hexpm",optional:false]},{:ssl_verify_fun,"1.1.4",[hex::ssl_verify_fun,repo:"hexpm",optional:false]}],"hexpm"},
4040
"html_sanitize_ex":{:hex,:html_sanitize_ex,"1.3.0","f005ad692b717691203f940c686208aa3d8ffd9dd4bb3699240096a51fa9564e",[:mix],[{:mochiweb,"~> 2.15",[hex::mochiweb,repo:"hexpm",optional:false]}],"hexpm"},
41+
"httpoison":{:hex,:httpoison,"1.5.1","0f55b5b673b03c5c327dac7015a67cb571b99b631acc0bc1b0b98dcd6b9f2104",[:mix],[{:hackney,"~> 1.8",[hex::hackney,repo:"hexpm",optional:false]}],"hexpm"},
4142
"idna":{:hex,:idna,"6.0.0","689c46cbcdf3524c44d5f3dde8001f364cd7608a99556d8fbd8239a5798d4c10",[:rebar3],[{:unicode_util_compat,"0.4.1",[hex::unicode_util_compat,repo:"hexpm",optional:false]}],"hexpm"},
4243
"inch_ex":{:hex,:inch_ex,"2.0.0","24268a9284a1751f2ceda569cd978e1fa394c977c45c331bb52a405de544f4de",[:mix],[{:bunt,"~> 0.2",[hex::bunt,repo:"hexpm",optional:false]},{:jason,"~> 1.0",[hex::jason,repo:"hexpm",optional:false]}],"hexpm"},
4344
"jason":{:hex,:jason,"1.1.2","b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7",[:mix],[{:decimal,"~> 1.0",[hex::decimal,repo:"hexpm",optional:true]}],"hexpm"},
@@ -50,6 +51,7 @@
5051
"mix_test_watch":{:hex,:mix_test_watch,"0.9.0","c72132a6071261893518fa08e121e911c9358713f62794a90c95db59042af375",[:mix],[{:file_system,"~> 0.2.1 or ~> 0.3",[hex::file_system,repo:"hexpm",optional:false]}],"hexpm"},
5152
"mochiweb":{:hex,:mochiweb,"2.18.0","eb55f1db3e6e960fac4e6db4e2db9ec3602cc9f30b86cd1481d56545c3145d2e",[],[],"hexpm"},
5253
"nanoid":{:hex,:nanoid,"2.0.1","7ddfe8f3abf1a559c3b673878efbe4feb2c81a657e3f0533aa28be5885257674",[:mix],[],"hexpm"},
54+
"open_graph":{:hex,:open_graph,"0.0.3","6edb31150443058bac6e046a3462e5a1fe9818dbc79f1bee028cfea1e6589e61",[:mix],[{:httpoison,"~> 1.0",[hex::httpoison,repo:"hexpm",optional:false]}],"hexpm"},
5355
"parse_trans":{:hex,:parse_trans,"3.3.0","09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1",[:rebar3],[],"hexpm"},
5456
"phoenix":{:hex,:phoenix,"1.4.9","746d098e10741c334d88143d3c94cab1756435f94387a63441792e66ec0ee974",[:mix],[{:jason,"~> 1.0",[hex::jason,repo:"hexpm",optional:true]},{:phoenix_pubsub,"~> 1.1",[hex::phoenix_pubsub,repo:"hexpm",optional:false]},{:plug,"~> 1.8.1 or ~> 1.9",[hex::plug,repo:"hexpm",optional:false]},{:plug_cowboy,"~> 1.0 or ~> 2.0",[hex::plug_cowboy,repo:"hexpm",optional:true]},{:telemetry,"~> 0.4",[hex::telemetry,repo:"hexpm",optional:false]}],"hexpm"},
5557
"phoenix_ecto":{:hex,:phoenix_ecto,"4.0.0","c43117a136e7399ea04ecaac73f8f23ee0ffe3e07acfcb8062fe5f4c9f0f6531",[:mix],[{:ecto,"~> 3.0",[hex::ecto,repo:"hexpm",optional:false]},{:phoenix_html,"~> 2.9",[hex::phoenix_html,repo:"hexpm",optional:true]},{:plug,"~> 1.0",[hex::plug,repo:"hexpm",optional:false]}],"hexpm"},

‎test/groupher_server_web/controller/og_test.exs‎

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,82 @@ defmodule GroupherServerWeb.Test.Controller.OG do
44
"""
55
useGroupherServer.TestTools
66

7+
@tag:wip
8+
test"should return valid structure when query url is valid"do
9+
conn=build_conn()
10+
11+
res=get(conn,"/api/og-info",%{url:"https://github.com"})
12+
res=json_response(res,200)
13+
14+
assertMap.has_key?(res,"success")
15+
assertMap.has_key?(res,"meta")
16+
17+
meta=res["meta"]
18+
assertMap.has_key?(meta,"description")
19+
assertMap.has_key?(meta,"image")
20+
assertMap.has_key?(meta,"title")
21+
22+
image=get_in(res,["meta","image"])
23+
24+
assertMap.has_key?(image,"url")
25+
end
26+
727
@tag:wip2
8-
test"basicshouldwork"do
28+
test"shouldreturn valid structure & error msg when query domain is not exsit"do
929
conn=build_conn()
1030

11-
res=get(conn,"/api/og-info")
12-
# res = conn(:get, "/api/og-info")
13-
# |> put_private(:plug_skip_csrf_protection, true)
14-
# |> GroupherServerWeb.Endpoint.call([])
15-
16-
IO.inspect(json_response(res,200),label:"res")
17-
# IO.inspect(text_response(res, 200), label: "res")
18-
# conn = get conn, todo_path(conn, :index)
19-
20-
# IO.inspect json_response(conn, 200), label: "lulu"
21-
22-
# assert json_response(conn, 200) == %{
23-
# "todos" => [%{
24-
# "title" => todo.title,
25-
# "description" => todo.description,
26-
# "inserted_at" => Ecto.DateTime.to_iso8601(todo.inserted_at),
27-
# "updated_at" => Ecto.DateTime.to_iso8601(todo.updated_at)
28-
# }]
29-
# }
31+
res=get(conn,"/api/og-info",%{url:"https://jfiel.com"})
32+
res=json_response(res,200)
33+
34+
assertMap.has_key?(res,"success")
35+
assertMap.has_key?(res,"meta")
36+
37+
success=res["success"]
38+
assertsuccess==0
39+
40+
meta=res["meta"]
41+
assertMap.has_key?(meta,"description")
42+
assertMap.has_key?(meta,"image")
43+
assertMap.has_key?(meta,"title")
44+
45+
title=get_in(res,["meta","title"])
46+
description=get_in(res,["meta","description"])
47+
asserttitle=="domain-not-exsit"
48+
assertdescription=="--"
49+
50+
image=get_in(res,["meta","image"])
51+
52+
assertMap.has_key?(image,"url")
53+
end
54+
55+
@tag:wip2
56+
test"return empty valid structure when url not follow open-graph"do
57+
conn=build_conn()
58+
59+
url="https://baidu.com"
60+
res=get(conn,"/api/og-info",%{url:url})
61+
res=json_response(res,200)
62+
63+
# IO.inspect(res, label: "json res")
64+
assertMap.has_key?(res,"success")
65+
assertMap.has_key?(res,"meta")
66+
67+
success=res["success"]
68+
assertsuccess==1
69+
70+
meta=res["meta"]
71+
assertMap.has_key?(meta,"description")
72+
assertMap.has_key?(meta,"image")
73+
assertMap.has_key?(meta,"title")
74+
75+
title=get_in(res,["meta","title"])
76+
asserttitle==url
77+
78+
description=get_in(res,["meta","description"])
79+
assertdescription==url
80+
81+
image=get_in(res,["meta","image"])
82+
assertMap.has_key?(image,"url")
83+
assertimage["url"]==nil
3084
end
3185
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp