Handle gists with file extensions

Somehow, in my Gist Proxy code 7518a035b1 I never accounted for gist
ids with file extensions. For example: `def123.js` instead of plain
`def123`. This is now fixed and articles with those kinds of gists in
them work now.

Reference article:
https://medium.com/neat-tips-tricks/ocaml-continuation-explained-3b73839
b679f
This commit is contained in:
Edward Loveall 2022-04-04 20:32:42 -04:00
parent 89e5c7209f
commit defec9319e
No known key found for this signature in database
GPG Key ID: A7606DFEC2BA731F
3 changed files with 18 additions and 13 deletions

View File

@ -10,6 +10,16 @@ describe GistParams do
params.filename.should eq("example.txt") params.filename.should eq("example.txt")
end end
describe "when gist file has a file extension" do
it "extracts params from the gist url" do
url = "https://gist.github.com/user/1d.js"
params = GistParams.extract_from_url(url)
params.id.should eq("1d")
end
end
describe "when no file param exists" do describe "when no file param exists" do
it "does not extract a filename" do it "does not extract a filename" do
url = "https://gist.github.com/user/1D" url = "https://gist.github.com/user/1D"

View File

@ -9,20 +9,15 @@ class GistParams
def self.extract_from_url(href : String) def self.extract_from_url(href : String)
uri = URI.parse(href) uri = URI.parse(href)
maybe_id = Monads::Try(Regex::MatchData) maybe_id = Path.posix(uri.path).stem
.new(->{ uri.path.match(GIST_ID_REGEX) })
.to_maybe if maybe_id.matches?(GIST_ID_REGEX)
.fmap(->(matches : Regex::MatchData) { matches[0] }) id = maybe_id
case maybe_id filename = uri.query_params["file"]?
in Monads::Just new(id: id, filename: filename)
id = maybe_id.value! else
in Monads::Nothing, Monads::Maybe
raise MissingGistId.new(href) raise MissingGistId.new(href)
end end
filename = uri.query_params["file"]?
new(id: id, filename: filename)
end end
def initialize(@id : String, @filename : String?) def initialize(@id : String, @filename : String?)

View File

@ -1,3 +1,3 @@
module Scribe module Scribe
VERSION = "2022-03-12" VERSION = "2022-04-04"
end end