line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Plywood::Simple; |
2
|
1
|
|
|
1
|
|
15842
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
32
|
|
3
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
26
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
24
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
675
|
use HTTP::Tiny; |
|
1
|
|
|
|
|
42128
|
|
|
1
|
|
|
|
|
36
|
|
7
|
1
|
|
|
1
|
|
1737
|
use Try::Tiny; |
|
1
|
|
|
|
|
1343
|
|
|
1
|
|
|
|
|
56
|
|
8
|
1
|
|
|
1
|
|
219
|
use JSON; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = 0.03; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
############ |
13
|
|
|
|
|
|
|
## Public ## |
14
|
|
|
|
|
|
|
############ |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub new { |
17
|
|
|
|
|
|
|
my $class = shift; |
18
|
|
|
|
|
|
|
my $params = ref($_[0]) ? $_[0] : {@_}; |
19
|
|
|
|
|
|
|
if (!$params->{scheme} || !$params->{host} || !$params->{port}) { |
20
|
|
|
|
|
|
|
die "arguments 'scheme', 'host' and 'port' are required"; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
$params->{_json} = JSON->new->utf8; |
23
|
|
|
|
|
|
|
return bless($params, $class); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub put { |
27
|
|
|
|
|
|
|
my ($self, $key, $data) = @_; |
28
|
|
|
|
|
|
|
if ($key =~ m/\//) { |
29
|
|
|
|
|
|
|
die 'can not have forward slash ( / ) characters in key names'; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
my $json; |
32
|
|
|
|
|
|
|
try { |
33
|
|
|
|
|
|
|
$json = $self->{_json}->encode($data); |
34
|
|
|
|
|
|
|
} catch { |
35
|
|
|
|
|
|
|
die "could not encode tree as JSON, error was: $_"; |
36
|
|
|
|
|
|
|
}; |
37
|
|
|
|
|
|
|
my $response; |
38
|
|
|
|
|
|
|
try { |
39
|
|
|
|
|
|
|
$response = HTTP::Tiny->new->put( |
40
|
|
|
|
|
|
|
"$self->{scheme}://$self->{host}:$self->{port}/tree/$key", |
41
|
|
|
|
|
|
|
{content => $json}, |
42
|
|
|
|
|
|
|
); |
43
|
|
|
|
|
|
|
} catch { |
44
|
|
|
|
|
|
|
die "could not store tree, error was: $_"; |
45
|
|
|
|
|
|
|
}; |
46
|
|
|
|
|
|
|
if (!$response->{success}) { |
47
|
|
|
|
|
|
|
die "$response->{reason} ($response->{status}): $response->{content}"; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
return $response; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub get { |
53
|
|
|
|
|
|
|
my ($self, $key, @path) = @_; |
54
|
|
|
|
|
|
|
my $path = join('/', @path); |
55
|
|
|
|
|
|
|
my $response; |
56
|
|
|
|
|
|
|
try { |
57
|
|
|
|
|
|
|
$response = HTTP::Tiny->new->get( |
58
|
|
|
|
|
|
|
"$self->{scheme}://$self->{host}:$self->{port}/tree/$key/$path", |
59
|
|
|
|
|
|
|
); |
60
|
|
|
|
|
|
|
} catch { |
61
|
|
|
|
|
|
|
die "could not get tree, error was: $_"; |
62
|
|
|
|
|
|
|
}; |
63
|
|
|
|
|
|
|
if (!$response->{success}) { |
64
|
|
|
|
|
|
|
die "$response->{reason} ($response->{status}): $response->{content}"; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
return $response; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
1; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
__END__ |