File Coverage

lib/WebService/Gyazo/B.pm
Criterion Covered Total %
statement 59 71 83.1
branch 17 26 65.3
condition 9 16 56.2
subroutine 14 14 100.0
pod 6 6 100.0
total 105 133 78.9


line stmt bran cond sub pod time code
1             package WebService::Gyazo::B;
2              
3 4     4   2312 use strict;
  4         6  
  4         93  
4 4     4   17 use warnings;
  4         4  
  4         84  
5              
6 4     4   1242 use WebService::Gyazo::B::Image;
  4         8  
  4         94  
7              
8 4     4   2116 use LWP::UserAgent;
  4         151231  
  4         128  
9 4     4   3198 use LWP::Protocol::socks;
  4         372685  
  4         140  
10 4     4   1585 use HTTP::Request::Common;
  4         7539  
  4         266  
11 4     4   1404 use URI::Simple;
  4         4034  
  4         2483  
12              
13             our $VERSION = '0.0406';
14              
15             my %PROXY_PROTOCOLS = (map { $_ => 1 } qw(http https socks socks4));
16              
17             sub new {
18 4     4 1 1898 my ($self, %args) = @_;
19 4         9 $self = bless(\%args, $self);
20              
21 4         12 return $self;
22             }
23              
24             # Get/set error message
25             sub error {
26 95   100 95 1 955 return shift->{error} || 'N/A';
27             }
28              
29             sub _error {
30 50     50   289 my ( $self, $error_str ) = @_;
31 50 50       74 if ( $error_str ) {
32 50         71 $self->{error} = $error_str;
33             }
34              
35 50         58 return $self->{error};
36             }
37              
38             sub isError {
39 2 100   2 1 17 return shift->{error} ? 1 : 0;
40             }
41              
42             # Set proxy
43             sub setProxy {
44 88     88 1 37294 my ($self, $proxyStr) = @_;
45              
46 88 50       142 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         207 my $proxyUrl = URI::Simple->new($proxyStr);
53              
54             # Check if we have protocol and host/ip from proxy string
55 88 50 33     6098 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       1395 if ( ! exists($PROXY_PROTOCOLS{$proxyUrl->protocol})) {
62 36         250 $self->_error('Wrong protocol type: ' . $proxyUrl->protocol);
63 36         150 return 0;
64             }
65              
66             # Check if port is correct
67 52 100 66     363 if ( $proxyUrl->port && $proxyUrl->port > 65535 ) {
68 12         172 $self->_error('Wrong proxy port!');
69 12         51 return 0;
70             }
71              
72 40   50     553 $self->{proxy} = sprintf("%s://%s:%s", $proxyUrl->protocol, $proxyUrl->host, $proxyUrl->port || 80);
73              
74 40         869 return 1;
75             }
76              
77             # Assign ID
78             sub setId {
79 5     5 1 2126 my ($self, $id) = @_;
80              
81             # Check the length of ID
82 5 100 66     38 if ( ! defined $id || $id !~ m#^\w{1,14}$# ) {
83 2         5 $self->_error('Wrong id syntax!');
84 2         8 return 0;
85             }
86              
87 3         13 $self->{id} = $id;
88              
89 3         10 return 1;
90             }
91              
92             # Upload file
93             sub uploadFile {
94 1     1 1 288 my ($self, $filename) = @_;
95              
96             # Assign ID unless already assigned
97 1   33     9 $self->{id} ||= time();
98              
99             # Check if file path was passed
100 1 50       9 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       18 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       2397 $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         6 'imagedata' => [$filename],
129             ]
130             );
131              
132             # Send POST-request and check the response
133 1         21745 my $res = $self->{ua}->request($req);
134 1 50       214692 if ( my ($id) = ($res->content) =~ m#https://gyazo.com/(\w+)#is ) {
135 1         29 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__