line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Stripe::Simple::Data; |
2
|
|
|
|
|
|
|
$Net::Stripe::Simple::Data::VERSION = '0.003'; |
3
|
|
|
|
|
|
|
# ABSTRACT: collection of methods to augment a JSON hash |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
29
|
use v5.10; |
|
3
|
|
|
|
|
8
|
|
7
|
3
|
|
|
3
|
|
23
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
63
|
|
8
|
3
|
|
|
3
|
|
10
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
88
|
|
9
|
|
|
|
|
|
|
|
10
|
3
|
|
|
3
|
|
17
|
use Scalar::Util qw(refaddr); |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
334
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
use overload '""' => sub { |
13
|
6
|
|
|
6
|
|
308
|
my $self = shift; |
14
|
6
|
100
|
|
|
|
21
|
return $self->{id} if exists $self->{id}; |
15
|
5
|
|
|
|
|
39
|
return __PACKAGE__ . sprintf '=HASH(0x%x)', refaddr $self; |
16
|
3
|
|
|
3
|
|
13
|
}; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
25
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use overload 'cmp' => sub { |
19
|
3
|
|
|
3
|
|
669
|
my ( $left, $right, $reversed ) = @_; |
20
|
3
|
|
|
|
|
8
|
my $v = "$left" cmp "$right"; |
21
|
3
|
50
|
|
|
|
37
|
return $reversed ? -$v : $v; |
22
|
3
|
|
|
3
|
|
339
|
}; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
37
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# an unknown method is an accessor |
25
|
|
|
|
|
|
|
sub AUTOLOAD { |
26
|
3
|
|
|
3
|
|
167
|
our $AUTOLOAD; |
27
|
3
|
50
|
|
|
|
12
|
my $self = shift or return; |
28
|
3
|
|
|
|
|
21
|
( my $key = $AUTOLOAD ) =~ s{.*::}{}; |
29
|
3
|
|
|
5
|
|
17
|
my $accessor = sub { shift->{$key} }; |
|
5
|
|
|
|
|
130
|
|
30
|
|
|
|
|
|
|
{ |
31
|
3
|
|
|
3
|
|
426
|
no strict 'refs'; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
587
|
|
|
3
|
|
|
|
|
4
|
|
32
|
3
|
|
|
|
|
21
|
*$AUTOLOAD = $accessor; |
33
|
|
|
|
|
|
|
} |
34
|
3
|
|
|
|
|
7
|
unshift @_, $self; |
35
|
3
|
|
|
|
|
11
|
goto &$AUTOLOAD; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
1
|
|
|
1
|
1
|
4
|
sub unbless { _unbless(shift) } |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _unbless { |
42
|
3
|
|
|
3
|
|
4
|
my $v = shift; |
43
|
3
|
|
|
|
|
6
|
my $ref = ref($v); |
44
|
|
|
|
|
|
|
|
45
|
3
|
50
|
|
|
|
14
|
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
|
|
|
|
|
5
|
map { $_ => _unbless( $v->{$_} ) } keys %$v |
|
2
|
|
|
|
|
8
|
|
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} elsif ($ref =~ /^JSON/) { |
52
|
0
|
|
|
|
|
0
|
return "$v"; |
53
|
|
|
|
|
|
|
} else { |
54
|
1
|
|
|
|
|
8
|
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.003 |
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 VERSION |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
version 0.002 |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 METHODS |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head2 $self->unbless |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Returns a copy of the data with all the magic stripped away. JSON objects are |
165
|
|
|
|
|
|
|
converted to their stringified form. The intended use of this is prettier |
166
|
|
|
|
|
|
|
debugging dumps. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 AUTHORS |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=over 4 |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item * |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Grant Street Group <developers@grantstreet.com> |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=item * |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
David F. Houghton <dfhoughton@gmail.com> |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=back |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
This software is copyright (c) 2014 by Grant Street Group. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
187
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head1 AUTHORS |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=over 4 |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=item * |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Grant Street Group <developers@grantstreet.com> |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=item * |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
David F. Houghton <dfhoughton@gmail.com> |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=back |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
This software is copyright (c) 2014 by Grant Street Group. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
208
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=cut |