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