| 1 | package Hubot::Scripts::tweet; | ||||||||||||||||||||||||||||
| 2 | 2 | $Hubot::Scripts::tweet::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 JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use AnyEvent::HTTP::ScopedClient; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | sub load { | ||||||||||||||||||||||||||
|
Subroutine
load: called CC=3 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 10 | 1 | my ( $class, $robot ) = @_; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | my $authorization; | |||||||||||||||||||||||||||
| 13 | $robot->hear( | ||||||||||||||||||||||||||||
| 14 | qr/https?:\/\/(mobile\.)?twitter\.com\/.*?\/status\/([0-9]+)/i, | ||||||||||||||||||||||||||||
| 15 | 1 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=14.2
| |||||||||||||||||||||||||||||
| 16 | 1 | my $msg = shift; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | return unless $authorization; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | $msg->http('https://api.twitter.com/1.1/statuses/show.json') | ||||||||||||||||||||||||||||
| 21 | ->header( { 'Authorization' => $authorization } ) | ||||||||||||||||||||||||||||
| 22 | ->query( 'id' => $msg->match->[1] )->get( | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 24 | 0 | my ( $body, $hdr ) = @_; | |||||||||||||||||||||||||||
| 25 | 0 | ▶ | return if ( !$body || !$hdr->{Status} =~ /^2/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $body or !$hdr->{"Status"} =~ /^2/
MC/DC0%0/2
not $body or !$hdr->{"Status"} =~ /^2/
not $body
!$hdr->{"Status"} =~ /^2/
Truth table0%0/3
not(A) or not(B)
A= not $bodyB= !$hdr->{"Status"} =~ /^2/
| |||||||||||||||||||||||||||||
| 26 | 0 | my $tweet = decode_json($body); | |||||||||||||||||||||||||||
| 27 | 0 | my $text = "$tweet->{user}{screen_name}: $tweet->{text}"; | |||||||||||||||||||||||||||
| 28 | 0 | $msg->send( split /\n/, $text ); | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | 0 | ); | |||||||||||||||||||||||||||
| 31 | 0 | $msg->message->finish; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 1 | ); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | return | ||||||||||||||||||||||||||||
| 36 | unless $ENV{HUBOT_TWITTER_CONSUMER_KEY} | ||||||||||||||||||||||||||||
| 37 | 1 | ▶ | && $ENV{HUBOT_TWITTER_CONSUMER_SECRET}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ENV{"HUBOT_TWITTER_CONSUMER_KEY"} and $ENV{"HUBOT_TWITTER_CONSUMER_SECRET"}
MC/DC0%0/2
$ENV{"HUBOT_TWITTER_CONSUMER_KEY"} and $ENV{"HUBOT_TWITTER_CONSUMER_SECRET"}
$ENV{"HUBOT_TWITTER_CONSUMER_KEY"}
$ENV{"HUBOT_TWITTER_CONSUMER_SECRET"}
Truth table33%1/3
A and B
A= $ENV{"HUBOT_TWITTER_CONSUMER_KEY"}B= $ENV{"HUBOT_TWITTER_CONSUMER_SECRET"}
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | my $client = AnyEvent::HTTP::ScopedClient->new( | ||||||||||||||||||||||||||||
| 40 | 'https://api.twitter.com/oauth2/token', | ||||||||||||||||||||||||||||
| 41 | options => { | ||||||||||||||||||||||||||||
| 42 | auth => uri_escape( $ENV{HUBOT_TWITTER_CONSUMER_KEY} ) . ':' | ||||||||||||||||||||||||||||
| 43 | . uri_escape( $ENV{HUBOT_TWITTER_CONSUMER_SECRET} ) | ||||||||||||||||||||||||||||
| 44 | 0 | } | |||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | $client->post( | ||||||||||||||||||||||||||||
| 48 | { grant_type => 'client_credentials' }, | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 50 | 0 | my ( $body, $hdr ) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | return if !$body; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | my $data = decode_json($body); | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ( $hdr->{Status} =~ /^2/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | my ( $token_type, $access_token ) | ||||||||||||||||||||||||||||
| 57 | 0 | = ( $data->{token_type}, $data->{access_token} ); | |||||||||||||||||||||||||||
| 58 | 0 | $authorization = ucfirst $token_type . " $access_token"; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | else { | ||||||||||||||||||||||||||||
| 61 | 0 | print STDERR __PACKAGE__ . " - $data->{errors}[0]{message}\n"; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 0 | ); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 2 | 1; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =pod | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head1 NAME | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Hubot::Scripts::tweet | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =head1 VERSION | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | version 0.2.8 | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | <tweeturl> - Display tweet content | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =over | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =item HUBOT_TWITTER_CONSUMER_KEY | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =item HUBOT_TWITTER_CONSUMER_SECRET | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =back | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Detect tweet URL and send tweet content | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | Hyungsuk Hong <hshong@perl.kr> | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | This software is copyright (c) 2012 by Hyungsuk Hong. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 108 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =cut | ||||||||||||||||||||||||||||