From a066b73f2d1432309714d7e71a520d8930e46337 Mon Sep 17 00:00:00 2001 From: Dan Milne Date: Tue, 21 Jan 2025 14:22:21 +1100 Subject: [PATCH] Make the default provider just assume the URL is a direct file link --- lib/picopackage/provider.rb | 45 ++++++++++++++----------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/lib/picopackage/provider.rb b/lib/picopackage/provider.rb index cb945f3..30cae51 100644 --- a/lib/picopackage/provider.rb +++ b/lib/picopackage/provider.rb @@ -39,11 +39,9 @@ module Picopackage @content = nil end - def transform_url(url) = url - def body = @body ||= fetch - def json_body = @json_body ||= JSON.parse(body) + def transform_url(url) = url def fetch begin @@ -67,52 +65,43 @@ module Picopackage def handles_body? true - rescue FileTooLargeError + rescue FileTooLargeError, Net::HTTPError, RuntimeError => e false end - def content - # Implement in subclass - this come from the `body`. - # Spliting content into code and metadata is the job of the SourceFile class + # Implement in subclass - this come from the `body`. + # Spliting content into code and metadata is the job of the SourceFile class + def content = body - raise NotImplementedError - end - - def filename - # Implement in subclass - this should return the filename extracted from the body - if it exists, but not from the metadata - raise NotImplementedError - end + # Implement in subclass - this should return the filename extracted from the body - if it exists, but not from the metadata + def filename = File.basename @url def source_file - @source_file ||= SourceFile.from_content( - content, metadata: {'filename' => filename, 'url' => url, 'version' => '0.0.1'} - ) + @source_file ||= SourceFile.from_content(content, metadata: {'filename' => filename, 'url' => url, 'version' => '0.0.1'}) end end class GithubGistProvider < DefaultProvider def self.handles_url?(url) = url.match?(%r{gist\.github\.com}) - + def content = json_body["files"].values.first["content"] + def filename = json_body["files"].values.first["filename"] def transform_url(url) gist_id = url[/gist\.github\.com\/[^\/]+\/([a-f0-9]+)/, 1] "https://api.github.com/gists/#{gist_id}" end - - def content = json_body["files"].values.first["content"] - - def filename = json_body["files"].values.first["filename"] end class OpenGistProvider < DefaultProvider - def handles_url?(url) - :maybe - end - + def handles_url?(url) = :maybe def transform_url(url) = "#{url}.json" - def content = json_body.dig("files",0, "content") - def filename = json_body.dig("files",0, "filename") + def handles_body? + content && filename + rescue FileTooLargeError, Net::HTTPError, RuntimeError => e + false + end + # If we successfully fetch the body, and the body contains content and a filename, then we can handle the body end PROVIDERS = [