blib/lib/Mojo/CloudCheckr.pm

filter: Statementstmt 81.2% 26 / 32 Branchbran 50.0% 3 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.1% 4 / 7 Podpod 100% 1 / 1 totaltotal 73.9% 34 / 46 CC 4 SCAR 10.2
CC
4
Cov
76%
get
5.1
__ANON__
2.0
__ANON__
1.1
CRAP 2.8
↑ Summary
1 package Mojo::CloudCheckr;
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 0 ▶has access_key => sub { die 'missing access_key' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
9 1 has base_url => 'https://api.cloudcheckr.com/api';
10 1 has format => 'json';
11 0 ▶has _ua => sub { Mojo::UserAgent->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
12  
13 1 ▶sub get {
Subroutine get: called CC=4 SCAR=16.3
Pod documented
14 1 ▶ my ($self, $cb) = (shift, ref $_[-1] eq 'CODE' ? pop @_ : ());
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 1 1
15 1 my ($controller, $action) = (shift, shift);
16 1 ▶ my $args = @_ % 2 == 0 ? {@_} : shift;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 0 ? : 1 0 1
17 1 ▶ if ( $cb ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 0 1 1
18 0 ▶ $self->_ua->get($self->_url($controller, $action) => form => $args => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 0 my ($ua, $tx) = @_;
20 0 $tx->req->params->remove('access_key');
21 0 $cb->($ua, $tx);
22 0 });
23 } else {
24 1 my $tx = $self->_ua->get($self->_url($controller, $action) => form => $args);
25 1 $tx->req->params->remove('access_key');
26 1 return $tx;
27 }
28 }
29  
30 1 ▶sub _url {
Subroutine _url: called CC=1 SCAR=0.0
31 1 my ($self, $controller, $action) = @_;
32 1 my $format = $self->format;
33 1 my $url = Mojo::URL->new($self->base_url);
34 1 push @{$url->path->parts}, "$controller.$format", $action;
35 1 $url->query(access_key => $self->access_key);
36 1 return $url;
37 }
38  
39 1 1;
40  
41 =encoding utf8
42  
43 =head1 NAME
44  
45 Mojo::CloudCheckr - A simple interface to the CloudCheckr API
46  
47 =head1 SYNOPSIS
48  
49 use Mojo::CloudCheckr;
50  
51 my $cc = Mojo::CloudCheckr->new(access_key => '...');
52 say $cc->get(account => 'get_accounts_v2')->result->json('/accounts_and_users/0/account_name');
53
54 =head1 DESCRIPTION
55  
56 A simple interface to the CloudCheckr API.
57  
58 Currently only a single L<get> method is available, and it offers no data
59 validation or error handling. No built-in support for paging. No support for
60 POST queries (GET only). Pull requests welcome!
61  
62 The API user guide is available at L<http://support.cloudcheckr.com/cloudcheckr-api-userguide/>
63 The API reference guide is available at L<http://support.cloudcheckr.com/cloudcheckr-api-userguide/cloudcheckr-api-reference-guide/>
64 The API admin reference guide is available at L<http://support.cloudcheckr.com/cloudcheckr-api-userguide/cloudcheckr-admin-api-reference-guide/>
65 The API inventory guide is available at L<http://support.cloudcheckr.com/api-reference-guide-inventory/>
66  
67 So what does this module do? It makes building the API URL easier and includes
68 the access key on all requests. It then removes the access key from the request
69 message of the returned transaction. So, not much. But it does offer a little
70 bit of sugar, and, hopefully eventually, some data validation, error handling,
71 built-in support for paging, and POST queries.
72  
73 =head1 ATTRIBUTES
74  
75 L<Mojo::CloudCheckr> implements the following attributes.
76  
77 =head2 access_key
78  
79 my $access_key = $cc->access_key;
80 $cc = $cc->access_key($key);
81  
82 The access_key for your CloudCheckr API. Learn more at L<http://support.cloudcheckr.com/cloudcheckr-api-userguide/>
83  
84 =head2 base_url
85  
86 my $base_url = $cc->base_url;
87 $cc = $cc->base_url($url);
88  
89 The base URL for the CloudCheckr API, defaults to https://api.cloudcheckr.com/api.
90  
91 =head2 format
92  
93 my $format = $cc->format;
94 $cc = $cc->format($format);
95  
96 The response format from the CloudCheckr API, defaults to json.
97  
98 # Set the format to XML
99 $cc->format('xml');
100  
101 =head1 METHODS
102  
103 L<Mojo::CloudCheckr> inherits all methods from L<Mojo::Base> and implements the
104 following new ones.
105  
106 =head2 get
107  
108 # Blocking
109 my $tx = $cc->get(controller => 'action', %args);
110 say $tx->result->body;
111
112 # Non-blocking
113 $cc->get(controller => 'action', %args => sub {
114 my ($ua, $tx) = @_;
115 say $tx->result->body;
116 });
117  
118 All CloudCheckr API calls have a controller (or category) and an action (or
119 task). Each of these is required for L<get> and is defined in the API
120 reference docs. If a callback is provided, it will process the request non-
121 blocking. The access_key parameter will be removed from the request in the
122 returned transaction (so that the arguments that were used to generate the
123 response can be dumped without the need to hide this private key.)
124  
125 =head1 SEE ALSO
126  
127 L<http://cloudcheckr.com>
128  
129 =cut
↑ Summary