Compare commits

2 Commits

Author SHA1 Message Date
Dan Milne
a066b73f2d Make the default provider just assume the URL is a direct file link 2025-01-21 14:22:21 +11:00
Dan Milne
5e05567309 Bug fix 2025-01-21 13:48:52 +11:00
2 changed files with 18 additions and 29 deletions

View File

@@ -95,7 +95,7 @@ module Picopackage
file = argv.first
source_file = SourceFile.from_file(file)
begin
Fetch.fetch(source_file.url, path, force: options[:force])
Fetch.fetch(source_file.url, File.dirname(file), force: options[:force])
rescue LocalModificationError => e
puts "Error: #{e.message}"
rescue => e

View File

@@ -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 = [