| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebService::Gyazo::B; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 5 |  |  | 5 |  | 8741 | use strict; | 
|  | 5 |  |  |  |  | 9 |  | 
|  | 5 |  |  |  |  | 142 |  | 
| 4 | 5 |  |  | 5 |  | 25 | use warnings; | 
|  | 5 |  |  |  |  | 8 |  | 
|  | 5 |  |  |  |  | 231 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 5 |  |  | 5 |  | 2262 | use WebService::Gyazo::B::Image; | 
|  | 5 |  |  |  |  | 9 |  | 
|  | 5 |  |  |  |  | 195 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 5 |  |  | 5 |  | 4664 | use LWP::UserAgent; | 
|  | 5 |  |  |  |  | 362346 |  | 
|  | 5 |  |  |  |  | 171 |  | 
| 9 | 5 |  |  | 5 |  | 6003 | use LWP::Protocol::socks; | 
|  | 5 |  |  |  |  | 649650 |  | 
|  | 5 |  |  |  |  | 176 |  | 
| 10 | 5 |  |  | 5 |  | 3718 | use HTTP::Request::Common; | 
|  | 5 |  |  |  |  | 11915 |  | 
|  | 5 |  |  |  |  | 352 |  | 
| 11 | 5 |  |  | 5 |  | 3496 | use URI::Simple; | 
|  | 5 |  |  |  |  | 6025 |  | 
|  | 5 |  |  |  |  | 3637 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | our $VERSION = '0.0404'; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | my %PROXY_PROTOCOLS = (map { $_ => 1 } qw(http https socks socks4)); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | sub new { | 
| 18 | 4 |  |  | 4 | 1 | 1714 | my ($self, %args) = @_; | 
| 19 | 4 |  |  |  |  | 12 | $self = bless(\%args, $self); | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 4 |  |  |  |  | 15 | return $self; | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | # Get/set error message | 
| 25 |  |  |  |  |  |  | sub error { | 
| 26 | 95 |  | 100 | 95 | 1 | 1263 | return shift->{error} || 'N/A'; | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | sub _error { | 
| 30 | 50 |  |  | 50 |  | 292 | my ( $self, $error_str ) = @_; | 
| 31 | 50 | 50 |  |  |  | 107 | if ( $error_str ) { | 
| 32 | 50 |  |  |  |  | 113 | $self->{error} = $error_str; | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 50 |  |  |  |  | 83 | return $self->{error}; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub isError { | 
| 39 | 2 | 100 |  | 2 | 1 | 11 | return shift->{error} ? 1 : 0; | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | # Set proxy | 
| 43 |  |  |  |  |  |  | sub setProxy { | 
| 44 | 88 |  |  | 88 | 1 | 37648 | my ($self, $proxyStr) = @_; | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 88 | 50 |  |  |  | 198 | if ( !$proxyStr ) { | 
| 47 | 0 |  |  |  |  | 0 | $self->_error('Undefined proxy value!'); | 
| 48 | 0 |  |  |  |  | 0 | return 0; | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | # If $proxyStr was passed, get protocol, ip and port from it | 
| 52 | 88 |  |  |  |  | 253 | my $proxyUrl = URI::Simple->new($proxyStr); | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | # Check if we have protocol and host/ip from proxy string | 
| 55 | 88 | 50 | 33 |  |  | 7148 | if ( !$proxyUrl->protocol || !$proxyUrl->host ) { | 
| 56 | 0 |  |  |  |  | 0 | $self->_error('Wrong proxy protocol or hostname!'); | 
| 57 | 0 |  |  |  |  | 0 | return 0; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | # Check if protocol is correct | 
| 61 | 88 | 100 |  |  |  | 1505 | if ( ! exists($PROXY_PROTOCOLS{$proxyUrl->protocol})) { | 
| 62 | 36 |  |  |  |  | 314 | $self->_error('Wrong protocol type: ' . $proxyUrl->protocol); | 
| 63 | 36 |  |  |  |  | 225 | return 0; | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | # Check if port is correct | 
| 67 | 52 | 100 | 66 |  |  | 465 | if ( $proxyUrl->port && $proxyUrl->port > 65535 ) { | 
| 68 | 12 |  |  |  |  | 234 | $self->_error('Wrong proxy port!'); | 
| 69 | 12 |  |  |  |  | 78 | return 0; | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 40 |  | 50 |  |  | 705 | $self->{proxy} = sprintf("%s://%s:%s", $proxyUrl->protocol, $proxyUrl->host, $proxyUrl->port || 80); | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 40 |  |  |  |  | 968 | return 1; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | # Assign ID | 
| 78 |  |  |  |  |  |  | sub setId { | 
| 79 | 5 |  |  | 5 | 1 | 1872 | my ($self, $id) = @_; | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | # Check the length of ID | 
| 82 | 5 | 100 | 33 |  |  | 32 | if ( ! defined $id || $id !~ m#^\w{1,14}$# ) { | 
| 83 | 2 |  |  |  |  | 6 | $self->_error('Wrong id syntax!'); | 
| 84 | 2 |  |  |  |  | 8 | return 0; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 3 |  |  |  |  | 6 | $self->{id} = $id; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 3 |  |  |  |  | 13 | return 1; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | # Upload file | 
| 93 |  |  |  |  |  |  | sub uploadFile { | 
| 94 | 1 |  |  | 1 | 1 | 346 | my ($self, $filename) = @_; | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | # Assign ID unless already assigned | 
| 97 | 1 |  | 33 |  |  | 12 | $self->{id} ||= time(); | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | # Check if file path was passed | 
| 100 | 1 | 50 |  |  |  | 3 | if ( ! defined $filename ) { | 
| 101 | 0 |  |  |  |  | 0 | $self->_error('File parameter was not specified or is undef!'); | 
| 102 | 0 |  |  |  |  | 0 | return 0; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | # Check if it is a file | 
| 106 | 1 | 50 |  |  |  | 20 | if ( ! -f $filename ) { | 
| 107 | 0 |  |  |  |  | 0 | $self->_error('File parameter to uploadFile() was not found!'); | 
| 108 | 0 |  |  |  |  | 0 | return 0; | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | # Check if file is readable | 
| 112 | 1 | 50 |  |  |  | 12 | if ( ! -r $filename) { | 
| 113 | 0 |  |  |  |  | 0 | $self->error('The file parameter to uploadFile() is not readable!'); | 
| 114 | 0 |  |  |  |  | 0 | return 0; | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | # Create user agent object | 
| 118 | 1 | 50 |  |  |  | 11 | $self->{ua} = LWP::UserAgent->new('agent' => 'Gyazo/'.$VERSION) unless (defined $self->{ua}); | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | # Assign proxy if it was passed | 
| 121 | 1 | 50 |  |  |  | 15883 | $self->{ua}->proxy(['http'], $self->{proxy}.'/') if ($self->{proxy}); | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | # Create object for POST-request | 
| 124 |  |  |  |  |  |  | my $req = POST('https://gyazo.com/upload.cgi', | 
| 125 |  |  |  |  |  |  | 'Content_Type' => 'form-data', | 
| 126 |  |  |  |  |  |  | 'Content' => [ | 
| 127 |  |  |  |  |  |  | 'id' => $self->{id}, | 
| 128 | 1 |  |  |  |  | 15 | 'imagedata' => [$filename], | 
| 129 |  |  |  |  |  |  | ] | 
| 130 |  |  |  |  |  |  | ); | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | # Send POST-request and check the response | 
| 133 | 1 |  |  |  |  | 43930 | my $res = $self->{ua}->request($req); | 
| 134 | 1 | 50 |  |  |  | 632726 | if ( my ($id) = ($res->content) =~ m#https://gyazo.com/(\w+)#is ) { | 
| 135 | 1 |  |  |  |  | 30 | return WebService::Gyazo::B::Image->new(id => $id); | 
| 136 |  |  |  |  |  |  | } else { | 
| 137 | 0 |  |  |  |  |  | $self->_error("Cannot parsed URL in the:\n".$res->as_string."\n"); | 
| 138 | 0 |  |  |  |  |  | return 0; | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  | } | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | 1; | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | __END__ |