Compare commits
2 Commits
b59ac53e4b
...
a066b73f2d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a066b73f2d | ||
|
|
5e05567309 |
@@ -95,7 +95,7 @@ module Picopackage
|
|||||||
file = argv.first
|
file = argv.first
|
||||||
source_file = SourceFile.from_file(file)
|
source_file = SourceFile.from_file(file)
|
||||||
begin
|
begin
|
||||||
Fetch.fetch(source_file.url, path, force: options[:force])
|
Fetch.fetch(source_file.url, File.dirname(file), force: options[:force])
|
||||||
rescue LocalModificationError => e
|
rescue LocalModificationError => e
|
||||||
puts "Error: #{e.message}"
|
puts "Error: #{e.message}"
|
||||||
rescue => e
|
rescue => e
|
||||||
|
|||||||
@@ -39,11 +39,9 @@ module Picopackage
|
|||||||
@content = nil
|
@content = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def transform_url(url) = url
|
|
||||||
|
|
||||||
def body = @body ||= fetch
|
def body = @body ||= fetch
|
||||||
|
|
||||||
def json_body = @json_body ||= JSON.parse(body)
|
def json_body = @json_body ||= JSON.parse(body)
|
||||||
|
def transform_url(url) = url
|
||||||
|
|
||||||
def fetch
|
def fetch
|
||||||
begin
|
begin
|
||||||
@@ -67,52 +65,43 @@ module Picopackage
|
|||||||
|
|
||||||
def handles_body?
|
def handles_body?
|
||||||
true
|
true
|
||||||
rescue FileTooLargeError
|
rescue FileTooLargeError, Net::HTTPError, RuntimeError => e
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
def content
|
# Implement in subclass - this come from the `body`.
|
||||||
# Implement in subclass - this come from the `body`.
|
# Spliting content into code and metadata is the job of the SourceFile class
|
||||||
# Spliting content into code and metadata is the job of the SourceFile class
|
def content = body
|
||||||
|
|
||||||
raise NotImplementedError
|
# Implement in subclass - this should return the filename extracted from the body - if it exists, but not from the metadata
|
||||||
end
|
def filename = File.basename @url
|
||||||
|
|
||||||
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 source_file
|
def source_file
|
||||||
@source_file ||= SourceFile.from_content(
|
@source_file ||= SourceFile.from_content(content, metadata: {'filename' => filename, 'url' => url, 'version' => '0.0.1'})
|
||||||
content, metadata: {'filename' => filename, 'url' => url, 'version' => '0.0.1'}
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class GithubGistProvider < DefaultProvider
|
class GithubGistProvider < DefaultProvider
|
||||||
def self.handles_url?(url) = url.match?(%r{gist\.github\.com})
|
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)
|
def transform_url(url)
|
||||||
gist_id = url[/gist\.github\.com\/[^\/]+\/([a-f0-9]+)/, 1]
|
gist_id = url[/gist\.github\.com\/[^\/]+\/([a-f0-9]+)/, 1]
|
||||||
"https://api.github.com/gists/#{gist_id}"
|
"https://api.github.com/gists/#{gist_id}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def content = json_body["files"].values.first["content"]
|
|
||||||
|
|
||||||
def filename = json_body["files"].values.first["filename"]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class OpenGistProvider < DefaultProvider
|
class OpenGistProvider < DefaultProvider
|
||||||
def handles_url?(url)
|
def handles_url?(url) = :maybe
|
||||||
:maybe
|
|
||||||
end
|
|
||||||
|
|
||||||
def transform_url(url) = "#{url}.json"
|
def transform_url(url) = "#{url}.json"
|
||||||
|
|
||||||
def content = json_body.dig("files",0, "content")
|
def content = json_body.dig("files",0, "content")
|
||||||
|
|
||||||
def filename = json_body.dig("files",0, "filename")
|
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
|
end
|
||||||
|
|
||||||
PROVIDERS = [
|
PROVIDERS = [
|
||||||
|
|||||||
Reference in New Issue
Block a user