File Coverage

blib/lib/Twitter/API/Util.pm
Criterion Covered Total %
statement 32 32 100.0
branch 6 6 100.0
condition 3 4 75.0
subroutine 13 13 100.0
pod 4 4 100.0
total 58 59 98.3


line stmt bran cond sub pod time code
1             package Twitter::API::Util;
2             # ABSTRACT: Utilities for working with the Twitter API
3             $Twitter::API::Util::VERSION = '1.0006';
4 1     1   61991 use 5.14.1;
  1         13  
5 1     1   5 use warnings;
  1         1  
  1         23  
6 1     1   4 use Carp qw/croak/;
  1         1  
  1         48  
7 1     1   5 use Scalar::Util qw/blessed/;
  1         2  
  1         47  
8 1     1   436 use Time::Local qw/timegm/;
  1         2093  
  1         55  
9 1     1   6 use Try::Tiny;
  1         2  
  1         41  
10 1     1   441 use namespace::clean;
  1         11882  
  1         6  
11              
12 1         11 use Sub::Exporter::Progressive -setup => {
13             exports => [ qw/
14             is_twitter_api_error
15             timestamp_to_gmtime
16             timestamp_to_localtime
17             timestamp_to_time
18             /],
19 1     1   280 };
  1         1  
20              
21             sub is_twitter_api_error {
22 4 100   4 1 592 blessed($_[0]) && $_[0]->isa('Twitter::API::Error');
23             }
24              
25             my %month;
26             @month{qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/} = 0..11;
27             sub _parse_ts {
28 6   50 6   14 local $_ = shift() // return;
29              
30             # "Wed Jun 06 20:07:10 +0000 2012"
31 6 100       198 my ( $M, $d, $h, $m, $s, $y ) = /
32             ^(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)
33             \ (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)
34             \ (\d\d)\ (\d\d):(\d\d):(\d\d)
35             \ \+0000\ (\d{4})$
36             /x or return;
37 5         29 return ( $s, $m, $h, $d, $month{$M}, $y - 1900 );
38             };
39              
40 2     2 1 1092 sub timestamp_to_gmtime { gmtime timestamp_to_time($_[0]) }
41 2     2 1 1051 sub timestamp_to_localtime { localtime timestamp_to_time($_[0]) }
42             sub timestamp_to_time {
43 7   100 7 1 1968 my $ts = shift // return undef;
44 6 100       12 my @t = _parse_ts($ts) or croak "invalid timestamp: $ts";
45 5         15 timegm @t;
46             }
47              
48             1;
49              
50             __END__
51              
52             =pod
53              
54             =encoding UTF-8
55              
56             =head1 NAME
57              
58             Twitter::API::Util - Utilities for working with the Twitter API
59              
60             =head1 VERSION
61              
62             version 1.0006
63              
64             =head1 SYNOPSIS
65              
66             use Twitter::API::Util ':all';
67              
68             # Given a timestamp in Twitter's text format:
69             my $ts = $status->{created_at}; # "Wed Jun 06 20:07:10 +0000 2012"
70              
71             # Convert it UNIX epoch seconds (a Perl "time" value):
72             my $time = timestamp_to_time($status->{created_at});
73              
74             # Or a Perl localtime:
75             my $utc = timestamp_to_timepiece($status->{created_at});
76              
77             # Or a Perl gmtime:
78             my $utc = timestamp_to_gmtime($status->{created_at});
79              
80             # Check to see if an exception is a Twitter::API::Error
81             if ( is_twitter_api_error($@) ) {
82             warn "Twitter API error: " . $@->twitter_error_text;
83             }
84              
85             =head1 DESCRIPTION
86              
87             Exports helpful utility functions.
88              
89             =head1 METHODS
90              
91             =head2 timestamp_to_gmtime
92              
93             Returns C<gmtime> from a Twitter timestamp string. See L<perlfunc/gmtime-EXPR>
94             for details.
95              
96             =head2 timestamp_to_localtime
97              
98             Returns C<localtime> for a Twitter timestamp string. See
99             L<perlfunc/localtime-EXPR> for details.
100              
101             =head2 timestamp_to_time
102              
103             Returns a UNIX epoch time for a Twitter timestamp string. See L<perlfunc/time>
104             for details.
105              
106             =head2 is_twitter_api_error
107              
108             Returns true if the scalar passed to it is a L<Twitter::API::Error>. Otherwise,
109             it returns false.
110              
111             =head1 AUTHOR
112              
113             Marc Mims <marc@questright.com>
114              
115             =head1 COPYRIGHT AND LICENSE
116              
117             This software is copyright (c) 2015-2021 by Marc Mims.
118              
119             This is free software; you can redistribute it and/or modify it under
120             the same terms as the Perl 5 programming language system itself.
121              
122             =cut