IDN: Structured Answer (#4356)

* idn structured answer, remove tabs

* make returned answer more readable
master
Philip Kirkbride 2017-08-23 19:23:50 -04:00 committed by Rob Emery
parent 817a8ee122
commit 3105bc1be5
2 changed files with 38 additions and 21 deletions

View File

@ -18,13 +18,20 @@ handle query_lc => sub {
my $idn = ($1 eq "idn" ? uc $1 : $1) . ": ";
$idn = 'internationalized domain: ' if $1 eq 'internationalize domain';
return unless m/\.(ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|asia|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cu|cv|cw|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|info|int|io|iq|ir|is|it|je|jm|jo|jobs|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|local|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mobi|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sx|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|travel|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|xxx|ye|yt|za|zm|zw)$/i;
if(/^xn--/) {
return 'Decoded ' . $idn . domain_to_unicode($_);
}
else {
return 'Encoded ' . $idn . domain_to_ascii($_);
}
return;
my $string_answer = (/^xn--/ ? 'Decoded ' . $idn . domain_to_unicode($_) : 'Encoded ' . $idn . domain_to_ascii($_));
my $title = (/^xn--/ ? domain_to_unicode($_) : domain_to_ascii($_));
my $subtitle = (/^xn--/ ? $_ . " decoded" : $_ . " encoded");
return $string_answer, structured_answer => {
data => {
title => $title,
subtitle => $subtitle,
},
templates => {
group => 'text'
}
}
};
zci is_cached => 1;

38
t/IDN.t
View File

@ -10,21 +10,31 @@ use utf8;
zci answer_type => 'idn';
zci is_cached => 1;
sub build_answer {
my($string_answer, $title, $subtitle) = @_;
return $string_answer,
structured_answer => {
data => {
title => $title,
subtitle => $subtitle,
},
templates => {
group => 'text',
}
};
}
sub build_test { test_zci(build_answer(@_)) }
ddg_goodie_test(
[qw(
DDG::Goodie::IDN
)],
'idn exämple.com' =>
test_zci('Encoded IDN: xn--exmple-cua.com'),
#########
'internationalize domain exämple.com' =>
test_zci('Encoded internationalized domain: xn--exmple-cua.com'),
'idn xn--exmple-cua.com' =>
test_zci('Decoded IDN: exämple.com'),
'international domain xn--exmple-cua.com' =>
test_zci('Decoded international domain: exämple.com'),
'internationalized domain xn--exmple-cua.com' =>
test_zci('Decoded internationalized domain: exämple.com'),
[qw(
DDG::Goodie::IDN
)],
'idn exämple.com' => build_test('Encoded IDN: xn--exmple-cua.com', 'xn--exmple-cua.com', 'exämple.com encoded'),
'internationalize domain exämple.com' => build_test('Encoded internationalized domain: xn--exmple-cua.com', 'xn--exmple-cua.com', 'exämple.com encoded'),
'idn xn--exmple-cua.com' => build_test('Decoded IDN: exämple.com', 'exämple.com', 'xn--exmple-cua.com decoded'),
'international domain xn--exmple-cua.com' => build_test('Decoded international domain: exämple.com', 'exämple.com', 'xn--exmple-cua.com decoded'),
'internationalized domain xn--exmple-cua.com' => build_test('Decoded internationalized domain: exämple.com', 'exämple.com', 'xn--exmple-cua.com decoded'),
);
done_testing;