Skip to content

Add #pretty_print which prints a nice table of data #37

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

Merged
merged 7 commits into from
Feb 16, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
66 changes: 37 additions & 29 deletions lib/github-pages-health-check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
require_relative "github-pages-health-check/errors/invalid_cname"
require_relative "github-pages-health-check/errors/invalid_dns"
require_relative "github-pages-health-check/errors/not_served_by_pages"
require_relative "github-pages-health-check/printer"

class GitHubPages
class HealthCheck
Expand Down Expand Up @@ -153,28 +154,6 @@ def github_domain?
!!domain.match(/\.github\.com$/)
end

def to_hash
{
:uri => uri.to_s,
:dns_resolves? => dns?,
:proxied? => proxied?,
:cloudflare_ip? => cloudflare_ip?,
:old_ip_address? => old_ip_address?,
:a_record? => a_record?,
:cname_record? => cname_record?,
:valid_domain? => valid_domain?,
:apex_domain? => apex_domain?,
:should_be_a_record? => should_be_a_record?,
:pointed_to_github_user_domain? => pointed_to_github_user_domain?,
:pointed_to_github_pages_ip? => pointed_to_github_pages_ip?,
:pages_domain? => pages_domain?,
:served_by_pages? => served_by_pages?,
:valid? => valid?,
:reason => reason
}
end
alias_method :to_h, :to_hash

def served_by_pages?
return @served_by_pages if defined? @served_by_pages
@served_by_pages = begin
Expand All @@ -193,10 +172,6 @@ def served_by_pages?
end
end

def to_json
to_hash.to_json
end

# Runs all checks, raises an error if invalid
def check!
raise InvalidDNS unless dns?
Expand Down Expand Up @@ -229,14 +204,47 @@ def inspect
"#<GitHubPages::HealthCheck @domain=\"#{domain}\" valid?=#{valid?}>"
end

def to_hash
{
:uri => uri.to_s,
:valid? => valid?,
:dns_resolves? => dns?,
:proxied? => proxied?,
:cloudflare_ip? => cloudflare_ip?,
:old_ip_address? => old_ip_address?,
:a_record? => a_record?,
:cname_record? => cname_record?,
:valid_domain? => valid_domain?,
:apex_domain? => apex_domain?,
:should_be_a_record? => should_be_a_record?,
:pointed_to_github_user_domain? => pointed_to_github_user_domain?,
:pointed_to_github_pages_ip? => pointed_to_github_pages_ip?,
:pages_domain? => pages_domain?,
:served_by_pages? => served_by_pages?,
:reason => reason
}
end
alias_method :to_h, :to_hash

def to_json
to_hash.to_json
end

def to_s
to_hash.inject(Array.new) do |all, pair|
all.push pair.join(": ")
end.join("\n")
printer.simple_string
end

def to_s_pretty
printer.pretty_print
end
alias_method :pretty_print, :to_s_pretty

private

def printer
@printer ||= GitHubPages::HealthCheck::Printer.new(self)
end

# surpress warn-level feedback due to unsupported record types
def without_warnings(&block)
warn_level, $VERBOSE = $VERBOSE, nil
Expand Down
72 changes: 72 additions & 0 deletions lib/github-pages-health-check/printer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
class GitHubPages
class HealthCheck
class Printer
PRETTY_LEFT_WIDTH = 11
PRETTY_JOINER = " | "

attr_reader :health_check

def initialize(health_check)
@health_check = health_check
end

def simple_string
health_check.to_hash.inject(Array.new) do |all, pair|
all.push pair.join(": ")
end.join("\n")
end

def pretty_print
values = health_check.to_hash
output = StringIO.new

# Header
output.puts new_line "Domain", "#{values[:uri]} was health checked"
output.puts ("-" * (PRETTY_LEFT_WIDTH + 1)) + "|" + "-" * 50

output.puts new_line "DNS", "does not resolve" if not values[:dns_resolves?]

# Valid?
output.write new_line "State", "#{values[:valid?] ? "valid" : "invalid"}"
output.puts " - is #{"NOT " if not values[:served_by_pages?]}served by Pages"

# What's wrong?
output.puts new_line "Reason", "#{values[:reason]}" if not values[:valid?]
output.puts new_line nil, "pointed to user domain" if values[:pointed_to_github_user_domain?]
output.puts new_line nil, "pointed to pages IP" if values[:pointed_to_github_pages_ip?]

# DNS Record info
output.write new_line "Record Type", "#{values[:a_record?] ? "A" : values[:cname_record?] ? "CNAME" : "other"}"
output.puts values[:should_be_a_record?] ? ", should be A record" : ", should be CNAME"

ip_problems = []
ip_problems << "not apex domain" if not values[:apex_domain?]
ip_problems << "invalid domain" if not values[:valid_domain?]
ip_problems << "old ip address used" if values[:old_ip_address?]
output.puts new_line "IP Problems", "#{ip_problems.size > 0 ? ip_problems.join(", ") : "none"} "
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this only be printed if :should_be_a_record? is true? Or always print it?


if values[:proxied?]
output.puts new_line "Proxied", "yes, through #{values[:cloudflare_ip?] ? "CloudFlare" : "unknown"}"
end

output.puts new_line "Domain", "*.github.com/io domain" if values[:pages_domain?]

output.string
end

def new_line(left = nil, right = nil)
if left and right
ljust(left) + PRETTY_JOINER + right
elsif left
ljust(left)
elsif right
" " * (PRETTY_LEFT_WIDTH + PRETTY_JOINER.size) + right
end
end

def ljust(line)
line.ljust(PRETTY_LEFT_WIDTH)
end
end
end
end