scribe/src/models/gist_params.cr
Edward Loveall defec9319e
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
2022-04-04 20:32:42 -04:00

25 lines
514 B
Crystal

class GistParams
class MissingGistId < Exception
end
GIST_ID_REGEX = /[a-f\d]+$/i
getter id : String
getter filename : String?
def self.extract_from_url(href : String)
uri = URI.parse(href)
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
end
def initialize(@id : String, @filename : String?)
end
end