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
|
package Twitter::API::Trait::NormalizeBooleans;
# ABSTRACT: Normalize Booleans
$Twitter::API::Trait::NormalizeBooleans::VERSION = '1.0006';
use 5.14.1;
use Moo::Role;
use namespace::clean;
requires 'preprocess_args';
around preprocess_args => sub {
my ( $next, $self, $c ) = @_;
$self->$next($c);
$self->normalize_bools($c->args);
};
# Twitter usually accepts 1, 't', 'true', or false for booleans, but they
# prefer 'true' or 'false'. In some cases, like include_email, they only accept
# 'true'. So, we normalize these options.
my @normal_bools = qw/
contributor_details display_coordinates exclude_replies hide_media
hide_thread hide_tweet include_email include_entities include_my_tweet
include_rts include_user_entities map omit_script possibly_sensitive
reverse trim_user
/;
# Workaround Twitter bug: any value passed for these options are treated as
# true. The only way to get 'false' is to not pass the skip_user at all. So,
# we strip these boolean args if their values are false.
my @true_only_bools = qw/skip_status skip_user/;
my %is_bool = map { $_ => undef } @normal_bools, @true_only_bools;
my %is_true_only_bool = map { $_ => undef } @true_only_bools;
sub is_bool { exists $is_bool{$_[1]} }
sub is_true_only_bool { exists $is_true_only_bool{$_[1]} }
sub normalize_bools {
my ( $self, $args ) = @_;
# Twitter prefers 'true' or 'false' (requires it in some cases).
for my $k ( keys %$args ) {
next unless $self->is_bool($k);
$args->{$k} = $args->{$k} ? 'true' : 'false';
delete $args->{$k} if $self->is_true_only_bool($k)
&& $args->{$k} eq 'false';
}
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Twitter::API::Trait::NormalizeBooleans - Normalize Booleans
=head1 VERSION
version 1.0006
=head1 SYNOPSIS
use Twitter::API;
my $client = Twitter::API->new_with_traits(
traits => [ qw/ApiMethods NormalizeBooleans/ ],
%other_new_options
);
my ( $r, $c ) = $client->home_timeline({ trim_user => 1 });
say $c->http_request->uri;
# output:
https://api.twitter.com/1.1/statuses/home_timeline.json?trim_user=true
=head1 DESCRIPTION
Twitter has a strange concept of boolean values. Usually, the API accepts C<t>,
C<true>, or C<1> for true. Sometimes it accepts C<f>, C<false>, or C<0> for
false. But then you have strange cases like the C<include_email> parameter used
for authorized applications by the C<verify_credentials> endpoint. It only
accepts C<true>. Worse, for some boolean values, passing C<f>, C<false>, or
C<0> all work as if you passed C<true>. For those values, false means not
including the parameter at all.
So, this trait attempts to normalize booleans by transforming any perl truthy
value to the Twitter API's preference, C<true>. It transforms falsey values to
C<false>. And then it removes false parameters that the API always treats as
true.
You're welcome.
=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
|