File Coverage

blib/lib/Regru/API/Role/Client.pm
Criterion Covered Total %
statement 29 42 69.0
branch 1 10 10.0
condition 1 2 50.0
subroutine 8 25 32.0
pod n/a
total 39 79 49.3


line stmt bran cond sub pod time code
1              
2             # ABSTRACT: something that makes requests to API
3              
4             use strict;
5 12     12   96337 use warnings;
  12         24  
  12         298  
6 12     12   55 use Moo::Role;
  12         24  
  12         253  
7 12     12   59 use Regru::API::Response;
  12         18  
  12         68  
8 12     12   7993 use namespace::autoclean;
  12         38  
  12         348  
9 12     12   70 use Carp;
  12         22  
  12         65  
10 12     12   630  
  12         22  
  12         8294  
11             our $VERSION = '0.052'; # VERSION
12             our $AUTHORITY = 'cpan:CHIM'; # AUTHORITY
13              
14             with qw(
15             Regru::API::Role::Namespace
16             Regru::API::Role::Serializer
17             Regru::API::Role::UserAgent
18             Regru::API::Role::Loggable
19             );
20              
21             has username => (
22             is => 'rw',
23             required => 1,
24             predicate => 'has_username',
25             );
26             has password => (
27             is => 'rw',
28             required => 1,
29             predicate => 'has_password',
30             );
31             has io_encoding => (
32             is => 'rw',
33             isa => sub {
34             my %valid = map { ($_ => 1) } qw(utf8 cp1251 cp866 koi8-r koi8-u);
35             croak "Empty encoding value" unless $_[0];
36             croak "Unsupported encoding: $_[0]" unless exists $valid{$_[0]};
37             },
38             predicate => 'has_io_encoding',
39             );
40             has lang => (
41             is => 'rw',
42             isa => sub {
43             my %valid = map { ($_ => 1) } qw(en ru th);
44             croak "Empty language value" unless $_[0];
45             croak "Unsupported language: $_[0]" unless exists $valid{$_[0]};
46             },
47             predicate => 'has_lang',
48             );
49             has debug => (
50             is => 'rw',
51             predicate => 'has_debug',
52             );
53              
54             has namespace => (
55             is => 'ro',
56             default => sub { '' },
57             );
58              
59             has endpoint => (
60             is => 'ro',
61             default => sub { $ENV{REGRU_API_ENDPOINT} || 'https://api.reg.ru/api/regru2' },
62             );
63              
64             my $class = shift;
65              
66 21     21   1684 my $meta = $class->meta;
67              
68 21         126 foreach my $method ( @{ $class->available_methods } ) {
69             $method = lc $method;
70 21         979 $method =~ s/\s/_/g;
  21         81  
71 150         766403  
72 150         285 my $handler = sub {
73             my ($self, @args) = @_;
74             $self->api_request($method => @args);
75 0     0   0 };
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
        0      
76 0         0  
77 150         457 $meta->add_method($method => $handler);
78             }
79 150         574 }
80              
81             my ($self, $method, %params) = @_;
82              
83             my $url = join '' => $self->endpoint,
84 0     0   0 $self->to_namespace(delete $params{namespace}), # compose namespace part
85             ($method ? '/' . $method : '');
86              
87 0 0       0 # protect I/O formats against modifying
88             delete $params{output_format};
89             delete $params{input_format};
90              
91 0         0 my %post_params = (
92 0         0 username => $self->username,
93             password => $self->password,
94 0         0 output_format => 'json',
95             input_format => 'json',
96             );
97              
98             $post_params{lang} = $self->lang if $self->has_lang;
99             $post_params{io_encoding} = $self->io_encoding if $self->has_io_encoding;
100              
101 0 0       0 $self->debug_warn('API request:', $url, "\n", 'with params:', \%params) if $self->debug;
102 0 0       0  
103             my $json = $self->serializer->encode( \%params );
104 0 0       0  
105             my $response = $self->useragent->post(
106 0         0 $url,
107             [ %post_params, input_data => $json ]
108 0         0 );
109              
110             return Regru::API::Response->new( response => $response, debug => $self->debug );
111             }
112              
113 0         0 my ($self, $namespace) = @_;
114              
115             $namespace = $namespace || $self->namespace || undef;
116              
117 4     4   966 return $namespace ? '/' . $namespace : '';
118             }
119 4   50     50  
120             1; # End of Regru::API::Role::Client
121 4 50       24  
122              
123             =pod
124              
125             =encoding UTF-8
126              
127             =head1 NAME
128              
129             Regru::API::Role::Client - something that makes requests to API
130              
131             =head1 VERSION
132              
133             version 0.052
134              
135             =head1 SYNOPSIS
136              
137             # in some namespace package
138             package Regru::API::Dummy;
139              
140             use strict;
141             use warnings;
142             use Moo;
143              
144             with 'Regru::API::Role::Client';
145              
146             has '+namespace' => (
147             default => sub { 'dummy' },
148             );
149              
150             sub available_methods {[qw(foo bar baz)]}
151              
152             __PACKAGE__->namespace_methods;
153             __PACKAGE__->meta->make_immutable;
154              
155             =head1 DESCRIPTION
156              
157             Any class or role that consumes this role will able to execute requests to REG.API v2.
158              
159             =head1 ATTRIBUTES
160              
161             =head2 username
162              
163             Account name of the user to access to L<reg.com|https://www.reg.com> website. Required. Should be passed at instance
164             create time. Although it might be changed at runtime.
165              
166             =head2 password
167              
168             Account password of the user to access to L<reg.com|https://www.reg.com> website or an alternative password for API
169             defined at L<Reseller settings|https://www.reg.com/reseller/details> page. Required. Should be passed at instance create time.
170             Although it might be changed at runtime.
171              
172             =head2 io_encoding
173              
174             Defines encoding that will be used for data exchange between the Service and the Client. At the moment REG.API v2
175             supports the following encodings: C<utf8>, C<cp1251>, C<koi8-r>, C<koi8-u>, C<cp866>. Optional. Default value is B<utf8>.
176              
177             =head2 lang
178              
179             Defines the language which will be used in error messages. At the moment REG.API v2 supports the following languages:
180             C<en> (English), C<ru> (Russian) and C<th> (Thai). Optional. Default value is B<en>.
181              
182             =head2 debug
183              
184             A few messages will be printed to STDERR. Default value is B<0> (suppressed debug activity).
185              
186             =head2 namespace
187              
188             Used internally.
189              
190             =head2 endpoint
191              
192             REG.API v2 endpoint url. There's no needs to change it. Although it might be overridden by setting environment variable:
193              
194             export REGRU_API_ENDPOINT=https://api.example.com
195              
196             Default value is
197              
198             https://api.reg.ru/api/regru2
199              
200             =head1 METHODS
201              
202             =head2 namespace_methods
203              
204             Dynamically creates methods-shortcuts in in namespaces (categories) for requests to appropriate REG.API v2 functions.
205              
206             =head2 api_request
207              
208             Performs an API request to REG.API service. Returns a L<Regru::API::Response> object.
209              
210             =head2 to_namespace
211              
212             Used internally.
213              
214             =head1 SEE ALSO
215              
216             L<Regru::API>
217              
218             L<Regru::API::Role::Namespace>
219              
220             L<Regru::API::Role::Serializer>
221              
222             L<Regru::API::Role::UserAgent>
223              
224             L<Regru::API::Role::Loggable>
225              
226             L<Regru::API::Response>
227              
228             =head1 BUGS
229              
230             Please report any bugs or feature requests on the bugtracker website
231             L<https://github.com/regru/regru-api-perl/issues>
232              
233             When submitting a bug or request, please include a test-file or a
234             patch to an existing test-file that illustrates the bug or desired
235             feature.
236              
237             =head1 AUTHORS
238              
239             =over 4
240              
241             =item *
242              
243             Polina Shubina <shubina@reg.ru>
244              
245             =item *
246              
247             Anton Gerasimov <a.gerasimov@reg.ru>
248              
249             =back
250              
251             =head1 COPYRIGHT AND LICENSE
252              
253             This software is copyright (c) 2013 by REG.RU LLC.
254              
255             This is free software; you can redistribute it and/or modify it under
256             the same terms as the Perl 5 programming language system itself.
257              
258             =cut