Merge branch 'is_valid'

master
Dylan Lloyd 2012-08-06 13:37:50 -04:00
commit 227a9ff2ee
4 changed files with 124 additions and 0 deletions

View File

@ -31,6 +31,11 @@ Convert::Color::Library = 0.03
Math::Round = 0.06
Convert::Morse = 0.05
Net::IDN::Encode = 2.003
; IsValid::JSON and IsValid::XML
Try::Tiny = 0
JSON = 0
XML::SAX::Expat = 0
XML::Simple = 0
[Prereqs / TestRequires]
Test::More = 0.98

View File

@ -0,0 +1,37 @@
package DDG::Goodie::IsValid::JSON;
# ABSTRACT: Check whether the submitted data is valid JSON.
use DDG::Goodie;
use Try::Tiny;
use JSON qw(from_json);
attribution github => ['https://github.com/AlexBio', 'AlexBio' ],
web => ['http://ghedini.me', 'Alessandro Ghedini'];
zci answer_type => 'isvalid';
zci is_cached => 1;
triggers any => 'json';
handle remainder => sub {
return unless s/ ?(is )?valid\?? ?//gi;
my ($result, $error) = try {
from_json $_;
return 'valid!';
} catch {
$_ =~ /^(.* at character offset \d+ .*) at/;
return ('invalid: ', $1);
};
my $answer = "Your JSON is $result";
my $answer_html = $answer;
$answer .= $error if $error;
$answer_html .= "<pre style=\"font-size:12px\">$error</pre>" if $error;
return $answer, html => $answer_html
};
1;

View File

@ -0,0 +1,38 @@
package DDG::Goodie::IsValid::XML;
# ABSTRACT: Check whether the submitted data is valid XML.
use DDG::Goodie;
use Try::Tiny;
use XML::Simple;
attribution github => ['https://github.com/AlexBio', 'AlexBio' ],
web => ['http://ghedini.me', 'Alessandro Ghedini'];
zci answer_type => 'isvalid';
zci is_cached => 1;
triggers any => 'xml';
handle remainder => sub {
return unless s/ ?(is )?valid\?? ?//gi;
my ($result, $error) = try {
XMLin $_;
return 'valid!';
} catch {
$_ =~ /^\n(.* at line \d+, column \d+, byte \d+) at/;
return ('invalid: ', $1);
};
my $answer = "Your XML is $result";
my $answer_html = $answer;
$answer .= $error if $error;
$answer_html .= "<pre style=\"font-size:12px\">$error</pre>" if $error;
return $answer, html => $answer_html
};
1;

44
t/IsValid.t Executable file
View File

@ -0,0 +1,44 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;
zci answer_type => 'isvalid';
zci is_cached => 1;
ddg_goodie_test(
[qw(DDG::Goodie::IsValid::JSON)],
'is valid json {"test":"lol"}' => test_zci(
'Your JSON is valid!',
html => 'Your JSON is valid!'
)
);
ddg_goodie_test(
[qw(DDG::Goodie::IsValid::JSON)],
'is valid json {"test" "lol"}' => test_zci(
'Your JSON is invalid: \':\' expected, at character offset 8 (before ""lol"}")',
html => 'Your JSON is invalid: <pre style="font-size:12px">\':\' expected, at character offset 8 (before ""lol"}")</pre>'
)
);
ddg_goodie_test(
[qw(DDG::Goodie::IsValid::XML)],
'is valid xml <test></test>' => test_zci(
'Your XML is valid!',
html => 'Your XML is valid!'
)
);
ddg_goodie_test(
[qw(DDG::Goodie::IsValid::XML)],
'is valid xml <test>lol' => test_zci(
'Your XML is invalid: no element found at line 1, column 9, byte 9',
html => 'Your XML is invalid: <pre style="font-size:12px">no element found at line 1, column 9, byte 9</pre>'
)
);
done_testing;