File Coverage

lib/WebService/Gyazo/B.pm
Criterion Covered Total %
statement 59 71 83.1
branch 17 26 65.3
condition 8 16 50.0
subroutine 14 14 100.0
pod 6 6 100.0
total 104 133 78.2


line stmt bran cond sub pod time code
1             package WebService::Gyazo::B;
2              
3 5     5   9003 use strict;
  5         10  
  5         131  
4 5     5   24 use warnings;
  5         7  
  5         168  
5              
6 5     5   2266 use WebService::Gyazo::B::Image;
  5         10  
  5         136  
7              
8 5     5   4904 use LWP::UserAgent;
  5         262241  
  5         179  
9 5     5   6139 use LWP::Protocol::socks;
  5         645410  
  5         167  
10 5     5   4050 use HTTP::Request::Common;
  5         12008  
  5         373  
11 5     5   3501 use URI::Simple;
  5         6244  
  5         3739  
12              
13             our $VERSION = '0.0405';
14              
15             my %PROXY_PROTOCOLS = (map { $_ => 1 } qw(http https socks socks4));
16              
17             sub new {
18 4     4 1 1762 my ($self, %args) = @_;
19 4         14 $self = bless(\%args, $self);
20              
21 4         18 return $self;
22             }
23              
24             # Get/set error message
25             sub error {
26 95   100 95 1 1209 return shift->{error} || 'N/A';
27             }
28              
29             sub _error {
30 50     50   355 my ( $self, $error_str ) = @_;
31 50 50       104 if ( $error_str ) {
32 50         96 $self->{error} = $error_str;
33             }
34              
35 50         81 return $self->{error};
36             }
37              
38             sub isError {
39 2 100   2 1 13 return shift->{error} ? 1 : 0;
40             }
41              
42             # Set proxy
43             sub setProxy {
44 88     88 1 34267 my ($self, $proxyStr) = @_;
45              
46 88 50       186 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         249 my $proxyUrl = URI::Simple->new($proxyStr);
53              
54             # Check if we have protocol and host/ip from proxy string
55 88 50 33     7599 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       1694 if ( ! exists($PROXY_PROTOCOLS{$proxyUrl->protocol})) {
62 36         330 $self->_error('Wrong protocol type: ' . $proxyUrl->protocol);
63 36         234 return 0;
64             }
65              
66             # Check if port is correct
67 52 100 66     495 if ( $proxyUrl->port && $proxyUrl->port > 65535 ) {
68 12         217 $self->_error('Wrong proxy port!');
69 12         74 return 0;
70             }
71              
72 40   50     743 $self->{proxy} = sprintf("%s://%s:%s", $proxyUrl->protocol, $proxyUrl->host, $proxyUrl->port || 80);
73              
74 40         1044 return 1;
75             }
76              
77             # Assign ID
78             sub setId {
79 5     5 1 1863 my ($self, $id) = @_;
80              
81             # Check the length of ID
82 5 100 33     30 if ( ! defined $id || $id !~ m#^\w{1,14}$# ) {
83 2         6 $self->_error('Wrong id syntax!');
84 2         9 return 0;
85             }
86              
87 3         9 $self->{id} = $id;
88              
89 3         12 return 1;
90             }
91              
92             # Upload file
93             sub uploadFile {
94 1     1 1 400 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       4 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       25 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       13 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       4266 $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         7 'imagedata' => [$filename],
129             ]
130             );
131              
132             # Send POST-request and check the response
133 1         39206 my $res = $self->{ua}->request($req);
134 1 50       660951 if ( my ($id) = ($res->content) =~ m#https://gyazo.com/(\w+)#is ) {
135 1         28 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__