Packages
extwitter
0.9.1
0.14.0
0.13.1
0.13.0
0.12.5
0.12.4
0.12.3
0.12.2
0.12.1
0.12.0
0.11.0
0.10.0
0.9.6
0.9.5
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.3
0.7.2
0.7.1
0.7.0
0.6.3
0.6.2
0.6.1
0.6.0
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.0
0.2.1
0.2.0
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
Twitter client library for Elixir.
Current section
Files
Jump to
Current section
Files
lib/extwitter/proxy.ex
defmodule ExTwitter.Proxy do
@moduledoc """
Provide helper functions for proxy setting.
"""
@doc """
Initialize proxy settings based on defintion in config.exs.
"""
def initialize do
if proxy() != nil do
:httpc.set_options(get_proxy_option(proxy()))
end
end
@doc """
Get proxy settings for sending request based on definition in config.exs.
"""
def options do
if proxy() != nil do
get_auth_option(proxy())
else
[]
end
end
def get_proxy_option(proxy) do
if proxy[:server] != nil && proxy[:port] != nil do
server = String.to_charlist(proxy[:server])
port = parse_port(proxy[:port])
[{:proxy, {{server, port}, []}}]
else
[]
end
end
def get_auth_option(proxy) do
if proxy[:user] != nil && proxy[:password] != nil do
user = String.to_charlist(proxy[:user])
password = String.to_charlist(proxy[:password])
[{:proxy_auth, {user, password}}]
else
[]
end
end
defp proxy do
Application.get_env(:extwitter, :proxy, nil)
end
defp parse_port(port) when is_binary(port) do
case Integer.parse(port) do
{integer, _binary} ->
integer
:error ->
raise ExTwitter.Error,
message: "Failed to parse :port for proxy as integer (port: #{port})"
end
end
defp parse_port(port), do: port
end