lib/statusz.rb in statusz-0.0.2 vs lib/statusz.rb in statusz-0.0.3
- old
+ new
@@ -1,59 +1,51 @@
require "cgi"
require "erb"
require "time"
+require "json"
module Statusz
- ALL_FIELDS = %w(git_directory latest_sha current_branch date username git_user_info commit_search)
-
FIELD_TO_SCRAPING_PROC = {
- "git_directory" => Proc.new { `git rev-parse --show-toplevel`.strip.rpartition("/").last },
- "latest_sha" => Proc.new { `git log --pretty=%H -n 1`.strip },
- "current_branch" => Proc.new do
+ "git directory" => Proc.new { `git rev-parse --show-toplevel`.strip.rpartition("/").last },
+ "latest commit" => Proc.new { `git log --pretty=%H -n 1`.strip },
+ "current branch" => Proc.new do
branch = `git symbolic-ref HEAD 2> /dev/null`.strip.sub(%r{^refs/heads/}, "")
$?.to_i.zero? ? branch : "<no branch>"
end,
"date" => Proc.new { Time.now.strftime("%Y-%m-%d %H:%M:%S %z") },
- "username" => Proc.new { `whoami`.strip },
- "git_user_info" => Proc.new do
+ "current user on deploy host" => Proc.new { `whoami`.strip },
+ "git user info" => Proc.new do
"#{`git config --get user.name`.strip} <#{`git config --get user.email`.strip}>"
end,
- "commit_search" => Proc.new { `git log --pretty=%H`.strip }
+ "all commits" => Proc.new { `git log --pretty=%H`.strip }
}
- FIELD_TO_HEADER_NAME = {
- "git_directory" => "git directory",
- "latest_sha" => "latest commit",
- "current_branch" => "current branch",
- "date" => "date",
- "username" => "current user on deploy host",
- "git_user_info" => "git user info",
- "commit_search" => "all commits"
- }
-
- def self.write_git_metadata(filename = "./statusz.html", options = {})
+ def self.write_file(filename = "./statusz.html", options = {})
options[:format] ||= :html
- raise "Bad format: #{options[:format]}" unless [:html, :text].include? options[:format]
- options[:fields] ||= ALL_FIELDS
- bad_options = options[:fields] - ALL_FIELDS
+ raise "Bad format: #{options[:format]}" unless [:html, :text, :json].include? options[:format]
+ options[:fields] ||= FIELD_TO_SCRAPING_PROC.keys
+ bad_options = options[:fields] - FIELD_TO_SCRAPING_PROC.keys
raise "Bad options: #{bad_options.inspect}" unless bad_options.empty?
+ extra_fields = options[:extra_fields] || {}
+ unless extra_fields.is_a? Hash
+ raise "Extra fields should be a hash, but #{extra_fields.inspect} (#{extra_fields.class}) was given."
+ end
results = {}
- options[:fields].each { |field| results[field] = FIELD_TO_SCRAPING_PROC[field].call }
+ options[:fields].each do |field|
+ results[field] = FIELD_TO_SCRAPING_PROC[field].call
+ end
+ extra_fields.each { |field, value| results[field.to_s] = value.to_s }
case options[:format]
when :text
- sections = options[:fields].map do |field|
- "#{FIELD_TO_HEADER_NAME[field]}:\n#{results[field]}"
- end
- output = sections.join("\n\n")
+ output = results.map { |name, value| "#{name}:\n#{value}" }.join("\n\n")
+ when :json
+ output = results.to_json
when :html
- html_values = options[:fields].reduce({}) do |hash, field|
- if field == "commit_search"
- pair = { FIELD_TO_HEADER_NAME[field] => FIELD_TO_SCRAPING_PROC[field].call.split("\n") }
- else
- pair = { FIELD_TO_HEADER_NAME[field] => CGI.escapeHTML(FIELD_TO_SCRAPING_PROC[field].call) }
- end
+ html_values = results.reduce({}) do |hash, field_and_value|
+ field, value = field_and_value
+ pair = (field == "all commits") ? { field => value.split("\n") } : { field => CGI.escapeHTML(value) }
hash.merge pair
end
output = ERB.new(File.read(File.join(File.dirname(__FILE__), "statusz.erb"))).result(binding)
end