File Coverage

blib/lib/Rest/Client/Builder.pm
Criterion Covered Total %
statement 49 49 100.0
branch 7 10 70.0
condition n/a
subroutine 14 14 100.0
pod 0 7 0.0
total 70 80 87.5


line stmt bran cond sub pod time code
1             package Rest::Client::Builder;
2              
3 2     2   1008 use strict;
  2         3  
  2         66  
4 2     2   10 use warnings;
  2         3  
  2         401  
5              
6             our $VERSION = '0.02';
7             our $AUTOLOAD;
8              
9             sub new {
10 8     8 0 36 my ($class, $opts, $path) = @_;
11              
12 8 50       100 return bless({
13             on_request => $opts->{on_request},
14             path => defined $path ? $path : '',
15             opts => { %$opts },
16             }, $class);
17             }
18              
19             sub _construct {
20 11     11   16 my ($self, $path) = (shift, shift);
21              
22 11         10 my $id = $path;
23 11         22 my $class = ref($self) . '::' . $path;
24              
25 11 50       62 if (defined $self->{path}) {
26 11         23 $path = $self->{path} . '/' . $path;
27             }
28              
29 11 100       26 if (@_) {
30 8         19 my $tail = '/' . join('/', @_);
31 8         9 $id .= $tail;
32 8         12 $path .= $tail;
33             }
34              
35 11 100       31 unless ($self->{objects}->{$id}) {
36 7         20 $self->{objects}->{$id} = bless(Rest::Client::Builder->new($self->{opts}, $path), $class);
37             }
38              
39 2     2   19 no strict 'refs';
  2         2  
  2         244  
40 11         15 push @{$class . '::ISA'}, ref($self);
  11         204  
41              
42 11         153 return $self->{objects}->{$id};
43             }
44              
45             sub AUTOLOAD {
46 5     5   18 my $self = shift;
47              
48 5         32 (my $method = $AUTOLOAD) =~ s{.*::}{};
49 5 50       15 return undef if $method eq 'DESTROY';
50 2     2   10 no strict 'refs';
  2         3  
  2         573  
51 5         9 my $ref = ref($self);
52              
53 5         27 *{$ref . '::' . $method} = sub {
54 11     11   2987 my $self = shift;
55 11         41 $self->_construct($method, @_);
56 5         20 };
57              
58 5         15 return $self->$method(@_);
59             }
60              
61             sub get {
62 1     1 0 2 my ($self, $args) = @_;
63 1         8 return $self->{on_request}->('GET', $self->{path}, $args);
64             }
65              
66             sub post {
67 1     1 0 2 my ($self, $args) = @_;
68 1         13 return $self->{on_request}->('POST', $self->{path}, $args);
69             }
70              
71             sub put {
72 1     1 0 2 my ($self, $args) = @_;
73 1         10 return $self->{on_request}->('PUT', $self->{path}, $args);
74             }
75              
76             sub delete {
77 1     1 0 2 my ($self, $args) = @_;
78 1         13 return $self->{on_request}->('DELETE', $self->{path}, $args);
79             }
80              
81             sub patch {
82 1     1 0 3 my ($self, $args) = @_;
83 1         13 return $self->{on_request}->('PATCH', $self->{path}, $args);
84             }
85              
86             sub head {
87 1     1 0 2 my ($self, $args) = @_;
88 1         14 return $self->{on_request}->('HEAD', $self->{path}, $args);
89             }
90              
91             1;
92              
93             __END__