File Coverage

blib/lib/Twitter/API/Context.pm
Criterion Covered Total %
statement 11 15 73.3
branch n/a
condition n/a
subroutine 6 10 60.0
pod 3 8 37.5
total 20 33 60.6


line stmt bran cond sub pod time code
1             package Twitter::API::Context;
2             # ABSTRACT: Encapsulated state for a request/response
3             $Twitter::API::Context::VERSION = '1.0006';
4 14     14   149659 use Moo;
  14         9976  
  14         109  
5 14     14   11563 use namespace::clean;
  14         123099  
  14         82  
6              
7             has [ qw/http_method args headers extra_args/ ] => (
8             is => 'ro',
9             );
10              
11             for my $attr ( qw/url result http_response http_request/ ) {
12             has $attr => (
13             writer => "set_$attr",
14             is => 'ro',
15             );
16             }
17              
18             has options => (
19             is => 'ro',
20             default => sub { {} },
21             );
22              
23 302     302 0 2539 sub get_option { $_[0]->options->{$_[1]} }
24 107     107 0 513 sub has_option { exists $_[0]->options->{$_[1]} }
25 65     65 0 705 sub set_option { $_[0]->options->{$_[1]} = $_[2] }
26 0     0 0 0 sub delete_option { delete $_[0]->options->{$_[1]} }
27              
28             # private method
29             my $limit = sub {
30             my ( $self, $which ) = @_;
31              
32             my $res = $self->http_response;
33             $res->header("X-Rate-Limit-$which");
34             };
35              
36 0     0 1 0 sub rate_limit { shift->$limit('Limit') }
37 0     0 1 0 sub rate_limit_remaining { shift->$limit('Remaining') }
38 0     0 1 0 sub rate_limit_reset { shift->$limit('Reset') }
39              
40             sub set_header {
41 46     46 0 116 my ( $self, $header, $value ) = @_;
42              
43 46         153 $self->headers->{$header} = $value;
44             }
45              
46             1;
47              
48             __END__
49              
50             =pod
51              
52             =encoding UTF-8
53              
54             =head1 NAME
55              
56             Twitter::API::Context - Encapsulated state for a request/response
57              
58             =head1 VERSION
59              
60             version 1.0006
61              
62             =head1 SYNOPSIS
63              
64             my ( $r, $c ) = $client->verify_credentials;
65              
66             say sprintf '%d verify_credentials calls remaining unitl %s',
67             $c->rate_limit_remaining,
68             scalar localtime $c->rate_limit_reset;
69              
70             # output:
71             74 verify_credentials_calls remaining until Sat Dec 3 22:05:41 2016
72              
73             =head1 DESCRIPTION
74              
75             The state for every API call is stored in a context object. It is automatically
76             created when a request is initiated and is returned to the caller as the second
77             value in list context. The context includes the L<HTTP::Request> and
78             L<HTTP::Response> objects, a reference to the API return data, and accessor for
79             rate limit information.
80              
81             A reference to the context is also included in a L<Twitter::API::Error>
82             exception.
83              
84             =head1 METHODS
85              
86             =head2 http_request
87              
88             Returns the L<HTTP::Request> object for the API call.
89              
90             =head2 http_response
91              
92             Returns the L<HTTP::Response> object for the API call.
93              
94             =head2 result
95              
96             Returns the result data for the API call.
97              
98             =head2 rate_limit
99              
100             Every API endpoint has a rate limit. This method returns the rate limit for the
101             endpoint of the API call. See
102             L<https://developer.twitter.com/en/docs/basics/rate-limiting> for details.
103              
104             =head2 rate_limit_remaining
105              
106             Returns the number of API calls remaining for the endpoint in the current rate limit window.
107              
108             =head2 rate_limit_reset
109              
110             Returns the time of the next rate limit window in UTC epoch seconds.
111              
112             =head1 AUTHOR
113              
114             Marc Mims <marc@questright.com>
115              
116             =head1 COPYRIGHT AND LICENSE
117              
118             This software is copyright (c) 2015-2021 by Marc Mims.
119              
120             This is free software; you can redistribute it and/or modify it under
121             the same terms as the Perl 5 programming language system itself.
122              
123             =cut