Skip to content

Commit 90d14ed

Browse files
Add support file
This support file is from the mu-elixir-template and should be replaced by integrating the template in here.
1 parent d6bf5f7 commit 90d14ed

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

lib/support.ex

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
defmodule Support do
2+
# environment variables
3+
def sparql_endpoint() do
4+
System.get_env("MU_SPARQL_ENDPOINT")
5+
end
6+
def application_graph() do
7+
System.get_env("MU_APPLICATION_GRAPH")
8+
end
9+
def sparql_timeout() do
10+
System.get_env("MU_SPARQL_TIMEOUT")
11+
end
12+
def log_level() do
13+
System.get_env("LOG_LEVEL")
14+
end
15+
16+
# uuid helper
17+
def generate_uuid() do
18+
UUID.uuid4()
19+
end
20+
21+
# graph helper
22+
def graph() do
23+
application_graph()
24+
end
25+
26+
# query helper
27+
def _query_get_URL(escaped_query_string) do
28+
if sparql_timeout() > 0 do
29+
"#{sparql_endpoint()}?query=#{escaped_query_string}&timeout=#{sparql_timeout()}"
30+
else
31+
"#{sparql_endpoint()}?query=#{escaped_query_string}"
32+
end
33+
end
34+
35+
def query(query_string) do
36+
query_string
37+
|> URI.encode
38+
|> _query_get_URL
39+
|> HTTPoison.get!([{"Accept", "application/json"}])
40+
|> ( &( &1.body ) ).()
41+
|> Poison.decode!
42+
end
43+
44+
def update(query_string) do
45+
HTTPoison.post!(
46+
sparql_endpoint(),
47+
[
48+
"query=" <>
49+
URI.encode_www_form(query_string) <>
50+
"&format=" <> URI.encode_www_form("application/sparql-results+json")
51+
],
52+
["Content-Type": "application/x-www-form-urlencoded"],
53+
[]
54+
)
55+
|> ( &( &1.body ) ).()
56+
|> Poison.decode!
57+
end
58+
59+
def sparql_escape(value) do
60+
String.replace(value, "\"", "\\\"")
61+
end
62+
end

mix.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ defmodule UsePlugProxy.Mixfile do
4848
{:plug_cowboy, "~> 2.4.1"},
4949
{:dialyxir, "~> 1.0", only: [:dev], runtime: false},
5050
{:credo, "~> 1.5", only: [:dev, :test], runtime: false},
51-
{:poison, "~> 2.0"}
51+
{:poison, "~> 2.0"},
52+
{:uuid, "~> 1.1"},
53+
{:httpoison, "~> 1.5"}
5254
]
5355
end
5456
end

0 commit comments

Comments
 (0)