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