Add lib/Pingback.pm and tests

This commit is contained in:
Brian Fraser fraserb@gmail.com
2012-08-07 18:08:51 -03:00
parent d77a3a19f6
commit 58647e8b42
2 changed files with 125 additions and 0 deletions

49
lib/Pingback.pm Normal file
View File

@@ -0,0 +1,49 @@
package Pingback;
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use constant PTDEBUG => $ENV{PTDEBUG} || 0;
local $EVAL_ERROR;
eval {
require HTTP::Tiny;
require Transformers;
};
sub pingback {
my ($url, $ua) = @_;
$ua ||= HTTP::Tiny->new( verify_ssl => 1 );
my $response = $ua->get($url);
if ( $response->{status} >= 500
|| (exists $response->{reason} && !exists $response->{content}) )
{
return;
}
my $checks = $response->{content}
? eval($response->{content})
: _default_checks();
my $e = $EVAL_ERROR;
$checks ||= _default_checks();
$checks->{check_code_error} = $e if $EVAL_ERROR;
my $options = {
headers => { 'content-type' => 'application/json', },
content => Transformers::encode_json($checks),
};
return $ua->post($url, $options);
}
sub _default_checks {
return +{
perl_version => $],
DBD_mysql_version => $DBD::mysql::VERSION || 'N/A',
operating_system => $^O eq "MSWin32" ? Win32::GetOSName() : $^O,
};
}
1;