File Coverage

blib/lib/Regru/API/Response.pm
Criterion Covered Total %
statement 39 39 100.0
branch 14 16 87.5
condition 2 3 66.6
subroutine 10 10 100.0
pod 1 1 100.0
total 66 69 95.6


line stmt bran cond sub pod time code
1              
2             # ABSTRACT: REG.API v2 response wrapper
3              
4             use strict;
5 13     13   83879 use warnings;
  13         25  
  13         342  
6 13     13   58 use Moo;
  13         36  
  13         260  
7 13     13   480 use Try::Tiny;
  13         9324  
  13         65  
8 13     13   4853 use Carp;
  13         1088  
  13         642  
9 13     13   66 use namespace::autoclean;
  13         36  
  13         685  
10 13     13   863  
  13         19295  
  13         108  
11             our $VERSION = '0.052'; # VERSION
12             our $AUTHORITY = 'cpan:CHIM'; # AUTHORITY
13              
14             with qw(
15             Regru::API::Role::Serializer
16             Regru::API::Role::Loggable
17             );
18              
19             has error_code => ( is => 'rw' );
20             has error_text => ( is => 'rw' );
21             has error_params => ( is => 'rw' );
22             has is_success => ( is => 'rw' );
23             has is_service_fail => ( is => 'rw' );
24              
25             has answer => (
26             is => 'rw',
27             default => sub { +{} }
28             );
29              
30             has response => (
31             is => 'rw',
32             trigger => 1,
33             );
34              
35             has debug => (
36             is => 'rw',
37             default => sub { 0 }
38             );
39              
40             my ($self, $response) = @_;
41              
42 8     8   7589 if ($response) {
43             try {
44 8 50       18 die 'Invalid response' unless ref $response eq 'HTTP::Response';
45              
46 8 100   8   295 $self->debug_warn('REG.API response code', $response->code) if $self->debug;
47              
48 7 100       23 $self->is_service_fail($response->code == 200 ? 0 : 1);
49              
50 7 100       81 if ($self->is_service_fail) {
51             # Stop processing response
52 7 100       77 $self->is_success(0);
53             die 'Service failed: ' . $response->content;
54 3         5 }
55 3         7  
56             my $decoded = $self->serializer->decode($response->content);
57             $self->is_success($decoded->{result} && $decoded->{result} eq 'success');
58 4         72  
59 2   66     69 $self->debug_warn('REG.API request', ($self->is_success ? 'success' : 'fail')) if $self->debug;
60             if ($self->is_success) {
61 2 50       11 $self->answer($decoded->{answer});
    100          
62 2 100       48 }
63 1         8 else {
64             foreach my $attr (qw/error_code error_text error_params/) {
65             $self->$attr($decoded->{$attr});
66 1         3 }
67 3         11 }
68             }
69             catch {
70             carp 'Error: ' . $_;
71             $self->error_code('API_FAIL');
72 6     6   641 $self->error_text('API response error');
73 6         230 };
74 6         32 }
75 8         47 }
76              
77             my ($self, $attr) = @_;
78              
79             return $self->answer->{$attr};
80 2     2 1 1954 }
81              
82 2         10 1; # End of Regru::API::Response
83              
84              
85             =pod
86              
87             =encoding UTF-8
88              
89             =head1 NAME
90              
91             Regru::API::Response - REG.API v2 response wrapper
92              
93             =head1 VERSION
94              
95             version 0.052
96              
97             =head1 SYNOPSIS
98              
99             my $resp = Regru::API::Response->new(
100             response => $response,
101             );
102              
103             =head1 ATTRIBUTES
104              
105             =head2 is_service_fail
106              
107             Flag to show whether or not the most last answer from the API service has not been finished with code I<HTTP 200>.
108              
109             $resp = $client->bill->nop(bill_id => 123213);
110              
111             if ($resp->is_success) {
112             print "It works!";
113             }
114             elsif ($resp->is_service_fail) {
115             print "Reg.ru API is gone :(";
116             }
117             else {
118             print "Error code: ". $resp->error_code;
119             }
120              
121             =head2 is_success
122              
123             Flag to show whether or not the most last API request has been successful.
124              
125             See example for L</is_service_fail>.
126              
127             =head2 response
128              
129             Contains a L<HTTP::Response> object for the most last API request.
130              
131             if ($resp->is_service_fail) {
132             print "HTTP code: " . $resp->response->code;
133             }
134              
135             =head2 answer
136              
137             Contains decoded answer for the most last successful API request.
138              
139             if ($resp->is_success) {
140             print Dumper($resp->answer);
141             }
142              
143             This is useful for debugging;
144              
145             =head2 error_code
146              
147             Contains error code for the most last API request if it has not been successful.
148              
149             Full list error codes list is available at
150             L<REG.API Common error codes|https://www.reg.com/support/help/api2#common_errors>.
151              
152             =head2 error_text
153              
154             Contains common error text for the most last API request if it has not been successful.
155              
156             Default language is B<enlish>. Language can be changed by passing option C<lang> to the
157             L<Regru::API> constructor.
158              
159             =head2 error_params
160              
161             Contains additional parameters included into the common error text.
162              
163             $error_params = $resp->error_params;
164             print "Details: " . $error_params->{error_detail};
165              
166             =head2 debug
167              
168             A few messages will be printed to STDERR. Default value is B<0> (suppressed debug activity).
169              
170             =head1 METHODS
171              
172             =head2 new
173              
174             Creates a response object from REG.API response. Available options:
175              
176             =over
177              
178             =item B<response>
179              
180             Required. This should be a result of HTTP request to REG.API. In general, is a L<HTTP::Response> object returned by
181             L<LWP::UserAgent>.
182              
183             =item B<debug>
184              
185             Not required. Print some debugging messages to STDERR. Default value is B<0>. Because of this contructor invoked from
186             L<Regru::API::Role::Client> mainly so this option sets to the value which passed to L<Regru::API> constructor.
187              
188             =back
189              
190             =head2 get
191              
192             Gets a value from stored in answer.
193              
194             $resp = $client->user->get_statistics;
195             print "Account balance: " . $resp->get("balance_total");
196              
197             =head1 SEE ALSO
198              
199             L<Regru::API>
200              
201             L<Regru::API::Role::Client>
202              
203             L<Regru::API::Role::Serializer>
204              
205             L<Regru::API::Role::Loggable>
206              
207             L<HTTP::Response>
208              
209             L<LWP::UserAgent>
210              
211             L<REG.API Common error codes|https://www.reg.com/support/help/api2#common_errors>
212              
213             =head1 BUGS
214              
215             Please report any bugs or feature requests on the bugtracker website
216             L<https://github.com/regru/regru-api-perl/issues>
217              
218             When submitting a bug or request, please include a test-file or a
219             patch to an existing test-file that illustrates the bug or desired
220             feature.
221              
222             =head1 AUTHORS
223              
224             =over 4
225              
226             =item *
227              
228             Polina Shubina <shubina@reg.ru>
229              
230             =item *
231              
232             Anton Gerasimov <a.gerasimov@reg.ru>
233              
234             =back
235              
236             =head1 COPYRIGHT AND LICENSE
237              
238             This software is copyright (c) 2013 by REG.RU LLC.
239              
240             This is free software; you can redistribute it and/or modify it under
241             the same terms as the Perl 5 programming language system itself.
242              
243             =cut