| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::Stripe::Resource; | 
| 2 |  |  |  |  |  |  | $Net::Stripe::Resource::VERSION = '0.42'; | 
| 3 |  |  |  |  |  |  | # ABSTRACT: represent a Resource object from Stripe | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 2 |  |  | 2 |  | 1288 | use Moose; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 21 |  | 
| 6 | 2 |  |  | 2 |  | 13246 | use Kavorka; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 12 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | has 'boolean_attributes' => (is => 'ro', isa => 'ArrayRef[Str]'); | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | around BUILDARGS => sub { | 
| 11 |  |  |  |  |  |  | my $orig = shift; | 
| 12 |  |  |  |  |  |  | my $class = shift; | 
| 13 |  |  |  |  |  |  | my %args = @_ == 1 ? %{ $_[0] } : @_; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | # Break out the JSON::XS::Boolean values into 1/0 | 
| 16 |  |  |  |  |  |  | for my $field (keys %args) { | 
| 17 |  |  |  |  |  |  | if (ref($args{$field}) =~ /^(JSON::XS::Boolean|JSON::PP::Boolean)$/) { | 
| 18 |  |  |  |  |  |  | $args{$field} = $args{$field} ? 1 : 0; | 
| 19 |  |  |  |  |  |  | } | 
| 20 |  |  |  |  |  |  | } | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | if (my $s = $args{source}) { | 
| 23 |  |  |  |  |  |  | if (ref($s) eq 'HASH' && $s->{object} eq 'source') { | 
| 24 |  |  |  |  |  |  | $args{source} = Net::Stripe::Source->new($s); | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | for my $f (qw/card default_card/) { | 
| 29 |  |  |  |  |  |  | next unless $args{$f}; | 
| 30 |  |  |  |  |  |  | next unless ref($args{$f}) eq 'HASH'; | 
| 31 |  |  |  |  |  |  | $args{$f} = Net::Stripe::Card->new($args{$f}); | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | if (my $s = $args{subscription}) { | 
| 35 |  |  |  |  |  |  | if (ref($s) eq 'HASH') { | 
| 36 |  |  |  |  |  |  | $args{subscription} = Net::Stripe::Subscription->new($s); | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  | if (my $s = $args{coupon}) { | 
| 40 |  |  |  |  |  |  | if (ref($s) eq 'HASH') { | 
| 41 |  |  |  |  |  |  | $args{coupon} = Net::Stripe::Coupon->new($s); | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  | if (my $s = $args{discount}) { | 
| 45 |  |  |  |  |  |  | if (ref($s) eq 'HASH') { | 
| 46 |  |  |  |  |  |  | $args{discount} = Net::Stripe::Discount->new($s); | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  | if (my $p = $args{plan}) { | 
| 50 |  |  |  |  |  |  | if (ref($p) eq 'HASH') { | 
| 51 |  |  |  |  |  |  | $args{plan} = Net::Stripe::Plan->new($p); | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | for my $attr ($class->meta()->get_all_attributes()) { | 
| 56 |  |  |  |  |  |  | next if !($attr->type_constraint && ( | 
| 57 |  |  |  |  |  |  | $attr->type_constraint eq 'Bool' || | 
| 58 |  |  |  |  |  |  | $attr->type_constraint eq 'Maybe[Bool]' || | 
| 59 |  |  |  |  |  |  | $attr->type_constraint eq 'Maybe[Bool|Object]' | 
| 60 |  |  |  |  |  |  | )); | 
| 61 |  |  |  |  |  |  | push @{$args{boolean_attributes}}, $attr->name; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | $class->$orig(%args); | 
| 65 |  |  |  |  |  |  | }; | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | fun form_fields_for_hashref ( | 
| 68 |  |  |  |  |  |  | Str $field_name!, | 
| 69 |  |  |  |  |  |  | HashRef $hashref!, | 
| 70 | 2 | 50 |  | 2 |  | 188311 | ) { | 
|  | 2 | 50 |  | 2 |  | 5 |  | 
|  | 2 | 50 |  | 2 |  | 257 |  | 
|  | 2 | 50 |  | 2 |  | 17 |  | 
|  | 2 | 50 |  |  |  | 5 |  | 
|  | 2 | 50 |  |  |  | 274 |  | 
|  | 2 | 50 |  |  |  | 15 |  | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 515 |  | 
|  | 2 |  |  |  |  | 825 |  | 
|  | 2 |  |  |  |  | 8 |  | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 9 |  | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 8 |  | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 4 |  | 
| 71 | 2 |  |  |  |  | 3 | my @field_values; | 
| 72 | 2 |  |  |  |  | 9 | foreach my $key (sort keys %$hashref) { | 
| 73 | 2 |  |  |  |  | 4 | my $value = $hashref->{$key}; | 
| 74 | 2 |  |  |  |  | 9 | my $nested_field_name = sprintf( '%s[%s]', $field_name, $key ); | 
| 75 | 2 | 100 |  |  |  | 8 | if ( ref( $value ) eq 'HASH' ) { | 
| 76 | 1 |  |  |  |  | 8 | push @field_values, form_fields_for_hashref( $nested_field_name, $value ); | 
| 77 |  |  |  |  |  |  | } else { | 
| 78 | 1 |  |  |  |  | 4 | push @field_values, ( $nested_field_name => $value ); | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  | } | 
| 81 | 2 |  |  |  |  | 8 | return @field_values; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | fun form_fields_for_arrayref ( | 
| 85 |  |  |  |  |  |  | Str $field_name!, | 
| 86 |  |  |  |  |  |  | ArrayRef $arrayref!, | 
| 87 | 2 | 50 |  | 2 |  | 6401 | ) { | 
|  | 2 | 50 |  | 2 |  | 5 |  | 
|  | 2 | 50 |  | 2 |  | 249 |  | 
|  | 2 | 50 |  | 1 |  | 16 |  | 
|  | 2 | 50 |  |  |  | 4 |  | 
|  | 2 | 50 |  |  |  | 281 |  | 
|  | 2 | 50 |  |  |  | 15 |  | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 263 |  | 
|  | 1 |  |  |  |  | 638 |  | 
|  | 1 |  |  |  |  | 6 |  | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 5 |  | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 7 |  | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 3 |  | 
| 88 | 1 |  |  |  |  | 6 | my $nested_field_name = sprintf( '%s[]', $field_name ); | 
| 89 | 1 |  |  |  |  | 4 | return $nested_field_name => $arrayref; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 2 | 50 |  | 2 |  | 3391 | method fields_for($for) { | 
|  | 2 | 50 |  | 26 |  | 6 |  | 
|  | 2 | 50 |  |  |  | 765 |  | 
|  | 26 |  |  |  |  | 89 |  | 
|  | 26 |  |  |  |  | 49 |  | 
|  | 26 |  |  |  |  | 48 |  | 
|  | 26 |  |  |  |  | 37 |  | 
| 93 | 26 | 50 |  |  |  | 89 | return unless $self->can($for); | 
| 94 | 26 |  |  |  |  | 710 | my $thingy = $self->$for; | 
| 95 | 26 | 100 |  |  |  | 72 | return unless defined( $thingy ); | 
| 96 | 2 | 50 | 33 |  |  | 16 | return ($for => $thingy->id) if $for eq 'card' && ref($thingy) eq 'Net::Stripe::Token'; | 
| 97 | 2 | 50 | 33 |  |  | 7 | return ($for => $thingy->id) if $for eq 'source' && ref($thingy) eq 'Net::Stripe::Token'; | 
| 98 | 2 | 50 |  |  |  | 7 | return $thingy->form_fields if ref($thingy) =~ m/^Net::Stripe::/; | 
| 99 | 2 | 50 |  |  |  | 10 | return form_fields_for_hashref( $for, $thingy ) if ref( $thingy ) eq 'HASH'; | 
| 100 | 2 | 50 |  |  |  | 9 | return form_fields_for_arrayref( $for, $thingy ) if ref( $thingy ) eq 'ARRAY'; | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 2 |  |  |  |  | 8 | my $token_id_type = Moose::Util::TypeConstraints::find_type_constraint( 'StripeTokenId' ); | 
| 103 | 2 | 0 | 33 |  |  | 202 | return form_fields_for_hashref( $for, { token => $thingy } ) | 
|  |  |  | 33 |  |  |  |  | 
| 104 |  |  |  |  |  |  | if $self->isa( 'Net::Stripe::PaymentMethod' ) && $for eq 'card' && $token_id_type->check( $thingy ); | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 2 |  |  |  |  | 12 | return ( $for => $self->get_form_field_value( $for ) ); | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 | 2 | 50 |  | 2 |  | 3662 | method form_fields_for(@fields!) { | 
|  | 2 | 50 |  | 2 |  | 7 |  | 
|  | 2 |  |  |  |  | 319 |  | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 24 |  | 
|  | 2 |  |  |  |  | 6 |  | 
| 110 | 2 |  |  |  |  | 6 | return map { $self->fields_for( $_ ) } @fields; | 
|  | 26 |  |  |  |  | 54 |  | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 2 | 50 |  | 2 |  | 4350 | method is_a_boolean(Str $attr!) { | 
|  | 2 | 50 |  | 2 |  | 5 |  | 
|  | 2 | 50 |  | 2 |  | 300 |  | 
|  | 2 | 50 |  |  |  | 16 |  | 
|  | 2 | 50 |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 435 |  | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 5 |  | 
| 114 | 2 | 50 |  |  |  | 4 | my %boolean_attributes = map { $_ => 1 } @{$self->boolean_attributes() || []}; | 
|  | 2 |  |  |  |  | 10 |  | 
|  | 2 |  |  |  |  | 77 |  | 
| 115 | 2 |  |  |  |  | 15 | return exists( $boolean_attributes{$attr} ); | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 2 | 50 |  | 2 |  | 4405 | method get_form_field_value(Str $attr!) { | 
|  | 2 | 50 |  | 2 |  | 6 |  | 
|  | 2 | 50 |  | 2 |  | 326 |  | 
|  | 2 | 50 |  |  |  | 16 |  | 
|  | 2 | 50 |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 370 |  | 
|  | 2 |  |  |  |  | 15 |  | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 9 |  | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 4 |  | 
| 119 | 2 |  |  |  |  | 57 | my $value = $self->$attr; | 
| 120 | 2 | 50 |  |  |  | 10 | return $value if ! $self->is_a_boolean( $attr ); | 
| 121 | 0 | 0 | 0 |  |  |  | return ( defined( $value ) && $value ) ? 'true' : 'false'; | 
| 122 |  |  |  |  |  |  | } | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | 1; | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | __END__ | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =pod | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | =head1 NAME | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | Net::Stripe::Resource - represent a Resource object from Stripe | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | =head1 VERSION | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | version 0.42 | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | =head1 ATTRIBUTES | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =head2 boolean_attributes | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | Reader: boolean_attributes | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | Type: ArrayRef[Str] | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | =head1 AUTHORS | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | =over 4 | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =item * | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | Luke Closs | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | =item * | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | Rusty Conover | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | =back | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | This software is copyright (c) 2015 by Prime Radiant, Inc., (c) copyright 2014 Lucky Dinosaur LLC. | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 165 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | =cut |