File Coverage

blib/lib/WWW/Hashbang/Pastebin/Client.pm
Criterion Covered Total %
statement 19 45 42.2
branch 3 20 15.0
condition 0 3 0.0
subroutine 5 9 55.5
pod 5 5 100.0
total 32 82 39.0


line stmt bran cond sub pod time code
1             package WWW::Hashbang::Pastebin::Client;
2 3     3   73934 use strict;
  3         8  
  3         106  
3 3     3   18 use warnings;
  3         6  
  3         142  
4             # ABSTRACT: a client library for WWW::Hashbang::Pastebin websites
5             our $VERSION = '0.003'; # VERSION
6              
7 3     3   30756 use HTTP::Tiny;
  3         240345  
  3         155  
8 3     3   36 use Carp;
  3         7  
  3         1860  
9              
10              
11             sub new {
12 1     1 1 82 my $class = shift;
13 1         5 my %args = @_;
14 1 50       4 croak 'No pastebin URL provided' unless $args{url};
15 1 50       11 croak 'Pastebin must be an absolute URL' unless $args{url} =~ m{^http};
16              
17 1         4 my $self = { url => $args{url} };
18 1 50       34 $self->{ua} = HTTP::Tiny->new(
19             agent => __PACKAGE__ . '/'
20             . (__PACKAGE__->VERSION ? __PACKAGE__->VERSION : 'dev')
21             . ' (+https://metacpan.org/module/' . __PACKAGE__ . ')'
22             );
23              
24 1         90 return bless $self, $class;
25             }
26              
27              
28             sub paste {
29 0     0 1   my $self = shift;
30 0           my %args = @_;
31              
32 0 0         if ($args{file}) {
33 0           $args{paste} = do {
34 0           local $/;
35 0 0         open my $in, '<', $args{file}
36             or die "Can't open $args{file} for reading: $!";
37             <$in>
38 0           };
39             }
40 0 0         croak 'No paste content given' unless $args{paste};
41              
42 0           my $post_response = $self->{ua}->post_form(
43             $self->{url}, { p => $args{paste} }
44             );
45              
46 0 0 0       return $post_response->{headers}->{'X-Pastebin-URL'} || $post_response->{content}
47             if $post_response->{success};
48              
49 0           die $post_response->{status}, ' ' , $post_response->{reason}, ' ', $post_response->{content};
50             }
51              
52              
53             sub put {
54 0     0 1   my $self = shift;
55 0           $self->paste(@_);
56             }
57              
58              
59             sub get {
60 0     0 1   my $self = shift;
61 0           my $id = shift;
62 0 0         croak 'No paste ID given' unless $id;
63            
64 0 0         $id =~ s{^\Q$self->{url}\E}{} if ($id =~ m/\Q$self->{url}\E/);
65 0           $id =~ s{^/}{};
66 0           $id =~ s{\+$}{};
67 0           chomp $id;
68              
69 0           my $URI = "$self->{url}/$id";
70 0           my $get_response = $self->{ua}->get($URI);
71              
72 0 0         return $get_response->{content} if $get_response->{success};
73              
74 0           die $get_response->{status}, ' ' , $get_response->{reason}, ' ', $get_response->{content};
75             }
76              
77              
78             sub retrieve {
79 0     0 1   my $self = shift;
80 0           $self->get(@_);
81             }
82              
83             1;
84              
85             __END__