diff --git a/spec/models/gist_params_spec.cr b/spec/models/gist_params_spec.cr index df711be..4386e6f 100644 --- a/spec/models/gist_params_spec.cr +++ b/spec/models/gist_params_spec.cr @@ -10,6 +10,16 @@ describe GistParams do params.filename.should eq("example.txt") 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 it "does not extract a filename" do url = "https://gist.github.com/user/1D" diff --git a/src/models/gist_params.cr b/src/models/gist_params.cr index c8d5dfe..0ce047e 100644 --- a/src/models/gist_params.cr +++ b/src/models/gist_params.cr @@ -9,20 +9,15 @@ class GistParams def self.extract_from_url(href : String) uri = URI.parse(href) - maybe_id = Monads::Try(Regex::MatchData) - .new(->{ uri.path.match(GIST_ID_REGEX) }) - .to_maybe - .fmap(->(matches : Regex::MatchData) { matches[0] }) - case maybe_id - in Monads::Just - id = maybe_id.value! - in Monads::Nothing, Monads::Maybe + maybe_id = Path.posix(uri.path).stem + + if maybe_id.matches?(GIST_ID_REGEX) + id = maybe_id + filename = uri.query_params["file"]? + new(id: id, filename: filename) + else raise MissingGistId.new(href) end - - filename = uri.query_params["file"]? - - new(id: id, filename: filename) end def initialize(@id : String, @filename : String?) diff --git a/src/version.cr b/src/version.cr index 4387319..b2a5e6e 100644 --- a/src/version.cr +++ b/src/version.cr @@ -1,3 +1,3 @@ module Scribe - VERSION = "2022-03-12" + VERSION = "2022-04-04" end