File Coverage

blib/lib/WebService/Toggl/Role/Base.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package WebService::Toggl::Role::Base;
2              
3 6     6   36641 use Module::Runtime qw(use_package_optimistically);
  6         9  
  6         34  
4 6     6   4043 use Storable qw(dclone);
  6         17302  
  6         392  
5 6     6   2422 use WebService::Toggl::Request;
  0            
  0            
6              
7             use Moo::Role;
8              
9             has api_key => (is => 'ro');
10             has server => (is => 'ro', default => 'https://www.toggl.com');
11              
12             has _request => (is => 'ro', lazy => 1, builder => 1);
13             sub _build__request { WebService::Toggl::Request->new({
14             api_key => $_[0]->api_key, server => $_[0]->server,
15             }) }
16              
17             sub api_get { shift->_request->get(@_) }
18             sub api_post { shift->_request->post(@_) }
19             sub api_put { shift->_request->put(@_) }
20             sub api_delete { shift->_request->delete(@_) }
21              
22             sub new_item {
23             my ($self, $class, $args) = @_;
24             use_package_optimistically('WebService::Toggl::API' . $class)
25             ->new({_request => $self->_request, %$args});
26             }
27              
28             sub new_item_from_raw {
29             my ($self, $class, $raw) = @_;
30             $self->new_item($class, {raw => dclone($raw)});
31             }
32              
33             sub new_report {
34             my ($self, $class, $args) = @_;
35             use_package_optimistically('WebService::Toggl::Report' . $class)
36             ->new({_request => $self->_request, %$args});
37             }
38              
39             sub new_set { shift->new_item(@_) }
40              
41             sub new_set_from_raw { shift->new_item_from_raw(@_) }
42              
43              
44              
45             1;
46             __END__
47              
48             =encoding utf-8
49              
50             =head1 NAME
51              
52             WebService::Toggl::Role::Base - Common behavior for all WebService::Toggl objects
53              
54             =head1 DESCRIPTION
55              
56             This role provide behavior common to all C<WebService::Toggl::API::>
57             and C<WebService::Toggl::Report::> objects.
58              
59             =head1 ATTRIBUTES
60              
61             =head2 api_key
62              
63             The API token used to identify the authorized user. If you don't
64             provide this, you'll need to supply the C<_request> attribute.
65              
66             =head2 server
67              
68             The base URL for the Toggl API server. Defaults to 'https://www.toggl.com'.
69              
70             =head2 _request
71              
72             The object that sets the headers and makes the requests. Defaults to
73             a L<WebService::Toggl::Request> object that uses L<Role::REST::Client>.
74              
75             =head1 METHODS
76              
77             =head2 api_get($url, $data, $args)
78              
79             =head2 api_post($url, $data, $args)
80              
81             =head2 api_put($url, $data, $args)
82              
83             =head2 api_delete($url, $data, $args)
84              
85             These are proxy methods to the C<get>, C<post>, C<put>, and C<delete>
86             methods available on the C<_request> object via L<Role::REST::Client>.
87              
88             =head2 new_item($class, \%args)
89              
90             Creates a new object of type C<WebService::Toggl::API::$class>. The
91             new object receives the C<_request> attribute of the calling object,
92             and so does not need the C<api_key> attribute to be set. C<\%args>
93             will be passed through to the constructor of the new object.
94              
95             =head2 new_item_from_raw($class, \%raw)
96              
97             Similar to C<new_item()> but sets the new object's C<raw> attribute to
98             the C<\%raw> argument. This obviates the need for querying the API to
99             get the object.
100              
101             =head2 new_report($class, $args)
102              
103             Same as C<new_item()>, but creates an object of type
104             C<WebService::Toggl::Report::$class>.
105              
106             =head2 new_set($class, $args)
107              
108             Proxies to C<new_item()>. If API Items and Sets are split into
109             different classes, this may change.
110              
111             =head2 new_set_from_raw($class, $raw)
112              
113             Proxies to C<new_item_from_raw()>. If API Items and Sets are split into
114             different classes, this may change.
115              
116             =head1 LICENSE
117              
118             Copyright (C) Fitz Elliott.
119              
120             This library is free software; you can redistribute it and/or modify
121             it under the same terms as Perl itself.
122              
123             =head1 AUTHOR
124              
125             Fitz Elliott E<lt>felliott@fiskur.orgE<gt>
126              
127             =cut