File Coverage

blib/lib/Data/Riak/Fast/Link.pm
Criterion Covered Total %
statement 12 28 42.8
branch 0 16 0.0
condition n/a
subroutine 4 6 66.6
pod 0 2 0.0
total 16 52 30.7


line stmt bran cond sub pod time code
1             package Data::Riak::Fast::Link;
2              
3 23     23   907 use Mouse;
  23         39656  
  23         156  
4              
5 23     23   26321 use URL::Encode qw/url_encode url_decode/;
  23         131671  
  23         1478  
6 23     23   23178 use HTTP::Headers::ActionPack::LinkHeader;
  23         782210  
  23         11955  
7              
8             has bucket => (
9             is => 'ro',
10             isa => 'Str',
11             required => 1
12             );
13              
14             has key => (
15             is => 'ro',
16             isa => 'Str',
17             predicate => 'has_key'
18             );
19              
20             has riaktag => (
21             is => 'ro',
22             isa => 'Str',
23             predicate => 'has_riaktag'
24             );
25              
26             has params => (
27             is => 'ro',
28             isa => 'HashRef',
29             default => sub { +{} }
30             );
31              
32             sub from_link_header {
33 0     0 0   my ($class, $link_header) = @_;
34              
35 0           my ($bucket, $key, $riaktag);
36              
37             # link to another key in riak
38 0 0         if ($link_header->href =~ /^\/buckets\/(.*)\/keys\/(.*)/) {
    0          
39 0           ($bucket, $key) = ($1, $2);
40             }
41             # link to a bucket
42             elsif ($link_header->href =~ /^\/buckets\/(.*)/) {
43 0           $bucket = $1;
44             }
45             else {
46 0           confess "Incompatible link header URL (" . $link_header->href . ")";
47             }
48              
49 0           my %params = %{ $link_header->params };
  0            
50              
51 0 0         $riaktag = url_decode( delete $params{'riaktag'} )
52             if exists $params{'riaktag'};
53              
54 0 0         $class->new(
    0          
55             bucket => $bucket,
56             ($key ? (key => $key) : ()),
57             ($riaktag ? (riaktag => $riaktag) : ()),
58             params => \%params
59             );
60             }
61              
62             sub as_link_header {
63 0     0 0   my $self = shift;
64 0 0         if ($self->has_key) {
65 0           return HTTP::Headers::ActionPack::LinkHeader->new(
66             sprintf('/buckets/%s/keys/%s', $self->bucket, $self->key),
67             ($self->has_riaktag ? (riaktag => url_encode($self->riaktag)) : ()),
68 0 0         %{ $self->params }
69             );
70             }
71             else {
72 0           return HTTP::Headers::ActionPack::LinkHeader->new(
73             sprintf('/buckets/%s', $self->bucket),
74             ($self->has_riaktag ? (riaktag => url_encode($self->riaktag)) : ()),
75 0 0         %{ $self->params }
76             );
77             }
78             }
79              
80              
81             __PACKAGE__->meta->make_immutable;
82 23     23   614 no Mouse;
  23         55  
  23         239  
83              
84             1;
85              
86             __END__