Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor affiliate linking. #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions lib/ello_click/affiliate.ex
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
defmodule ElloClick.Affiliate do
alias ElloClick.Affiliate.VigLink
alias ElloClick.Affiliate.{VigLink,Link}
@behaviour Plug

def init(opts), do: opts

def call(conn, _opts) do
{:ok, url} = affiliate(conn)
Plug.Conn.put_resp_header(conn, "location", url)
link = affiliate(conn)
Plug.Conn.put_resp_header(conn, "location", link.affiliated)
end

defp affiliate(conn) do
conn.assigns.url
|> skip_ello
%Link{original: conn.assigns.url}
|> handle_ello
|> VigLink.affiliate(conn)
|> fallback
end

defp skip_ello("http://ello.co" <> _ = ello), do: {:ok, ello}
defp skip_ello("https://ello.co" <> _ = ello), do: {:ok, ello}
defp skip_ello(not_ello), do: not_ello
defp handle_ello(%Link{is_affiliated: true} = link), do: link
defp handle_ello(%Link{original: "http://ello.co" <> _} = link),
do: Link.affiliate(link, link.original)
defp handle_ello(%Link{original: "https://ello.co" <> _} = link),
do: Link.affiliate(link, link.original)
defp handle_ello(%Link{original: "https://ello.ninja" <> _} = link),
do: Link.affiliate(link, link.original)
defp handle_ello(%Link{} = link), do: link

# Ensure we always return {:ok, link}
defp fallback({:ok, affiliated}), do: {:ok, affiliated}
defp fallback(unaffiliated), do: {:ok, unaffiliated}
defp fallback(%Link{is_affiliated: true} = link), do: link
defp fallback(link), do: Link.affiliate(link, link.original)
end
9 changes: 9 additions & 0 deletions lib/ello_click/affiliate/link.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule ElloClick.Affiliate.Link do
defstruct [original: nil, affiliated: nil, is_affiliated: false]
Copy link

@jayzes jayzes Aug 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth expanding out the names of these fields a bit as long as we're trying to be extra-clear (e.g. original_link, affiliated_link)? Not sure if it's idiomatic, just a thought.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind the shorter names. Since they are in a Link struct the "link" prefix is not needed. Not sure what you think @alanpeabody.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend towards brevity in variable names, but happy to update to add them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were actually thinking this could even be taken a little further and have those be %URI{} structs so we could match on hostname without schema easier.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah nice, that'd be cool.


def affiliate(link, url) do
link
|> Map.put(:affiliated, url)
|> Map.put(:is_affiliated, true)
end
end
8 changes: 5 additions & 3 deletions lib/ello_click/affiliate/vig_link.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
defmodule ElloClick.Affiliate.VigLink do
alias Plug.Conn
alias ElloClick.Affiliate.Link

# Don't re-affiliate links something further up the chain got
def affiliate({:ok, affiliated}, _conn), do: {:ok, affiliated}
def affiliate(%Link{is_affiliated: true} = link, _conn), do: link

def affiliate(unaffiliated, conn) do
call_api(config.key, unaffiliated, referer(conn))
def affiliate(link, conn) do
{:ok, affiliated} = call_api(config.key, link.original, referer(conn))
Link.affiliate(link, affiliated)
end

# No API key
Expand Down