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   1017 use strict;
  2         3  
  2         65  
4 2     2   7 use warnings;
  2         2  
  2         359  
5              
6             our $VERSION = '0.01';
7             our $AUTOLOAD;
8              
9             sub new {
10 8     8 0 33 my ($class, $opts, $path) = @_;
11              
12 8 50       88 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         11 my $id = $path;
23 11         16 my $class = ref($self) . '::' . $path;
24              
25 11 50       51 if (defined $self->{path}) {
26 11         20 $path = $self->{path} . '/' . $path;
27             }
28              
29 11 100       23 if (@_) {
30 8         19 my $tail = '/' . join('/', @_);
31 8         8 $id .= $tail;
32 8         9 $path .= $tail;
33             }
34              
35 11 100       27 unless ($self->{objects}->{$id}) {
36 7         18 $self->{objects}->{$id} = bless(Rest::Client::Builder->new($self->{opts}, $path), $class);
37             }
38              
39 2     2   20 no strict 'refs';
  2         2  
  2         259  
40 11         13 push @{$class . '::ISA'}, ref($self);
  11         152  
41              
42 11         124 return $self->{objects}->{$id};
43             }
44              
45             sub AUTOLOAD {
46 5     5   17 my $self = shift;
47              
48 5         27 (my $method = $AUTOLOAD) =~ s{.*::}{};
49 5 50       14 return undef if $method eq 'DESTROY';
50 2     2   11 no strict 'refs';
  2         2  
  2         616  
51 5         6 my $ref = ref($self);
52              
53 5         23 *{$ref . '::' . $method} = sub {
54 11     11   2343 my $self = shift;
55 11         38 $self->_construct($method, @_);
56 5         18 };
57              
58 5         12 return $self->$method(@_);
59             }
60              
61             sub get {
62 1     1 0 2 my ($self, $args) = @_;
63 1         7 return $self->{on_request}->('GET', $self->{path}, $args);
64             }
65              
66             sub post {
67 1     1 0 2 my ($self, $args) = @_;
68 1         8 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 1 my ($self, $args) = @_;
78 1         8 return $self->{on_request}->('DELETE', $self->{path}, $args);
79             }
80              
81             sub patch {
82 1     1 0 3 my ($self, $args) = @_;
83 1         7 return $self->{on_request}->('PATCH', $self->{path}, $args);
84             }
85              
86             sub head {
87 1     1 0 3 my ($self, $args) = @_;
88 1         10 return $self->{on_request}->('HEAD', $self->{path}, $args);
89             }
90              
91             1;
92              
93             __END__