2021-05-01 23:26:48 +02:00
|
|
|
require "json"
|
|
|
|
|
|
|
|
class Articles::Show < BrowserAction
|
2021-10-03 22:45:20 +02:00
|
|
|
fallback do
|
2021-10-11 17:56:28 +02:00
|
|
|
post_id = maybe_post_id(context.request)
|
|
|
|
case post_id
|
2021-10-03 22:45:20 +02:00
|
|
|
in Monads::Just
|
2021-10-11 17:56:28 +02:00
|
|
|
response = client_class.post_data(post_id.value!)
|
2021-10-03 22:45:20 +02:00
|
|
|
page = PageConverter.new.convert(response.data)
|
|
|
|
html ShowPage, page: page
|
|
|
|
in Monads::Nothing, Monads::Maybe
|
|
|
|
html(
|
2022-02-12 23:56:36 +01:00
|
|
|
Errors::ParseErrorPage,
|
2021-09-05 03:31:48 +02:00
|
|
|
message: "Error parsing the URL",
|
2021-12-12 18:01:55 +01:00
|
|
|
status_code: 500,
|
2022-02-12 23:56:36 +01:00
|
|
|
original_path: request.path,
|
2021-09-05 03:31:48 +02:00
|
|
|
)
|
|
|
|
end
|
2021-10-03 22:45:20 +02:00
|
|
|
end
|
|
|
|
|
2021-10-11 17:56:28 +02:00
|
|
|
def maybe_post_id(request : HTTP::Request)
|
|
|
|
from_params = post_id_from_params(request.query_params)
|
|
|
|
from_path = post_id_from_path(request.path)
|
|
|
|
from_path.or(from_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_id_from_path(request_path : String)
|
2021-10-03 22:45:20 +02:00
|
|
|
Monads::Try(Regex::MatchData)
|
|
|
|
.new(->{ request_path.match(/([0-9a-f]+)$/i) })
|
|
|
|
.to_maybe
|
|
|
|
.fmap(->(matches : Regex::MatchData) { matches[1] })
|
|
|
|
end
|
|
|
|
|
2021-10-11 17:56:28 +02:00
|
|
|
def post_id_from_params(params : URI::Params)
|
|
|
|
maybe_uri = Monads::Try(String)
|
|
|
|
.new(->{ params["redirectUrl"] })
|
|
|
|
.to_maybe
|
|
|
|
.fmap(->(url : String) { URI.parse(url) })
|
|
|
|
.bind(->(uri : URI) { post_id_from_path(uri.path) })
|
|
|
|
end
|
|
|
|
|
2021-10-03 22:45:20 +02:00
|
|
|
def client_class
|
2021-12-12 18:01:55 +01:00
|
|
|
if use_local?
|
2021-10-03 22:45:20 +02:00
|
|
|
LocalClient
|
2021-05-01 23:26:48 +02:00
|
|
|
else
|
2021-10-03 22:45:20 +02:00
|
|
|
MediumClient
|
2021-05-01 23:26:48 +02:00
|
|
|
end
|
|
|
|
end
|
2021-12-12 18:01:55 +01:00
|
|
|
|
|
|
|
def use_local?
|
|
|
|
ENV.fetch("USE_LOCAL", "false") == "true"
|
|
|
|
end
|
2021-05-01 23:26:48 +02:00
|
|
|
end
|