File Coverage

blib/lib/SilverGoldBull/API.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 SilverGoldBull::API;
2              
3 1     1   24136 use strict;
  1         2  
  1         29  
4 1     1   4 use warnings;
  1         1  
  1         29  
5              
6 1     1   270 use Mouse;
  0            
  0            
7              
8             use Carp qw(croak);
9             use LWP::UserAgent;
10             use HTTP::Request;
11             use HTTP::Headers;
12             use URI;
13             use JSON::XS;
14              
15             use SilverGoldBull::API::Response;
16              
17             use constant {
18             API_URL => 'https://api.silvergoldbull.com/',
19             JSON_CONTENT_TYPE => 'application/json',
20             TIMEOUT => 10,
21             };
22              
23             =head1 NAME
24              
25             SilverGoldBull::API - Perl client for the SilverGoldBull(https://silvergoldbull.com/) web service
26              
27             =head1 VERSION
28              
29             Version 0.01
30              
31             =cut
32              
33             our $VERSION = '0.01';
34              
35             has 'ua' => (
36             is => 'ro',
37             init_arg => undef,
38             isa => 'LWP::UserAgent',
39             default => sub {
40             return LWP::UserAgent->new();
41             }
42             );
43              
44             has 'json' => (
45             is => 'ro',
46             init_arg => undef,
47             isa => 'JSON::XS',
48             default => sub {
49             return JSON::XS->new();
50             }
51             );
52              
53             has 'api_url' => ( is => 'rw', isa => 'Str', default => sub { return API_URL; } );
54             has 'api_key' => ( is => 'rw', isa => 'Maybe[Str]', default => sub { return $ENV{SILVERGOLDBULL_API_KEY}; } );
55             has 'version' => ( is => 'rw', isa => 'Int', default => sub { return 1; } );
56             has 'timeout' => ( is => 'rw', isa => 'Int', default => sub { return TIMEOUT } );
57              
58             sub BUILD {
59             my ($self) = @_;
60              
61             if (!$self->api_key) {
62             croak("API key is missing. Specify 'api_key' parameter or set 'SILVERGOLDBULL_API_KEY' variable environment.");
63             }
64             }
65              
66             sub _build_url {
67             my ($self, @params) = @_;
68             my $version = $self->version;
69             my $url_params = join('/', qq{v$version}, @params);
70              
71             return URI->new_abs($url_params, $self->api_url)->as_string;
72             }
73              
74             sub _request {
75             my ($self, $args) = @_;
76             my %params = (
77             'X-API-KEY' => $self->api_key,
78             %{$args->{params} || {}},
79             );
80            
81             my $head = HTTP::Headers->new(Content_Type => JSON_CONTENT_TYPE);
82             $head->header(%params);
83             my $req = HTTP::Request->new($args->{method},$args->{url},$head);
84             my $response = $self->ua->request($req);
85             my $content = $response->content;
86             my $success = $response->is_success;
87             my $data = undef;
88            
89             if ($response->headers->content_type =~ m/${\JSON_CONTENT_TYPE}/i) {
90             eval {
91             $data = $self->{json}->decode($content);
92             };
93             if ($@) {
94             croak('Internal server error');
95             }
96             }
97             else {
98             $data = $content;
99             }
100            
101             return SilverGoldBull::API::Response->new({ success => $success || 0, data => $data });
102             }
103              
104             =head1 SUBROUTINES/METHODS
105              
106             =head2 get_currency_list
107              
108             This method returns an available currencies.
109             Input: nothing
110             Result: SilverGoldBull::API::Response object
111              
112             =cut
113              
114             sub get_currency_list {
115             my ($self) = @_;
116              
117             return $self->_request({ method => 'GET', url => $self->_build_url('currencies') });
118             }
119              
120             =head2 get_payment_method_list
121              
122             This method returns an available payment methods.
123             Input: nothing
124             Result: SilverGoldBull::API::Response object
125              
126             =cut
127              
128             sub get_payment_method_list {
129             my ($self) = @_;
130             return $self->_request({ method => 'GET', url => $self->_build_url('payments/method') });
131             }
132              
133             =head2 get_shipping_method_list
134              
135             This method returns an available shipping methods.
136             Input: nothing
137             Result: SilverGoldBull::API::Response object
138              
139             =cut
140              
141             sub get_shipping_method_list {
142             my ($self) = @_;
143             return $self->_request({ method => 'GET', url => $self->_build_url('shipping/method') });
144             }
145              
146             =head2 get_product_list
147              
148             This method returns product list.
149             Input: nothing
150             Result: SilverGoldBull::API::Response object
151              
152             =cut
153              
154             sub get_product_list {
155             my ($self) = @_;
156             return $self->_request({ method => 'GET', url => $self->_build_url('products') });
157             }
158              
159             =head2 get_product
160              
161             This method returns detailed information about product by id.
162             Input: nothing
163             Result: SilverGoldBull::API::Response object
164              
165             =cut
166              
167             sub get_product {
168             my ($self, $id) = @_;
169             return $self->_request({ method => 'GET', url => $self->_build_url('products', $id) });
170             }
171              
172             =head2 get_order_list
173              
174             This method returns order list.
175             Input: nothing
176             Result: SilverGoldBull::API::Response object
177              
178             =cut
179              
180             sub get_order_list {
181             my ($self) = @_;
182             return $self->_request({ method => 'GET', url => $self->_build_url('orders') });
183             }
184              
185             =head2 get_order
186              
187             This method returns detailed information about order by id.
188             Input: nothing
189             Result: SilverGoldBull::API::Response object
190              
191             =cut
192              
193             sub get_order {
194             my ($self, $id) = @_;
195             if (!defined $id) {
196             croak('Missing order id');
197             }
198              
199             return $self->_request({ method => 'GET', url => $self->_build_url('orders', $id) });
200             }
201              
202             =head2 create_order
203              
204             This method creates a new order.
205             Input: nothing
206             Result: SilverGoldBull::API::Response object
207              
208             =cut
209              
210             sub create_order {
211             my ($self, $order) = @_;
212             if (!defined $order && (ref($order) ne 'SilverGoldBull::API::Order')) {
213             croak('Missing SilverGoldBull::API::Order object');
214             }
215              
216             return $self->_request({ method => 'POST', url => $self->_build_url('orders/create'), params => $order->to_hashref });
217             }
218              
219             =head2 create_quote
220              
221             This method creates a quote.
222             Input: nothing
223             Result: SilverGoldBull::API::Response object
224              
225             =cut
226              
227             sub create_quote {
228             my ($self, $quote) = @_;
229             if (!defined $quote && (ref($quote) ne 'SilverGoldBull::API::Quote')) {
230             croak('Missing SilverGoldBull::API::Quote object');
231             }
232              
233             return $self->_request({ method => 'POST', url => $self->_build_url('orders/quote'), params => $quote->to_hashref });
234             }
235              
236             =head1 AUTHOR
237              
238             Denis Boyun, C<< >>
239              
240             =head1 BUGS
241              
242             Please report any bugs or feature requests to C, or through
243             the web interface at L. I will be notified, and then you'll
244             automatically be notified of progress on your bug as I make changes.
245              
246              
247              
248              
249             =head1 SUPPORT
250              
251             You can find documentation for this module with the perldoc command.
252              
253             perldoc SilverGoldBull::API
254              
255              
256             You can also look for information at:
257              
258             =over 4
259              
260             =item * RT: CPAN's request tracker (report bugs here)
261              
262             L
263              
264             =item * AnnoCPAN: Annotated CPAN documentation
265              
266             L
267              
268             =item * CPAN Ratings
269              
270             L
271              
272             =item * Search CPAN
273              
274             L
275              
276             =back
277              
278              
279             =head1 ACKNOWLEDGEMENTS
280              
281              
282             =head1 LICENSE AND COPYRIGHT
283              
284             Copyright 2016 Denis Boyun.
285              
286             This program is free software; you can redistribute it and/or modify it
287             under the terms of the the Artistic License (2.0). You may obtain a
288             copy of the full license at:
289              
290             L
291              
292             Any use, modification, and distribution of the Standard or Modified
293             Versions is governed by this Artistic License. By using, modifying or
294             distributing the Package, you accept this license. Do not use, modify,
295             or distribute the Package, if you do not accept this license.
296              
297             If your Modified Version has been derived from a Modified Version made
298             by someone other than you, you are nevertheless required to ensure that
299             your Modified Version complies with the requirements of this license.
300              
301             This license does not grant you the right to use any trademark, service
302             mark, tradename, or logo of the Copyright Holder.
303              
304             This license includes the non-exclusive, worldwide, free-of-charge
305             patent license to make, have made, use, offer to sell, sell, import and
306             otherwise transfer the Package with respect to any patent claims
307             licensable by the Copyright Holder that are necessarily infringed by the
308             Package. If you institute patent litigation (including a cross-claim or
309             counterclaim) against any party alleging that the Package constitutes
310             direct or contributory patent infringement, then this Artistic License
311             to you shall terminate on the date that such litigation is filed.
312              
313             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
314             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
315             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
316             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
317             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
318             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
319             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
320             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
321              
322              
323             =cut
324              
325             1; # End of SilverGoldBull::API