File: RequestArgs.pm

package info (click to toggle)
libtwitter-api-perl 1.0006-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 424 kB
  • sloc: perl: 2,868; makefile: 7
file content (342 lines) | stat: -rw-r--r-- 10,697 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package Twitter::API::Role::RequestArgs;
# ABSTRACT: API request method helpers
$Twitter::API::Role::RequestArgs::VERSION = '1.0006';
use 5.14.1;
use warnings;
use Carp;
use Moo::Role;
use Ref::Util qw/is_arrayref is_hashref/;
use namespace::clean;

requires 'request';

#pod =method request_with_id
#pod
#pod Transforms an argument list with a required C<screen_name> or C<user_id>,
#pod optionally passed as a leading, positional argument, a hashref argument.
#pod
#pod If a hashref follows the optional plain scalar, the user_id or screen_name is
#pod added to it. Otherwise a new hashref is created and inserted into C<@_>.
#pod
#pod If the optional plain scalar argument is missing, and there is hashref of
#pod arguments, or if the hashref does not contain the key C<screen_name> or
#pod C<user_id>, request_with_id croaks.
#pod
#pod Examples:
#pod
#pod     $self->request_with_id(get => 'some/endpoint', 'foo');
#pod     # is transformed to:
#pod     $self->request(get => 'some/endpoint', { screen_name => 'foo' });
#pod
#pod     $self->request_with_id(get => 'some/endpoint', 8575429);
#pod     # is transformed to:
#pod     $self->request(get => 'some/endpoint', { user_id => 8675429 });
#pod
#pod     $self->request_with_id(get => 'some/endpoint', {
#pod         screen_name => 'semifor',
#pod     });
#pod     # is transformed to:
#pod     $self->request(get => 'some/endpoint', { screen_name => 'semifor' });
#pod
#pod     $self->request_with_id(get => 'some/endpoint', {
#pod         foo => 'bar',
#pod     }); ### croaks ###
#pod
#pod =cut

# if there is a positional arg, it's an :ID (screen_name or user_id)
sub request_with_id {
    splice @_, 1, 0, [];
    push @{$_[1]}, ':ID' if @_ > 4 && !is_hashref($_[4]);
    goto $_[0]->can('request_with_pos_args');
}

#pod =method request_with_pos_args
#pod
#pod Transforms a list of required arguments, optionally provided positionally in a
#pod determined order, into a hashref of named arguments. If a hashref follows the
#pod positional arguments, the named arguments are added to it. Otherwise, a new
#pod hashref in inserted into C<@_>.
#pod
#pod Zero or more of the required arguments may be provided positionally, as long as
#pod the appear in the specified order. I any of the required arguments are not
#pod provided positionally, they must be provided in the hashref or
#pod request_with_pos_args croaks.
#pod
#pod The positional name C<:ID> is treated specially. It is transformed to
#pod C<user_id> if the value it represents contains only digits. Otherwise, it is
#pod transformed to C<screen_name>.
#pod
#pod Examples:
#pod
#pod     $self->request_with_pos_args(
#pod         [ 'id', 'name' ], get => 'some/endpoint',
#pod         '007', 'Bond'
#pod     );
#pod     # is transformed to:
#pod     $self->request(get => 'some/endpoint', {
#pod         id   => '007',
#pod         name => 'Bond',
#pod     });
#pod
#pod     $self->request_with_pos_args(
#pod         [ 'id', 'name' ], get => 'some/endpoint',
#pod         '007', { name => 'Bond' }
#pod     );
#pod     # is also transformed to:
#pod     $self->request(get => 'some/endpoint', {
#pod         id   => '007',
#pod         name => 'Bond',
#pod     });
#pod
#pod     $self->request_with_pos_args(
#pod         [ ':ID', 'status' ], get => 'some/endpoint',
#pod         'alice', 'down the rabbit hole'
#pod     );
#pod     # is transformed to:
#pod     $self->request(get => 'some/endpoint', {
#pod         sreen_name => 'alice',
#pod         status     => 'down the rabbit hole',
#pod     });
#pod
#pod =cut

sub request_with_pos_args {
    my $self = shift;

    $self->request($self->normalize_pos_args(@_));
}

#pod =method normalize_pos_args
#pod
#pod Helper method for C<request_with_pos_args>. Takes the same arguments described in
#pod C<request_with_pos_args> above, and returns a list of arguments ready for a
#pod call to C<request>.
#pod
#pod Individual methods in L<Twitter::API::Trait::ApiMethods> use
#pod C<normalize_pos_args> if they need to do further processing on the args hashref
#pod before calling C<request>.
#pod
#pod =cut

sub normalize_pos_args {
    my $self        = shift;
    my @pos_names   = shift;
    my $http_method = shift;
    my $path        = shift;
    my %args;

    # names can be a single value or an arrayref
    @pos_names = @{ $pos_names[0] } if is_arrayref($pos_names[0]);

    # gather positional arguments and name them
    while ( @pos_names ) {
        last if @_ == 0 || is_hashref($_[0]);
        $args{shift @pos_names} = shift;
    }

    # get the optional, following args hashref and expand it
    my %args_hash; %args_hash = %{ shift() } if is_hashref($_[0]);

    # extract any required args if we still have names
    while ( my $name = shift @pos_names ) {
        if ( $name eq ':ID' ) {
            $name = exists $args_hash{screen_name} ? 'screen_name' : 'user_id';
            croak 'missing required screen_name or user_id'
                unless exists $args_hash{$name};
        }
        croak "missing required '$name' arg" unless exists $args_hash{$name};
        $args{$name} = delete $args_hash{$name};
    }

    # name the :ID value (if any) based on its value
    if ( my $id = delete $args{':ID'} ) {
        $args{$id =~/\D/ ? 'screen_name' : 'user_id'} = $id;
    }

    # merge in the remaining optional values
    for my $name ( keys %args_hash ) {
        croak "'$name' specified in both positional and named args"
            if exists $args{$name};
        $args{$name} = $args_hash{$name};
    }

    return ($http_method, $path, \%args, @_);
}

#pod =method flatten_list_args([ $key | \@keys ], \%args)
#pod
#pod Some Twitter API arguments take a list of values as a string of comma separated
#pod items. To allow callers to pass an array reference of items instead, this
#pod method is used to flatten array references to strings. The key or keys identify
#pod which values to flatten in the C<\%args> hash reference, if they exist.
#pod
#pod =cut

sub flatten_list_args {
    my ( $self, $keys, $args ) = @_;

    for my $key ( is_arrayref($keys) ? @$keys : $keys ) {
        if ( my $value = $args->{$key} ) {
            $args->{$key} = join ',' => @$value if is_arrayref($value);
        }
    }
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Twitter::API::Role::RequestArgs - API request method helpers

=head1 VERSION

version 1.0006

=head1 SYNOPSIS

    package MyApiMethods;
    use Moo::Role;

    sub timeline {
        shift->request_with_id(get => 'statuses/user_timeline, @_);
    }

Then, in your application code:

    use Twitter::API;

    my $client = Twitter::API->new_with_traits(
        traits => '+MyApiMethods',
        %othe_new_options,
    );

    my $statuses = $client->timeline('semifor');

    # equivalent to:
    my $statuses = $client->get('statuses/user_timeline', {
        screen_name => 'semifor',
    });

=head1 DESCRIPTION

Helper methods for implementers of custom traits for creating concise Twitter
API methods. Used in L<Twitter::API::Trait::ApiMethods>.

=head1 METHODS

=head2 request_with_id

Transforms an argument list with a required C<screen_name> or C<user_id>,
optionally passed as a leading, positional argument, a hashref argument.

If a hashref follows the optional plain scalar, the user_id or screen_name is
added to it. Otherwise a new hashref is created and inserted into C<@_>.

If the optional plain scalar argument is missing, and there is hashref of
arguments, or if the hashref does not contain the key C<screen_name> or
C<user_id>, request_with_id croaks.

Examples:

    $self->request_with_id(get => 'some/endpoint', 'foo');
    # is transformed to:
    $self->request(get => 'some/endpoint', { screen_name => 'foo' });

    $self->request_with_id(get => 'some/endpoint', 8575429);
    # is transformed to:
    $self->request(get => 'some/endpoint', { user_id => 8675429 });

    $self->request_with_id(get => 'some/endpoint', {
        screen_name => 'semifor',
    });
    # is transformed to:
    $self->request(get => 'some/endpoint', { screen_name => 'semifor' });

    $self->request_with_id(get => 'some/endpoint', {
        foo => 'bar',
    }); ### croaks ###

=head2 request_with_pos_args

Transforms a list of required arguments, optionally provided positionally in a
determined order, into a hashref of named arguments. If a hashref follows the
positional arguments, the named arguments are added to it. Otherwise, a new
hashref in inserted into C<@_>.

Zero or more of the required arguments may be provided positionally, as long as
the appear in the specified order. I any of the required arguments are not
provided positionally, they must be provided in the hashref or
request_with_pos_args croaks.

The positional name C<:ID> is treated specially. It is transformed to
C<user_id> if the value it represents contains only digits. Otherwise, it is
transformed to C<screen_name>.

Examples:

    $self->request_with_pos_args(
        [ 'id', 'name' ], get => 'some/endpoint',
        '007', 'Bond'
    );
    # is transformed to:
    $self->request(get => 'some/endpoint', {
        id   => '007',
        name => 'Bond',
    });

    $self->request_with_pos_args(
        [ 'id', 'name' ], get => 'some/endpoint',
        '007', { name => 'Bond' }
    );
    # is also transformed to:
    $self->request(get => 'some/endpoint', {
        id   => '007',
        name => 'Bond',
    });

    $self->request_with_pos_args(
        [ ':ID', 'status' ], get => 'some/endpoint',
        'alice', 'down the rabbit hole'
    );
    # is transformed to:
    $self->request(get => 'some/endpoint', {
        sreen_name => 'alice',
        status     => 'down the rabbit hole',
    });

=head2 normalize_pos_args

Helper method for C<request_with_pos_args>. Takes the same arguments described in
C<request_with_pos_args> above, and returns a list of arguments ready for a
call to C<request>.

Individual methods in L<Twitter::API::Trait::ApiMethods> use
C<normalize_pos_args> if they need to do further processing on the args hashref
before calling C<request>.

=head2 flatten_list_args([ $key | \@keys ], \%args)

Some Twitter API arguments take a list of values as a string of comma separated
items. To allow callers to pass an array reference of items instead, this
method is used to flatten array references to strings. The key or keys identify
which values to flatten in the C<\%args> hash reference, if they exist.

=head1 AUTHOR

Marc Mims <marc@questright.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2015-2021 by Marc Mims.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut