Hubot/Scripts/shorten.pm

filter: Statementstmt 84.0% 63 / 75 Branchbran 60.0% 12 / 20 Conditioncond 50.0% 9 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 1 totaltotal 70.3% 95 / 135 CC 18 SCAR 22.7
CC
18
Cov
74%
__ANON__
16.6
__ANON__
6.0
load
1.0
CRAP 9.7
« Hubot/Brain.pm ↑ Summary Hubot/Scripts/roles.pm »
1 package Hubot::Scripts::shorten;
2 2 $Hubot::Scripts::shorten::VERSION = '0.2.8';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Encode 'decode';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶sub load {
Subroutine load: called CC=1 SCAR=0.4
Pod undocumented
13 1 my ( $class, $robot ) = @_;
14 $robot->hear(
15 qr/(https?:\/\/\S+)/i,
16 3 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=17.9
17 3 my $msg = shift;
18 3 my $bitly = $msg->match->[0];
19 3 ▶ if ( length $bitly > 50
Branch: 1 branch
BranchTrueFalseTotal
if (length $bitly > 50 and $ENV{"HUBOT_BITLY_USERNAME"} and $ENV{"HUBOT_BITLY_API_KEY"}) 0 3 3
Condition50%3/6
length $bitly > 50 and $ENV{"HUBOT_BITLY_USERNAME"} and $ENV{"HUBOT_BITLY_API_KEY"}
!ll&&!rl&&r
300
length $bitly > 50 and $ENV{"HUBOT_BITLY_USERNAME"}
!ll&&!rl&&r
210
MC/DC0%0/3
length $bitly > 50 and $ENV{"HUBOT_BITLY_USERNAME"} and $ENV{"HUBOT_BITLY_API_KEY"}
length $bitly > 50 $ENV{"HUBOT_BITLY_USERNAME"} $ENV{"HUBOT_BITLY_API_KEY"}
Truth table50%2/4
A and B and C
A= length $bitly > 50B= $ENV{"HUBOT_BITLY_USERNAME"}C= $ENV{"HUBOT_BITLY_API_KEY"}
ABCresult
0XX0
10X0
1100
1111
20 && $ENV{HUBOT_BITLY_USERNAME}
21 && $ENV{HUBOT_BITLY_API_KEY} )
22 {
23 0 my $uri = URI->new("http://api.bitly.com/v3/shorten");
24 $uri->query_form_hash(
25 login => $ENV{HUBOT_BITLY_USERNAME},
26 apiKey => $ENV{HUBOT_BITLY_API_KEY},
27 0 longUrl => $bitly,
28 format => 'json'
29 );
30 0 my $ua = LWP::UserAgent->new;
31 0 my $req = HTTP::Request->new( 'GET' => $uri );
32 0 my $res = $ua->request($req);
33 0 ▶ return unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
34 0 my $data = decode_json( $res->content );
35 0 $bitly = $data->{data}{url};
36 }
37  
38 $msg->http( $msg->match->[0] )->header( "User-Agent",
39 "Mozilla/5.0 (X11; Linux x86_64; rv:10.0.7) Gecko/20100101 Firefox/10.0.7 Iceweasel/10.0.7"
40 )->get(
41 3 ▶ sub {
Subroutine __ANON__: called CC=14 SCAR=28.1
42 3 my ( $body, $hdr ) = @_;
43 3 ▶ return if ( !$body || !$hdr->{Status} =~ /^2/ );
Branch: 1 branch
BranchTrueFalseTotal
if not $body or !$hdr->{"Status"} =~ /^2/ 0 3 3
Condition33%1/3
not $body or !$hdr->{"Status"} =~ /^2/
l!l&&r!l&&!r
003
MC/DC0%0/2
not $body or !$hdr->{"Status"} =~ /^2/
not $body !$hdr->{"Status"} =~ /^2/
Truth table33%1/3
not(A) or not(B)
A= not $bodyB= !$hdr->{"Status"} =~ /^2/
ABresult
1X1
011
000
44  
45 ## content-type
46 3 my @ct = split( /\s*,\s*/, $hdr->{'content-type'} );
47 0 ▶ if ( grep {/^image\/.+$/i} @ct || grep { !/text/i } @ct ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {m[^image/.+$]i;} @ct || grep({not /text/i;} @ct)) 0 3 3
Condition33%1/3
@ct || grep({not /text/i;} @ct)
l!l&&r!l&&!r
300
MC/DC0%0/2
@ct || grep({not /text/i;} @ct)
@ct grep({not /text/i;} @ct)
Truth table33%1/3
A || B
A= @ctB= grep({not /text/i;} @ct)
ABresult
1X1
011
000
48 0 return $msg->send("[$ct[0]] - $bitly");
49 }
50  
51 ### charset
52 ### <meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
53 ### [FILTER] - <script type="text/javascript" src="http://news.chosun.com/dhtm/js/gnb_news_2011.js" charset="euc-kr"></script>
54 3 $body =~ s{\r\n}{\n}g;
55 my @charset_lines
56 3 = grep { $_ !~ /script/ } grep {/charset/} split /\n/,
57 $body;
58 3 my $charset;
59 3 ▶ if ( "@{[ @charset_lines ]}"
Branch: 1 branch
BranchTrueFalseTotal
if (join($", @{[@charset_lines];}) =~ /charset=(?:'([^']+?)'|"([^"]+?)"|([a-zA-Z0-9_-]+)\b)/) 2 1 3
60 =~ /charset=(?:'([^']+?)'|"([^"]+?)"|([a-zA-Z0-9_-]+)\b)/
61 )
62 {
63 2 ▶ $charset = lc( $1 || $2 || $3 );
Condition66%4/6
$1 || $2 || $3
l!l&&r!l&&!r
110
$1 or $2
l!l&&r!l&&!r
011
MC/DC0%0/3
$1 || $2 || $3
$1 $2 $3
Truth table50%2/4
A or B || C
A= $1B= $2C= $3
ABCresult
1XX1
01X1
0011
0000
64 }
65  
66 3 ▶ unless ($charset) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($charset) 1 2 3
67 1 for my $ct (@ct) {
68 1 ▶ if ( $ct =~ m/charset\s*=\s*(.*)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct =~ /charset\s*=\s*(.*)$/i) { } 1 0 1
69 1 $charset = $1;
70 }
71 else {
72 0 $charset = 'utf-8';
73 }
74 }
75 }
76  
77 3 ▶ $charset = 'euckr' if $charset =~ m/ksc5601/i;
Branch: 1 branch
BranchTrueFalseTotal
if $charset =~ /ksc5601/i 1 2 3
78 3 eval { $body = decode( $charset, $body ) };
79 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
80 0 return $msg->send("[$@] - $bitly");
81 }
82  
83 3 my ($title) = $body =~ m/<title>(.*?)<\/title>/is;
84 3 ▶ $title = 'no title' unless $title;
Branch: 1 branch
BranchTrueFalseTotal
unless $title 0 3 3
85 3 $title =~ s/\n//g;
86 3 $title =~ s/(^\s+|\s+$)//g;
87  
88 ## unescape html
89 3 $title =~ s/&amp;/&/g;
90 3 $title =~ s/&lt;/</g;
91 3 $title =~ s/&gt;/>/g;
92 3 $title =~ s/&quot;/"/g;
93 3 $title =~ s/&apos;/'/g;
94  
95 3 $msg->send("[$title] - $bitly");
96 }
97 3 );
98 }
99 1 );
100 }
101  
102 2 1;
103  
104 =pod
105  
106 =encoding utf-8
107  
108 =head1 NAME
109  
110 Hubot::Scripts::shorten
111  
112 =head1 VERSION
113  
114 version 0.2.8
115  
116 =head1 SYNOPSIS
117  
118 <url> - Shorten the URL using bit.ly
119  
120 =head1 DESCRIPTION
121  
122 Shorten URLs with bit.ly
123  
124 =head1 CONFIGURATION
125  
126 =over
127  
128 =item HUBOT_BITLY_USERNAME
129  
130 =item HUBOT_BITLY_API_KEY
131  
132 =back
133  
134 =head1 AUTHOR
135  
136 Hyungsuk Hong <hshong@perl.kr>
137  
138 =head1 COPYRIGHT AND LICENSE
139  
140 This software is copyright (c) 2012 by Hyungsuk Hong.
141  
142 This is free software; you can redistribute it and/or modify it under
143 the same terms as the Perl 5 programming language system itself.
144  
145 =cut
« Hubot/Brain.pm ↑ Summary Hubot/Scripts/roles.pm »