lib/how_is.rb in how_is-18.0.3 vs lib/how_is.rb in how_is-18.0.4
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
require 'how_is/version'
require 'contracts'
require 'cacert'
Cacert.set_in_env
@@ -52,11 +54,11 @@
#
# @param json [String] A JSON report object.
# @return [HowIs] A HowIs object that can be used for generating other
# reports, treating the JSON report as a cache.
def self.from_json(json)
- self.from_hash(JSON.parse(json))
+ from_hash(JSON.parse(json))
end
##
# Given report data as a hash, create a new HowIs object (for generating
# other reports).
@@ -65,21 +67,21 @@
# @return [HowIs] A HowIs object that can be used for generating other
# reports, treating the provided report data as a cache.
def self.from_hash(data)
analysis = HowIs::Analyzer.from_hash(data)
- self.new(analysis.repository, analysis)
+ new(analysis.repository, analysis)
end
##
# Returns a list of possible export formats.
#
# @return [Array<String>] An array of the types of reports you can
# generate.
def self.supported_formats
report_constants = HowIs.constants.grep(/.Report/) - [:BaseReport]
- report_constants.map {|x| x.to_s.split('Report').first.downcase }
+ report_constants.map { |x| x.to_s.split('Report').first.downcase }
end
##
# Returns whether or not the specified +file+ can be exported to.
#
@@ -91,10 +93,12 @@
supported_formats.include?(file.split('.').last)
end
# Generate an analysis.
# TODO: This may make more sense as Analysis.new().
+ # TODO: Nothing overrides +fetcher+ and +analyzer+. Remove ability to do so.
+ # FIXME: THIS CODE AND EVERYTHING ASSOCIATED WITH IT IS A FUCKING ATROCITY.
Contract C::KeywordArgs[repository: String,
fetcher: C::Optional[Class],
analyzer: C::Optional[Class],
github: C::Optional[C::Any]] => C::Any
def self.generate_analysis(repository:,
@@ -117,11 +121,13 @@
def self.generate_frontmatter(frontmatter, report_data)
frontmatter = convert_keys(frontmatter, :to_s)
report_data = convert_keys(report_data, :to_sym)
frontmatter = frontmatter.map { |k, v|
- v = v % report_data
+ # Sometimes report_data has unused keys, which generates a warning, but
+ # we're okay with it.
+ v = silence_warnings { v % report_data }
[k, v]
}.to_h
YAML.dump(frontmatter)
@@ -140,11 +146,10 @@
github: nil,
report_class: nil)
report_class ||= HowIs::Report
date = Date.strptime(Time.now.to_i.to_s, '%s')
- date_string = date.strftime('%Y-%m-%d')
friendly_date = date.strftime('%B %d, %y')
analysis = HowIs.generate_analysis(repository: config['repository'], github: github)
report_data = {
@@ -154,11 +159,13 @@
}
generated_reports = {}
config['reports'].map do |format, report_config|
- filename = report_config['filename'] % report_data
+ # Sometimes report_data has unused keys, which generates a warning, but
+ # we're okay with it.
+ filename = silence_warnings { report_config['filename'] % report_data }
file = File.join(report_config['directory'], filename)
report = report_class.export(analysis, format)
result = build_report(report_config['frontmatter'], report_data, report)
@@ -185,13 +192,26 @@
str.puts report
str.string
end
-private
# convert_keys({'foo' => 'bar'}, :to_sym)
# => {:foo => 'bar'}
def self.convert_keys(data, method_name)
- data.map {|k, v| [k.send(method_name), v]}.to_h
+ data.map { |k, v| [k.send(method_name), v] }.to_h
end
+ private_class_method :convert_keys
+ def self.silence_warnings(&block)
+ with_warnings(nil, &block)
+ end
+ private_class_method :silence_warnings
+
+ def self.with_warnings(flag, &_block)
+ old_verbose = $VERBOSE
+ $VERBOSE = flag
+ yield
+ ensure
+ $VERBOSE = old_verbose
+ end
+ private_class_method :with_warnings
end