File Coverage

blib/lib/App/Kritika.pm
Criterion Covered Total %
statement 45 45 100.0
branch 10 16 62.5
condition 2 5 40.0
subroutine 7 7 100.0
pod 0 2 0.0
total 64 75 85.3


line stmt bran cond sub pod time code
1             package App::Kritika;
2              
3 1     1   689 use strict;
  1         3  
  1         36  
4 1     1   8 use warnings;
  1         2  
  1         50  
5              
6             our $VERSION = '0.04';
7              
8 1     1   7 use JSON ();
  1         9  
  1         27  
9 1     1   7 use Cwd qw(abs_path);
  1         3  
  1         61  
10 1     1   823 use HTTP::Tiny;
  1         33527  
  1         499  
11              
12             sub new {
13 4     4 0 13436 my $class = shift;
14 4         24 my (%params) = @_;
15              
16 4         13 my $self = {};
17 4         12 bless $self, $class;
18              
19 4   50     26 $self->{base_url} = $params{base_url} || 'https://kritika.io';
20 4 50       19 $self->{token} = $params{token} or die 'token required';
21 4         12 $self->{root} = $params{root};
22 4         11 $self->{head} = $params{head};
23 4         12 $self->{changes} = $params{changes};
24              
25 4   33     35 $self->{ua} = $params{ua} || HTTP::Tiny->new;
26              
27 4         22 return $self;
28             }
29              
30             sub validate {
31 4     4 0 807 my $self = shift;
32 4         15 my ($path) = @_;
33              
34 4         331 $path = abs_path($path);
35              
36 4         15 my $content = do {
37 4         23 local $/;
38 4 50       154 open my $fh, '<', $path or die "Can't open file '$path': $!";
39 4         104 <$fh>;
40             };
41              
42 4         17 my $ua = $self->{ua};
43              
44 4 50       18 if ( my $root = $self->{root} ) {
45 4         271 $root = abs_path($root);
46 4         111 $path =~ s{^$root}{};
47 4         24 $path =~ s{^/}{};
48             }
49              
50             my $response = $ua->post_form(
51             "$self->{base_url}/validate",
52             {
53             $self->{head} ? ( head => $self->{head} ) : (),
54             $self->{changes} ? ( changes => $self->{changes} ) : (),
55             content => $content,
56             path => $path
57             },
58             { headers => { Authorization => 'Token ' . $self->{token} } }
59 4 50       106 );
    50          
60              
61 4 100       391 if ($response->{status} eq '599') {
62 1         4 my $content = $response->{content};
63 1 50       7 $content = substr($content, 0, 64) . '[...]' if length $content > 64;
64 1         16 die "Internal error: $response->{status} $content";
65             }
66              
67             die "Remote error: $response->{status} $response->{reason}\n"
68 3 100       21 unless $response->{success};
69              
70 2         21 return JSON::decode_json( $response->{content} );
71             }
72              
73             1;
74             __END__