diff --git a/lib/DDG/Goodie/PublicDNS.pm b/lib/DDG/Goodie/PublicDNS.pm index 41167d797..d1ea9713e 100644 --- a/lib/DDG/Goodie/PublicDNS.pm +++ b/lib/DDG/Goodie/PublicDNS.pm @@ -2,6 +2,8 @@ package DDG::Goodie::PublicDNS; use DDG::Goodie; +use List::Util qw(max); + primary_example_queries 'public dns'; description 'list common public DNS servers and their IP addresses'; name 'Public DNS'; @@ -13,14 +15,124 @@ attribution github => ['https://github.com/warthurton', 'warthurton']; triggers end => "public dns", "dns servers"; -zci is_cached => 1; +zci is_cached => 1; zci answer_type => "public_dns"; -my $text = share('publicdns.txt')->slurp; -my $html = share('publicdns.html')->slurp; +# Let them add new entries anywhere, but store them lexically. +# Done like this to allow for manual (or other) sorting should the +# need arise. +my @ordered_servers = sort { $a->{provider} cmp $b->{provider} } ({ + provider => 'Comodo Secure DNS', + ip4 => ['8.26.56.26', '8.20.247.20'], + ip6 => [], + info_url => 'http://www.comodo.com/secure-dns/', + }, + { + provider => 'DNS Advantage', + ip4 => ['156.154.70.1', '156.154.71.1'], + ip6 => [], + info_url => 'http://dnsadvantage.com', + }, + { + provider => 'Google Public DNS', + ip4 => ['8.8.8.8', '8.8.4.4'], + ip6 => ['2001:4860:4860::8888', '2001:4860:4860::8844'], + info_url => 'http://code.google.com/speed/public-dns/', + }, + { + provider => 'Norton DNS', + ip4 => ['198.153.192.1', '198.153.194.1'], + ip6 => [], + info_url => 'http://dns.norton.com', + }, + { + provider => 'OpenDNS', + ip4 => ['208.67.222.222', '208.67.220.220'], + ip6 => [], + info_url => 'http://opendns.com/', + }, +); + +# Today we could just change and use the key names, but I have no idea what the future holds. +my @display_cols = ({ + key => 'provider', + title => 'Provider', + }, + { + key => 'ip4', + title => 'IPv4', + }, + { + key => 'ip6', + title => 'IPv6', + }, +); + +my $layout = { + extra_space => 1, # How many extra spaces in the text table? + text_corner => '+', + text_line => '-', + text_col => '|', + text_array => ', ', + html_array => '
', +}; + +foreach my $column (@display_cols) { + $column->{width} = max(map { _max_str_len($_->{$column->{key}}) } @ordered_servers); + $column->{text_spacer} = sub { + my $to_show = shift; + # Everything is left-aligned + return + $layout->{text_col} + . (' ' x $layout->{extra_space}) + . $to_show + . (' ' x max(0, $column->{width} - length($to_show) + $layout->{extra_space})); + }; +} + +my $table_spacer = + join($layout->{text_corner}, '', (map { $layout->{text_line} x ($_->{width} + $layout->{extra_space} * 2) } @display_cols), '') . "\n"; + +# Actually build the output.. finally! +my $text = $table_spacer; +my $css = share("style.css")->slurp; +my $html = ''; + +# First the headers +$text .= join('', map { $_->{text_spacer}->($_->{title}) } @display_cols); +$html .= ''; +$text .= "|\n" . $table_spacer; + +# And now the content +foreach my $server (@ordered_servers) { + $html .= ""; + foreach my $column (@display_cols) { + if ($column->{key} ne 'provider') { + # Assuming we aren't displaying any non-provider string types! + $text .= $column->{text_spacer}->(join($layout->{text_array}, @{$server->{$column->{key}}})); + $html .= ''; + } else { + # Special-case provider + $text .= $column->{text_spacer}->($server->{$column->{key}}); + $html .= ''; + } + } + $text .= $layout->{text_col} . "\n"; + $html .= ''; +} + +$text .= $table_spacer; +$html .= '
' . join('', map { $_->{title} } @display_cols) . '
' . (join($layout->{html_array}, @{$server->{$column->{key}}})) . '' . $server->{$column->{key}} . '
'; handle sub { $text, html => $html; }; +sub _max_str_len { + my $item = shift; + + # We only have strings or arrays, so the max length of a string is the length of the string itself. + return (ref $item eq 'ARRAY') ? length(join($layout->{text_array}, @$item)) : length $item; +} + 1; diff --git a/share/goodie/public_dns/publicdns.html b/share/goodie/public_dns/publicdns.html deleted file mode 100644 index b31af022a..000000000 --- a/share/goodie/public_dns/publicdns.html +++ /dev/null @@ -1,25 +0,0 @@ -Comodo Secure DNS: -8.26.56.26, -8.20.247.20 -[info] - -
DNS Advantage: -156.154.70.1, -156.154.71.1 -[info] - -
Google Public DNS: -8.8.8.8, -8.8.4.4, -2001:4860:4860::8888, -2001:4860:4860::8844 -[info] - -
Norton DNS: -198.153.192.1, -198.153.194.1 -[info] - -
OpenDNS: -208.67.222.222, 208.67.220.220 -[info] diff --git a/share/goodie/public_dns/publicdns.txt b/share/goodie/public_dns/publicdns.txt deleted file mode 100644 index 94d9fb2c5..000000000 --- a/share/goodie/public_dns/publicdns.txt +++ /dev/null @@ -1,5 +0,0 @@ -Comodo Secure DNS: 8.26.56.26, 8.20.247.20 -DNS Advantage: 156.154.70.1, 156.154.71.1 -Google Public DNS: 8.8.8.8, 8.8.4.4, 2001:4860:4860::8888, 2001:4860:4860::8844 -Norton DNS: 198.153.192.1, 198.153.194.1 -OpenDNS: 208.67.222.222, 208.67.220.220 diff --git a/share/goodie/public_dns/style.css b/share/goodie/public_dns/style.css new file mode 100644 index 000000000..f8cf1f40c --- /dev/null +++ b/share/goodie/public_dns/style.css @@ -0,0 +1,11 @@ +#zero_click_abstract table.publicdns td { + white-space: nowrap; + padding: 0.5ex; + border: 1px solid #ddd; +} + +#zero_click_abstract table.publicdns th { + font-weight: bold; + text-align: center; + border: 1px solid #ddd; +} diff --git a/t/PublicDNS.t b/t/PublicDNS.t new file mode 100644 index 000000000..25a06239b --- /dev/null +++ b/t/PublicDNS.t @@ -0,0 +1,19 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; +use DDG::Test::Goodie; + +zci answer_type => "public_dns"; +zci is_cached => 1; + +# We don't want to test too specifically on the included data, so just confirm +# we got an answer with something approaching the correct form. +# Hopefully, some one has eyeballed the output to make sure its got the right data. +my $text_table = qr#^\+-+.*-+\+#m; +my $html_table = qr#.*
$#m; + +ddg_goodie_test([qw( DDG::Goodie::PublicDNS)], map { ("$_" => test_zci($text_table, html => $html_table,)) } ('public dns', 'dns servers',)); + +done_testing;