Make the default provider just assume the URL is a direct file link
This commit is contained in:
@@ -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
|
||||
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
|
||||
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 = [
|
||||
|
||||
Reference in New Issue
Block a user