File Coverage

blib/lib/BusyBird/Input/Lingr.pm
Criterion Covered Total %
statement 36 36 100.0
branch 6 10 60.0
condition 3 5 60.0
subroutine 9 9 100.0
pod 2 2 100.0
total 56 62 90.3


line stmt bran cond sub pod time code
1             package BusyBird::Input::Lingr;
2 2     2   63085 use 5.010;
  2         9  
  2         92  
3 2     2   12 use strict;
  2         3  
  2         68  
4 2     2   8 use warnings;
  2         18  
  2         57  
5 2     2   10 use Carp;
  2         5  
  2         156  
6 2     2   1940 use DateTime::Format::ISO8601;
  2         491248  
  2         169  
7 2     2   2167 use BusyBird::DateTime::Format;
  2         2651  
  2         835  
8              
9             our $VERSION = "0.02";
10              
11             my $PARSER = DateTime::Format::ISO8601->new;
12              
13             sub new {
14 2     2 1 20954 my ($class, %args) = @_;
15 2   100     15 my $api_base = $args{api_base} // "http://lingr.com/api";
16 2         32 $api_base =~ qr{^(https?://[^/]+)};
17 2         12 my $url_base = $1;
18 2         10 my $self = bless {
19             url_base => $url_base
20             }, $class;
21 2         9 return $self;
22             }
23              
24             sub convert {
25 7     7 1 24522 my ($self, @messages) = @_;
26 7         22 my @statuses = map { $self->_convert_one($_) } @messages;
  7         817  
27 7 100       1534 return wantarray ? @statuses : $statuses[0];
28             }
29              
30             sub _convert_one {
31 7     7   14 my ($self, $message) = @_;
32 7 50 33     62 croak "message must be a HASH-ref" if !defined($message) || ref($message) ne "HASH";
33 7 50       28 croak "timestamp field was empty" if !defined($message->{timestamp});
34 7 50       22 croak "id field was empty" if !defined($message->{id});
35 7         43 my $time = $PARSER->parse_datetime($message->{timestamp});
36 7 50       3447 croak "Invalid timestamp format" if !defined($time);
37 7         41 my $permalink = sprintf('%s/room/%s/archives/%04d/%02d/%02d#message-%s',
38             $self->{url_base}, $message->{room},
39             $time->year, $time->month, $time->day, $message->{id});
40             return {
41 7         246 id => $permalink,
42             created_at => BusyBird::DateTime::Format->format_datetime($time),
43             user => {
44             profile_image_url => $message->{icon_url},
45             screen_name => $message->{speaker_id},
46             name => $message->{nickname},
47             },
48             text => $message->{text},
49             busybird => {
50             status_permalink => $permalink
51             }
52             };
53             }
54              
55             1;
56             __END__