From ccdcc301484aa5a278733efff9e0cf4c25026bf4 Mon Sep 17 00:00:00 2001 From: Alessandro Ghedini Date: Thu, 2 Aug 2012 21:06:09 +0200 Subject: [PATCH] add IsValid::XML goodie plugin --- lib/DDG/Goodie/IsValid/XML.pm | 24 ++++++++++++++++++++++++ t/IsValid.t | 10 ++++++++++ 2 files changed, 34 insertions(+) create mode 100644 lib/DDG/Goodie/IsValid/XML.pm diff --git a/lib/DDG/Goodie/IsValid/XML.pm b/lib/DDG/Goodie/IsValid/XML.pm new file mode 100644 index 000000000..6e778e41d --- /dev/null +++ b/lib/DDG/Goodie/IsValid/XML.pm @@ -0,0 +1,24 @@ +package DDG::Goodie::IsValid::XML; + +use DDG::Goodie; + +use Try::Tiny; +use XML::Simple; + +zci answer_type => 'isvalid'; +zci is_cached => 1; + +triggers start => 'is valid xml', 'validate xml'; + +handle remainder => sub { + my $result = try { + XMLin($_); + return 'valid'; + } catch { + return 'invalid'; + }; + + return "Your XML is $result!"; +}; + +1; diff --git a/t/IsValid.t b/t/IsValid.t index db4ca5a5e..d4da2e2dc 100755 --- a/t/IsValid.t +++ b/t/IsValid.t @@ -19,4 +19,14 @@ ddg_goodie_test( 'is valid json {"test" "lol"}' => test_zci('Your JSON is invalid!'), ); +ddg_goodie_test( + [qw(DDG::Goodie::IsValid::XML)], + 'is valid xml ' => test_zci('Your XML is valid!'), +); + +ddg_goodie_test( + [qw(DDG::Goodie::IsValid::XML)], + 'is valid xml lol' => test_zci('Your XML is invalid!'), +); + done_testing;