Data/Riak/Link.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Data/Riak/HTTP.pm ↑ Summary Test/Data/Riak.pm »
1 package Data::Riak::Link;
2 {
3 $Data::Riak::Link::VERSION = '2.0';
4 }
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 use URL::Encode qw/url_encode url_decode/;
12 use HTTP::Headers::ActionPack::LinkHeader;
13  
14 use namespace::autoclean;
15  
16 has bucket => (
17 is => 'ro',
18 isa => 'Str',
19 required => 1
20 );
21  
22 has key => (
23 is => 'ro',
24 isa => 'Str',
25 predicate => 'has_key'
26 );
27  
28 has riaktag => (
29 is => 'ro',
30 isa => 'Str',
31 predicate => 'has_riaktag'
32 );
33  
34 has params => (
35 is => 'ro',
36 isa => 'HashRef',
37 default => sub { +{} }
38 );
39  
40 sub from_link_header {
41 my ($class, $link_header) = @_;
42  
43 my ($bucket, $key, $riaktag);
44  
45 # link to another key in riak
46 if ($link_header->href =~ /^\/buckets\/(.*)\/keys\/(.*)/) {
47 ($bucket, $key) = ($1, $2);
48 }
49 # link to a bucket
50 elsif ($link_header->href =~ /^\/buckets\/(.*)/) {
51 $bucket = $1;
52 }
53 else {
54 confess "Incompatible link header URL (" . $link_header->href . ")";
55 }
56  
57 my %params = %{ $link_header->params };
58  
59 $riaktag = url_decode( delete $params{'riaktag'} )
60 if exists $params{'riaktag'};
61  
62 $class->new(
63 bucket => $bucket,
64 ($key ? (key => $key) : ()),
65 ($riaktag ? (riaktag => $riaktag) : ()),
66 params => \%params
67 );
68 }
69  
70 sub as_link_header {
71 my $self = shift;
72 if ($self->has_key) {
73 return HTTP::Headers::ActionPack::LinkHeader->new(
74 sprintf('/buckets/%s/keys/%s', $self->bucket, $self->key),
75 ($self->has_riaktag ? (riaktag => url_encode($self->riaktag)) : ()),
76 %{ $self->params }
77 );
78 }
79 else {
80 return HTTP::Headers::ActionPack::LinkHeader->new(
81 sprintf('/buckets/%s', $self->bucket),
82 ($self->has_riaktag ? (riaktag => url_encode($self->riaktag)) : ()),
83 %{ $self->params }
84 );
85 }
86 }
87  
88  
89 __PACKAGE__->meta->make_immutable;
90  
91 1;
92  
93 __END__
« Data/Riak/HTTP.pm ↑ Summary Test/Data/Riak.pm »