line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Stripe::Simple::Data; |
2
|
|
|
|
|
|
|
$Net::Stripe::Simple::Data::VERSION = '0.005'; |
3
|
|
|
|
|
|
|
# ABSTRACT: collection of methods to augment a JSON hash |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
41
|
use v5.10; |
|
3
|
|
|
|
|
10
|
|
7
|
3
|
|
|
3
|
|
18
|
use strict; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
92
|
|
8
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
87
|
|
9
|
|
|
|
|
|
|
|
10
|
3
|
|
|
3
|
|
16
|
use Scalar::Util qw(refaddr); |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
345
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
use overload '""' => sub { |
13
|
332
|
|
|
332
|
|
3721
|
my $self = shift; |
14
|
332
|
100
|
|
|
|
3653
|
return $self->{id} if exists $self->{id}; |
15
|
52
|
|
|
|
|
1207
|
return __PACKAGE__ . sprintf '=HASH(0x%x)', refaddr $self; |
16
|
3
|
|
|
3
|
|
33
|
}; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
46
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use overload 'cmp' => sub { |
19
|
3
|
|
|
3
|
|
1148
|
my ( $left, $right, $reversed ) = @_; |
20
|
3
|
|
|
|
|
11
|
my $v = "$left" cmp "$right"; |
21
|
3
|
50
|
|
|
|
40
|
return $reversed ? -$v : $v; |
22
|
3
|
|
|
3
|
|
396
|
}; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
22
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# an unknown method is an accessor |
25
|
|
|
|
|
|
|
sub AUTOLOAD { |
26
|
25
|
|
|
25
|
|
1715
|
our $AUTOLOAD; |
27
|
25
|
50
|
|
|
|
104
|
my $self = shift or return; |
28
|
25
|
|
|
|
|
235
|
( my $key = $AUTOLOAD ) =~ s{.*::}{}; |
29
|
25
|
|
|
1321
|
|
165
|
my $accessor = sub { shift->{$key} }; |
|
1321
|
|
|
|
|
26055
|
|
30
|
|
|
|
|
|
|
{ |
31
|
3
|
|
|
3
|
|
496
|
no strict 'refs'; |
|
3
|
|
|
|
|
16
|
|
|
3
|
|
|
|
|
735
|
|
|
25
|
|
|
|
|
66
|
|
32
|
25
|
|
|
|
|
296
|
*$AUTOLOAD = $accessor; |
33
|
|
|
|
|
|
|
} |
34
|
25
|
|
|
|
|
86
|
unshift @_, $self; |
35
|
25
|
|
|
|
|
128
|
goto &$AUTOLOAD; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
1
|
|
|
1
|
1
|
5
|
sub unbless { _unbless(shift) } |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _unbless { |
42
|
3
|
|
|
3
|
|
6
|
my $v = shift; |
43
|
3
|
|
|
|
|
6
|
my $ref = ref($v); |
44
|
|
|
|
|
|
|
|
45
|
3
|
50
|
|
|
|
12
|
if ($ref eq 'ARRAY') { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
46
|
0
|
|
|
|
|
0
|
return [ map { _unbless($_) } @$v ]; |
|
0
|
|
|
|
|
0
|
|
47
|
|
|
|
|
|
|
} elsif ($ref eq 'Net::Stripe::Simple::Data') { |
48
|
|
|
|
|
|
|
return { |
49
|
2
|
|
|
|
|
6
|
map { $_ => _unbless( $v->{$_} ) } keys %$v |
|
2
|
|
|
|
|
9
|
|
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} elsif ($ref =~ /^JSON/) { |
52
|
0
|
|
|
|
|
0
|
return "$v"; |
53
|
|
|
|
|
|
|
} else { |
54
|
1
|
|
|
|
|
7
|
return $v; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
1; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
__END__ |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=pod |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=encoding UTF-8 |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 NAME |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Net::Stripe::Simple::Data - collection of methods to augment a JSON hash |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 SYNOPSIS |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my $subscription = $stripe->subscriptions( # API methods give us data objects |
73
|
|
|
|
|
|
|
update => { |
74
|
|
|
|
|
|
|
customer => $customer, # a data object as a parameter value |
75
|
|
|
|
|
|
|
id => $subscription, # and another |
76
|
|
|
|
|
|
|
plan => $spare_plan, # and another |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 DESCRIPTION |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
L<Net::Stripe::Simple::Data> is simply a L<JSON> hash with a little magic added |
83
|
|
|
|
|
|
|
to it. Principally, it will autoload any attribute name into an accessor method. |
84
|
|
|
|
|
|
|
So you can say |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
$data->id |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
instead of |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
$data->{id} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
This magic is applied recursively, so instead of |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
$data->{metadata}{foo} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
you can type |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
$data->metadata->foo |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
This hardly saves any keystrokes but it is arguably easier to read. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
The second bit of magic is that the stringification operator is overloaded |
103
|
|
|
|
|
|
|
so that data objects with an id attribute are stringified as their id rather |
104
|
|
|
|
|
|
|
than as |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Net::Stripe::Simple::Data=HASH(0xfeefaaf00) |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
This is useful because Stripe expects to see lots of different ids in its |
109
|
|
|
|
|
|
|
various methods, so you can type |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
$stripe->subscriptions( |
112
|
|
|
|
|
|
|
update => { |
113
|
|
|
|
|
|
|
customer => $customer, |
114
|
|
|
|
|
|
|
id => $subscription, |
115
|
|
|
|
|
|
|
plan => $spare_plan, |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
); |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
instead of |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
$stripe->subscriptions( |
122
|
|
|
|
|
|
|
update => { |
123
|
|
|
|
|
|
|
customer => $customer->id, |
124
|
|
|
|
|
|
|
id => $subscription->id, |
125
|
|
|
|
|
|
|
plan => $spare_plan->id, |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
); |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
or worse yet |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
$stripe->subscriptions( |
132
|
|
|
|
|
|
|
update => { |
133
|
|
|
|
|
|
|
customer => $customer->{id}, |
134
|
|
|
|
|
|
|
id => $subscription->{id}, |
135
|
|
|
|
|
|
|
plan => $spare_plan->{id}, |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
); |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
The 'cmp' operator is overloaded as well so the stringification works mostly |
140
|
|
|
|
|
|
|
as you expect. I.e., |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
$data eq $string; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
is equivalent to |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
$string eq $data; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 NAME |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Net::Stripe::Simple::Data - collection of methods to augment a JSON hash |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 METHODS |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 $self->unbless |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Returns a copy of the data with all the magic stripped away. JSON objects are |
157
|
|
|
|
|
|
|
converted to their stringified form. The intended use of this is prettier |
158
|
|
|
|
|
|
|
debugging dumps. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 AUTHORS |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=over 4 |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item * |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Grant Street Group <developers@grantstreet.com> |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item * |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
David F. Houghton <dfhoughton@gmail.com> |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=back |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
This software is copyright (c) 2014 by Grant Street Group. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
179
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head1 AUTHORS |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=over 4 |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=item * |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Grant Street Group <developers@grantstreet.com> |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=item * |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
David F. Houghton <dfhoughton@gmail.com> |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=back |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
This software is copyright (c) 2014 by Grant Street Group. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
200
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=cut |