blib/lib/Mojo/CallFire.pm

filter: Statementstmt 74.1% 23 / 31 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 6 / 12 Podpod 100% 4 / 4 totaltotal 68.6% 35 / 51 CC 3 SCAR 5.9
CC
3
Cov
71%
_tx
3.7
put
2.0
del
2.0
CRAP 1.8
↑ Summary
1 package Mojo::CallFire;
2 1 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 our $VERSION = '0.01';
5  
6 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 has base_url => 'https://api.callfire.com/v2';
9 0 ▶has password => sub { die 'missing password' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
10 0 ▶has username => sub { die 'missing username' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
11 0 ▶has _ua => sub { Mojo::UserAgent->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
12  
13 1 ▶sub get { shift->_tx(get => @_) }
Subroutine get: called CC=1 SCAR=0.0
Pod documented
14 1 ▶sub post { shift->_tx(post => @_) }
Subroutine post: called CC=1 SCAR=0.0
Pod documented
15 0 ▶sub put { shift->_tx(put => @_) }
Subroutine put: not called CC=1 SCAR=6.9
Pod documented
16 0 ▶sub del { shift->_tx(delete => @_) }
Subroutine del: not called CC=1 SCAR=6.9
Pod documented
17  
18 2 ▶sub _tx {
Subroutine _tx: called CC=3 SCAR=13.0
19 2 ▶ my ($self, $method, $cb) = (shift, shift, ref $_[-1] eq 'CODE' ? pop @_ : ());
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 2 2
20 2 my $path = shift;
21 2 ▶ if ( $cb ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 0 2 2
22 0 ▶ $self->_ua->$method($self->_url($path) => @_ => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 0 my ($ua, $tx) = @_;
24 0 $cb->($ua, $tx);
25 0 });
26 } else {
27 2 my $tx = $self->_ua->$method($self->_url($path) => @_);
28 2 return $tx;
29 }
30 }
31  
32 2 ▶sub _url {
Subroutine _url: called CC=1 SCAR=0.0
33 2 my ($self, $path) = @_;
34 2 my $url = Mojo::URL->new($self->base_url);
35 2 $url->userinfo(join ':', $self->username, $self->password)->path($path);
36 2 return $url;
37 }
38  
39 1 1;
40  
41 =encoding utf8
42  
43 =head1 NAME
44  
45 Mojo::CallFire - A simple interface to the CallFire API
46  
47 =head1 SYNOPSIS
48  
49 use Mojo::CallFire;
50  
51 my $cf = Mojo::CallFire->new(username => '...', password => '...');
52 say $cf->get('/calls')->result->json('/items/0/id');
53
54 =head1 DESCRIPTION
55  
56 A simple interface to the CallFire API.
57  
58 Currently only L<get>, L<post>, L<put>, and L<delete> methods are available,
59 and they offer no data validation or error handling. No built-in support for
60 paging. Pull requests welcome!
61  
62 The API reference guide is available at L<https://developers.callfire.com/docs.html>
63  
64 So what does this module do? It makes building the API URL easier and includes
65 the username and password on all requests. So, not much. But it does offer a
66 little bit of sugar, and, hopefully eventually, some data validation, error
67 handling, and built-in support for paging.
68  
69 =head1 ATTRIBUTES
70  
71 L<Mojo::CallFire> implements the following attributes.
72  
73 =head2 base_url
74  
75 my $base_url = $cf->base_url;
76 $cf = $cf->base_url($url);
77  
78 The base URL for the CallFire API, defaults to https://api.callfire.com/v2.
79  
80 =head2 password
81  
82 my $password = $cf->password;
83 $cf = $cf->password($password);
84  
85 The password for the CallFire API. Generate a password API credential on
86 CallFire's API access page. Read more at the Authentication section of the
87 API Reference at L<https://developers.callfire.com/docs.html#authentication>.
88  
89 =head2 username
90  
91 my $username = $cf->username;
92 $cf = $cf->username($username);
93  
94  
95 The username for the CallFire API. Generate a username API credential on
96 CallFire's API access page. Read more at the Authentication section of the
97 API Reference at L<https://developers.callfire.com/docs.html#authentication>.
98  
99 =head1 METHODS
100  
101 L<Mojo::CallFire> inherits all methods from L<Mojo::Base> and implements the
102 following new ones.
103  
104 =head2 del
105  
106 # Blocking
107 my $tx = $cf->del('/rest/endpoint', %args);
108 say $tx->result->body;
109
110 # Non-blocking
111 $cf->del('/rest/endpoint', %args => sub {
112 my ($ua, $tx) = @_;
113 say $tx->result->body;
114 });
115  
116 A RESTful DELETE method. Accepts the same arguments as L<Mojo::UserAgent> with
117 the exception that the URL is built starting from the L<base_url> and the
118 Basic HTTP Athorization of the username and password are automatically applied
119 on each request.
120  
121 See the CallFire API Reference at L<https://developers.callfire.com/docs.html>
122 for the HTTP methods, URL path, and parameters to supply for each desired
123 action.
124  
125 =head2 get
126  
127 # Blocking
128 my $tx = $cf->get('/rest/endpoint', %args);
129 say $tx->result->body;
130
131 # Non-blocking
132 $cf->get('/rest/endpoint', %args => sub {
133 my ($ua, $tx) = @_;
134 say $tx->result->body;
135 });
136  
137 A RESTful GET method. Accepts the same arguments as L<Mojo::UserAgent> with
138 the exception that the URL is built starting from the L<base_url> and the
139 Basic HTTP Athorization of the username and password are automatically applied
140 on each request.
141  
142 See the CallFire API Reference at L<https://developers.callfire.com/docs.html>
143 for the HTTP methods, URL path, and parameters to supply for each desired
144 action.
145  
146 =head2 post
147  
148 # Blocking
149 my $tx = $cf->post('/rest/endpoint', %args);
150 say $tx->result->body;
151
152 # Non-blocking
153 $cf->post('/rest/endpoint', %args => sub {
154 my ($ua, $tx) = @_;
155 say $tx->result->body;
156 });
157  
158 A RESTful POST method. Accepts the same arguments as L<Mojo::UserAgent> with
159 the exception that the URL is built starting from the L<base_url> and the
160 Basic HTTP Athorization of the username and password are automatically applied
161 on each request.
162  
163 See the CallFire API Reference at L<https://developers.callfire.com/docs.html>
164 for the HTTP methods, URL path, and parameters to supply for each desired
165 action.
166  
167 =head2 put
168  
169 # Blocking
170 my $tx = $cf->put('/rest/endpoint', %args);
171 say $tx->result->body;
172
173 # Non-blocking
174 $cf->put('/rest/endpoint', %args => sub {
175 my ($ua, $tx) = @_;
176 say $tx->result->body;
177 });
178  
179 A RESTful PUT method. Accepts the same arguments as L<Mojo::UserAgent> with
180 the exception that the URL is built starting from the L<base_url> and the
181 Basic HTTP Athorization of the username and password are automatically applied
182 on each request.
183  
184 See the CallFire API Reference at L<https://developers.callfire.com/docs.html>
185 for the HTTP methods, URL path, and parameters to supply for each desired
186 action.
187  
188 =head1 SEE ALSO
189  
190 L<http://callfire.com>
191  
192 =cut
↑ Summary