File Coverage

blib/lib/Mojolicious/Command/nopaste/Service/gist.pm
Criterion Covered Total %
statement 15 39 38.4
branch 0 14 0.0
condition n/a
subroutine 5 6 83.3
pod 0 1 0.0
total 20 60 33.3


line stmt bran cond sub pod time code
1             package Mojolicious::Command::nopaste::Service::gist;
2 1     1   619 use Mojo::Base 'Mojolicious::Command::nopaste::Service';
  1         2  
  1         4  
3 1     1   106 use Mojo::Path;
  1         1  
  1         5  
4 1     1   410 use File::HomeDir;
  1         3768  
  1         41  
5 1     1   4 use File::Spec ();
  1         1  
  1         11  
6 1     1   3 use Mojo::URL;
  1         2  
  1         5  
7              
8             has description => "Post to gist.github.com\n";
9             has token => sub {
10             my $self = shift;
11             return $ENV{GIST_TOKEN} if $ENV{GIST_TOKEN};
12             my $home = File::HomeDir->my_home;
13             my $path = File::Spec->catfile($home, '.gist_token');
14             return undef unless -e $path;
15             my $token = $self->slurp($path);
16             chomp $token;
17             return $token || undef;
18             };
19              
20             my $token_usage = <<'TOKEN';
21             A token is required for posting to gist.
22              
23             See how to create one at:
24             https://help.github.com/articles/creating-an-access-token-for-command-line-use
25             To specify your token one the command line, set the --token (-t) command line
26             switch to a path to your token or the token value itself.
27             Alternatively you can specify the token using the GIST_TOKEN env variable, or
28             by creating a file called .gist_token in your home directory.
29              
30             TOKEN
31              
32             has service_usage => $token_usage;
33              
34             sub paste {
35 0     0 0   my $self = shift;
36 0 0         my $token = $self->token or die $token_usage;
37 0 0         my $data = {
38             public => $self->private ? \0 : \1,
39             };
40              
41 0           my $files = $self->files;
42 0 0         if (@$files) {
43 0           $data->{files}{Mojo::Path->new($_)->[-1]}{content} = $self->slurp($_) for @$files;
44             } else {
45 0           $data->{files}{noname}{content} = $self->slurp;
46             }
47              
48 0 0         $data->{description} = $self->desc if $self->desc;
49              
50 0           my $url = Mojo::URL->new('https://api.github.com/gists');
51 0           my $method = 'POST';
52              
53 0 0         if (my $update = $self->update) {
54 0           push @{ $url->path->parts }, $update;
  0            
55 0           $method = 'PATCH';
56             }
57              
58 0           my $ua = $self->ua;
59 0           my $tx = $ua->build_tx(
60             $method => $url,
61             {
62             Accept => 'application/vnd.github.v3+json',
63             Authorization => "token $token",
64             'User-Agent' => 'Mojolicious-Command-nopaste (author: jberger)',
65             },
66             json => $data,
67             );
68 0           $ua->start($tx);
69              
70 0 0         if (my $error = $tx->error) {
71 0 0         if ($error->{code}) {
72 0           say "Request failed, code $error->{code}: $error->{message}";
73 0           say $tx->res->body;
74             } else {
75 0           say "Connection error: $error->{message}";
76             }
77 0           exit 1;
78             }
79              
80 0           return $tx->res->json->{html_url};
81             }
82              
83             1;
84