Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility method to safely parse YAML files #95

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/jekyll/commands/webmention.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def self.process(_args = [], _options = {})
count = 0
cached_outgoing = Jekyll::WebmentionIO.get_cache_file_path "outgoing"
if File.exist?(cached_outgoing)
outgoing = open(cached_outgoing) { |f| YAML.load(f) }
outgoing = Jekyll::WebmentionIO.load_yaml(cached_outgoing)
outgoing.each do |source, targets|
targets.each do |target, response|
next unless response == false
Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll/generators/queue_webmentions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def upgrade_outgoing_webmention_cache
unless File.exist? old_sent_file
return
end
sent_webmentions = open(old_sent_file) { |f| YAML.load(f) }
outgoing_webmentions = open(old_outgoing_file) { |f| YAML.load(f) }
sent_webmentions = Jekyll::WebmentionIO.load_yaml(old_sent_file)
outgoing_webmentions = Jekyll::WebmentionIO.load_yaml(old_outgoing_file)
merged = {}
outgoing_webmentions.each do |source_url, webmentions|
collection = {}
Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll/tags/_.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def initialize(tag_name, text, tokens)
super
cache_file = Jekyll::WebmentionIO.get_cache_file_path "incoming"
@cached_webmentions = if File.exist? cache_file
open(cache_file) { |f| YAML.load(f) }
Jekyll::WebmentionIO.load_yaml(cache_file)
else
{}
end
Expand Down
19 changes: 12 additions & 7 deletions lib/jekyll/webmention_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ def self.read_cached_webmentions(which)
end

cache_file = get_cache_file_path which
cached_webmentions = open(cache_file) { |f| YAML.load(f) }

cached_webmentions
load_yaml(cache_file)
end

def self.cache_webmentions(which, webmentions)
Expand Down Expand Up @@ -140,8 +138,7 @@ def self.get_response(api_params)

def self.read_lookup_dates()
cache_file = get_cache_file_path "lookups"
lookups = open(cache_file) { |f| YAML.load(f) }
lookups
load_yaml(cache_file)
end

def self.cache_lookup_dates(lookups)
Expand Down Expand Up @@ -286,6 +283,14 @@ def self.dump_yaml(file, data = {})
File.open(file, "wb") { |f| f.puts YAML.dump(data) }
end

# Utility Method
# Safely parse given YAML +file+ path and return data.
#
# Returns empty hash if parsing fails to return data
def self.load_yaml(file)
SafeYAML.load_file(file) || {}
end

private

def self.get_http_response(uri)
Expand Down Expand Up @@ -315,15 +320,15 @@ def self.uri_is_not_ok(uri)
# Never cache webmention.io in here
return if uri.host == "webmention.io"
cache_file = @cache_files["bad_uris"]
bad_uris = open(cache_file) { |f| YAML.load(f) }
bad_uris = load_yaml(cache_file)
bad_uris[uri.host] = Time.now.to_s
dump_yaml(cache_file, bad_uris)
end

def self.uri_ok?(uri)
uri = URI.parse(URI.encode(uri))
now = Time.now.to_s
bad_uris = open(@cache_files["bad_uris"]) { |f| YAML.load(f) }
bad_uris = load_yaml(@cache_files["bad_uris"])
if bad_uris.key? uri.host
last_checked = DateTime.parse(bad_uris[uri.host])
cache_bad_uris_for = @config["cache_bad_uris_for"] || 1 # in days
Expand Down