Support medium's redirectUrl query param
When a post has a gi= query param, Medium makes a global_identifier "query". This redirects via a 307 temporary redirect to a url that looks like this: https://medium.com/m/global-identity?redirectUrl=https%3A%2F%2Fexample.c om%2Fmy-post-000000000000 Previously, scribe looked for the Medium post id in the url's path, not it's query params since query params can include other garbage like medium_utm (not related to medium.com). Now it looks first for the post id in the path, then looks to the redirectUrl as a fallback.
This commit is contained in:
parent
91687bb689
commit
bb94fb41b1
2 changed files with 24 additions and 4 deletions
|
@ -68,4 +68,10 @@ describe Articles::Show do
|
||||||
|
|
||||||
TestClient.last_post_id.should eq("777777abcdef")
|
TestClient.last_post_id.should eq("777777abcdef")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "parses the post id for global identity redirects" do
|
||||||
|
HttpClient.get("/m/global-identity?redirectUrl=https%3A%2F%2Fexample.com%2Fmy-post-888888abcdef")
|
||||||
|
|
||||||
|
TestClient.last_post_id.should eq("888888abcdef")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,10 +2,10 @@ require "json"
|
||||||
|
|
||||||
class Articles::Show < BrowserAction
|
class Articles::Show < BrowserAction
|
||||||
fallback do
|
fallback do
|
||||||
maybe_post_id = post_id(context.request.path)
|
post_id = maybe_post_id(context.request)
|
||||||
case maybe_post_id
|
case post_id
|
||||||
in Monads::Just
|
in Monads::Just
|
||||||
response = client_class.post_data(maybe_post_id.value!)
|
response = client_class.post_data(post_id.value!)
|
||||||
page = PageConverter.new.convert(response.data)
|
page = PageConverter.new.convert(response.data)
|
||||||
html ShowPage, page: page
|
html ShowPage, page: page
|
||||||
in Monads::Nothing, Monads::Maybe
|
in Monads::Nothing, Monads::Maybe
|
||||||
|
@ -17,13 +17,27 @@ class Articles::Show < BrowserAction
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def post_id(request_path : String)
|
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)
|
||||||
Monads::Try(Regex::MatchData)
|
Monads::Try(Regex::MatchData)
|
||||||
.new(->{ request_path.match(/([0-9a-f]+)$/i) })
|
.new(->{ request_path.match(/([0-9a-f]+)$/i) })
|
||||||
.to_maybe
|
.to_maybe
|
||||||
.fmap(->(matches : Regex::MatchData) { matches[1] })
|
.fmap(->(matches : Regex::MatchData) { matches[1] })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
def client_class
|
def client_class
|
||||||
if Lucky::Env.use_local?
|
if Lucky::Env.use_local?
|
||||||
LocalClient
|
LocalClient
|
||||||
|
|
Loading…
Reference in a new issue