| 1 | package Druid; | ||||||||||
| 2 | |||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use JSON::MaybeXS qw(encode_json decode_json); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use HTTP::Request; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Druid::Util qw(iso8601_yyyy_mm_dd_hh_mm_ss); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | our $VERSION = '0.004'; | |||||||||
| 11 | |||||||||||
| 12 | 1 | has 'api_url' => ( | |||||||||
| 13 | 'is' => 'ro', | ||||||||||
| 14 | ); | ||||||||||
| 15 | |||||||||||
| 16 | has 'ua' => ( | ||||||||||
| 17 | 'is' => 'ro', | ||||||||||
| 18 | 0 | ▶ | 'default' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 19 | 0 | my $self = shift; | |||||||||
| 20 | 0 | my $ua = LWP::UserAgent->new(); | |||||||||
| 21 | 0 | $ua->ssl_opts( 'verify_hostname' => 0 ); | |||||||||
| 22 | 0 | return $ua; | |||||||||
| 23 | }, | ||||||||||
| 24 | 1 | ); | |||||||||
| 25 | |||||||||||
| 26 | has 'req' => ( | ||||||||||
| 27 | 'is' => 'ro', | ||||||||||
| 28 | 'lazy' => 1, | ||||||||||
| 29 | 0 | ▶ | 'default' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 30 | 0 | my $self = shift; | |||||||||
| 31 | 0 | my $req = HTTP::Request->new( 'POST' => $self->api_url ); | |||||||||
| 32 | 0 | $req->header( 'Content-Type' => 'application/json' ); | |||||||||
| 33 | 0 | return $req; | |||||||||
| 34 | }, | ||||||||||
| 35 | 1 | ); | |||||||||
| 36 | |||||||||||
| 37 | 0 | ▶ | sub send { | ||||||||
|
Subroutine
send: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||
| 38 | 0 | my $self = shift; | |||||||||
| 39 | 0 | my $query = shift; | |||||||||
| 40 | |||||||||||
| 41 | 0 | $self->{error} = undef; | |||||||||
| 42 | |||||||||||
| 43 | 0 | my $response; | |||||||||
| 44 | 0 | my $request_hash = $query->gen_query; | |||||||||
| 45 | 0 | $self->req->content( encode_json( $request_hash ) ); | |||||||||
| 46 | |||||||||||
| 47 | 0 | my $res = $self->ua->request( $self->req ); | |||||||||
| 48 | 0 | ▶ | if ($res->is_success) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | eval { | ||||||||||
| 50 | 0 | ▶ | $response = decode_json($res->content) if $res->content ne ""; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | $_->{'timestamp'} = iso8601_yyyy_mm_dd_hh_mm_ss( $_->{'timestamp'} ) | ||||||||||
| 52 | 0 | for @{$response}; | |||||||||
| 53 | 0 | 1; | |||||||||
| 54 | 0 | ▶ | } or do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 0 | $self->handle_error("500", "Parsing of the reponse failed"); | |||||||||
| 56 | 0 | warn "error: $@"; | |||||||||
| 57 | } | ||||||||||
| 58 | }else{ | ||||||||||
| 59 | 0 | $self->handle_error($res->code, $res->message, $res->content); | |||||||||
| 60 | } | ||||||||||
| 61 | 0 | return $response; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | sub handle_error { | ||||||||
|
Subroutine
handle_error: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 65 | 0 | my $self = shift; | |||||||||
| 66 | 0 | my ($code, $message, $content) = @_; | |||||||||
| 67 | |||||||||||
| 68 | $self->{error} = { | ||||||||||
| 69 | 0 | "code" => $code, | |||||||||
| 70 | "message" => $message, | ||||||||||
| 71 | "content" => $content | ||||||||||
| 72 | }; | ||||||||||
| 73 | |||||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 1 | 1; | |||||||||
| 77 | |||||||||||
| 78 | __END__ | ||||||||||