File Coverage

lib/WebService/Braintree/SubscriptionGateway.pm
Criterion Covered Total %
statement 14 41 34.1
branch 0 4 0.0
condition n/a
subroutine 5 14 35.7
pod 0 7 0.0
total 19 66 28.7


line stmt bran cond sub pod time code
1             package WebService::Braintree::SubscriptionGateway;
2             $WebService::Braintree::SubscriptionGateway::VERSION = '0.94';
3 20     20   276 use 5.010_001;
  20         63  
4 20     20   95 use strictures 1;
  20         100  
  20         602  
5              
6 20     20   1588 use WebService::Braintree::Util qw(to_instance_array validate_id);
  20         36  
  20         1139  
7 20     20   100 use Carp qw(confess);
  20         38  
  20         865  
8              
9 20     20   111 use Moose;
  20         36  
  20         114  
10             with 'WebService::Braintree::Role::MakeRequest';
11              
12             has 'gateway' => (is => 'ro');
13              
14             sub create {
15 0     0 0   my ($self, $params) = @_;
16 0           my $result = $self->_make_request("/subscriptions/", "post", {subscription => $params});
17 0           return $result;
18             }
19              
20             sub find {
21 0     0 0   my ($self, $id) = @_;
22 0 0         confess "NotFoundError" unless validate_id($id);
23 0           my $result = $self->_make_request("/subscriptions/$id", "get", undef)->subscription;
24             }
25              
26             sub update {
27 0     0 0   my ($self, $id, $params) = @_;
28 0           my $result = $self->_make_request("/subscriptions/$id", "put", {subscription => $params});
29             }
30              
31             sub cancel {
32 0     0 0   my ($self, $id) = @_;
33 0           my $result = $self->_make_request("/subscriptions/$id/cancel", "put", undef);
34             }
35              
36             sub search {
37 0     0 0   my ($self, $block) = @_;
38 0           my $search = WebService::Braintree::SubscriptionSearch->new;
39 0           my $params = $block->($search)->to_hash;
40 0           my $response = $self->gateway->http->post("/subscriptions/advanced_search_ids", {search => $params});
41             return WebService::Braintree::ResourceCollection->new()->init($response, sub {
42 0     0     $self->fetch_subscriptions($search, shift);
43 0           });
44             }
45              
46             sub all {
47 0     0 0   my $self = shift;
48 0           my $response = $self->gateway->http->post("/subscriptions/advanced_search_ids");
49             return WebService::Braintree::ResourceCollection->new->init($response, sub {
50 0     0     $self->fetch_subscriptions(WebService::Braintree::SubscriptionSearch->new, shift);
51 0           });
52             }
53              
54             sub fetch_subscriptions {
55 0     0 0   my ($self, $search, $ids) = @_;
56 0           $search->ids->in($ids);
57 0 0         return [] if scalar @{$ids} == 0;
  0            
58 0           my $response = $self->gateway->http->post("/subscriptions/advanced_search/", {search => $search->to_hash});
59 0           my $attrs = $response->{'subscriptions'}->{'subscription'};
60 0           return to_instance_array($attrs, "WebService::Braintree::Subscription");
61             }
62              
63             __PACKAGE__->meta->make_immutable;
64              
65             1;
66             __END__