File Coverage

lib/WebService/LogDNA.pm
Criterion Covered Total %
statement 41 43 95.3
branch 3 6 50.0
condition 1 3 33.3
subroutine 10 10 100.0
pod 0 1 0.0
total 55 63 87.3


line stmt bran cond sub pod time code
1 2     2   181832 use strict;
  2         4  
  2         64  
2 2     2   8 use warnings;
  2         4  
  2         76  
3              
4             package WebService::LogDNA;
5              
6             # ABSTRACT: Implements the ingest API call for L
7              
8 2     2   1132 use Moo;
  2         21930  
  2         10  
9 2     2   4160 use LWP::UserAgent;
  2         95142  
  2         76  
10 2     2   18 use URI;
  2         2  
  2         46  
11 2     2   10 use Time::HiRes;
  2         4  
  2         24  
12 2     2   1214 use MIME::Base64 qw/encode_base64/;
  2         1332  
  2         128  
13 2     2   824 use WebService::LogDNA::Body;
  2         6  
  2         74  
14              
15 2     2   1072 use namespace::clean;
  2         19502  
  2         14  
16              
17             has key => ( is => 'ro', required => 1 );
18              
19             has hostname => (
20             is => 'ro',
21             default => sub {
22             require POSIX;
23              
24             return( (POSIX::uname())[1] ); #nodename
25             }
26             );
27              
28             has mac => ( is => 'ro' );
29             has ip => ( is => 'ro' );
30              
31             # Private!
32             has agent => (
33             is => 'ro',
34             default => sub {
35             return LWP::UserAgent->new;
36             }
37             );
38              
39             # Mostly overrideable for testing
40             has url => (
41             is => 'ro',
42             default => sub {
43             URI->new("https://logs.logdna.com/logs/ingest");
44             },
45             coerce => sub {
46             unless( ref $_[0] ) { return URI->new($_[0]) }
47              
48             return $_[0];
49             },
50             );
51              
52             sub ingest {
53 1     1 0 7912 my( $self, $body ) = @_;
54              
55 1 50 33     10 unless( defined $body and ref $body ) {
56 0         0 die 'Ingest requires a $body argument';
57             }
58              
59             # Attempt to upgrade a hashref to a proper object
60 1 50       5 if( ref $body eq 'HASH' ) {
61 1         24 $body = WebService::LogDNA::Body->new( %$body );
62             }
63              
64 1 50       9 unless( UNIVERSAL::isa( $body, 'WebService::LogDNA::Body' ) ) {
65 0         0 die "Ingest requires WebService::LogDNA::Body argument, got: $body";
66             }
67              
68 1         5 my $now = int( Time::HiRes::time() * 1000 );
69 1         23 my $url = $self->url->clone;
70 1         100 $url->query_form(
71             hostname => $self->hostname,
72             mac => $self->mac,
73             ip => $self->ip,
74             now => $now,
75             );
76              
77 1         155 my $headers = HTTP::Headers->new;
78 1         32 $headers->authorization_basic($self->key, "");
79 1         147 $headers->content_type('application/json; charset=UTF-8');
80              
81 1         29 my $content = '{"lines":[' . $body->to_json . ']}';
82              
83 1         26 my $request = HTTP::Request->new( "POST", $url, $headers, $content );
84              
85 1         400 my $resp = $self->agent->request($request);
86             }
87              
88             1;
89              
90             __END__