File Coverage

blib/lib/Net/Twitter/Lite/WrapResult.pm
Criterion Covered Total %
statement 10 10 100.0
branch n/a
condition 1 3 33.3
subroutine 7 7 100.0
pod 6 6 100.0
total 24 26 92.3


line stmt bran cond sub pod time code
1             package Net::Twitter::Lite::WrapResult;
2             $Net::Twitter::Lite::WrapResult::VERSION = '0.12008';
3 9     9   39 use strict;
  9         19  
  9         2057  
4              
5             =head1 NAME
6              
7             Net::Twitter::Lite::WrapResult - Wrap the HTTP response and Twitter result
8              
9             =head1 VERSION
10              
11             version 0.12008
12              
13             =head1 SYNOPSIS
14              
15             use Net::Twitter::Lite::WithAPIv1_1;
16              
17             my $nt = Net::Twitter::Lite::WithAPIv1_1->new(
18             consumer_key => $consumer_key,
19             consumer_secret => $consumer_secret,
20             access_token => $access_token,
21             access_token_secret => $access_token_secret,
22             wrap_result => 1,
23             );
24              
25             my $r = $nt->verify_credentials;
26              
27             my $http_response = $r->http_response;
28             my $twitter_result = $r->result;
29             my $rate_limit_remaining = $r->rate_limit_remaining;
30              
31             =head1 DESCRIPTION
32              
33             Often, the result of a Twitter API call, inflated from the JSON body of the
34             HTTP response does not contain all the information you need. Twitter includes
35             meta data, such as rate limiting information, in HTTP response headers. This
36             object wraps both the inflated Twitter result and the HTTP response giving the
37             caller full access to all the meta data. It also provides accessors for the
38             rate limit information.
39              
40             =head1 METHODS
41              
42             =over 4
43              
44             =item new($twitter_result, $http_response)
45              
46             Constructs an object wrapping the Twitter result and HTTP response.
47              
48             =cut
49              
50             sub new {
51 17     17 1 21 my ( $class, $twitter_result, $http_response ) = @_;
52              
53 17   33     126 return bless {
54             result => $twitter_result,
55             http_response => $http_response,
56             }, ref $class || $class;
57             }
58              
59             =item result
60              
61             Returns the inflated Twitter API result.
62              
63             =cut
64              
65 17     17 1 51 sub result { shift->{result} }
66              
67             =item http_response
68              
69             Returns the L object for the API call.
70              
71             =cut
72              
73 85     85 1 152 sub http_response { shift->{http_response} }
74              
75             # private method
76             my $limit = sub {
77             my ( $self, $which ) = @_;
78            
79             my $res = $self->http_response;
80             $res->header("X-Rate-Limit-$which") || $res->header("X-FeatureRateLimit-$which");
81             };
82              
83             =item rate_limit
84              
85             Returns the rate limit, per 15 minute window, for the API endpoint called.
86             Returns undef if no suitable rate limit header is available.
87              
88             =cut
89              
90 17     17 1 33 sub rate_limit { shift->$limit('Limit') }
91              
92             =item rate_limit_remaining
93              
94             Returns the calls remaining in the current 15 minute window for the API
95             endpoint called. Returns undef if no suitable header is available.
96              
97             =cut
98              
99 17     17 1 632 sub rate_limit_remaining { shift->$limit('Remaining') }
100              
101             =item rate_limit_reset
102              
103             Returns the unix epoch time time of the next 15 minute window, i.e., when the
104             rate limit will be reset, for the API endpoint called. Returns undef if no
105             suitable header is available.
106              
107             =cut
108              
109 17     17 1 593 sub rate_limit_reset { shift->$limit('Reset') }
110              
111             1;
112              
113             __END__