File Coverage

blib/lib/Chart/Plotly/Image/Orca/Client.pm
Criterion Covered Total %
statement 17 30 56.6
branch 0 2 0.0
condition n/a
subroutine 6 7 85.7
pod 1 1 100.0
total 24 40 60.0


line stmt bran cond sub pod time code
1             package Chart::Plotly::Image::Orca::Client;
2              
3             # ABSTRACT: Export static images of Plotly charts using orca server
4              
5 1     1   20 use 5.010;
  1         4  
6 1     1   6 use strict;
  1         4  
  1         21  
7 1     1   5 use warnings;
  1         2  
  1         25  
8              
9 1     1   5 use Path::Tiny;
  1         4  
  1         44  
10 1     1   1332 use LWP::UserAgent;
  1         51454  
  1         38  
11 1     1   10 use utf8;
  1         5  
  1         11  
12              
13             our $VERSION = '0.041'; # VERSION
14              
15             sub save_image {
16 0     0 1   my %params = @_;
17              
18 0           my $plot = $params{plot};
19 0           my $file = path( $params{file} );
20              
21 0           my $ua = LWP::UserAgent->new;
22 0           $ua->agent("Chart::Plotly::Image::Orca::Client/0.1");
23              
24 0           my $req = HTTP::Request->new( POST => $params{server} );
25 0           $req->content_type('application/json');
26 0           $req->content( $plot->to_json_text );
27 0           my $res = $ua->request($req);
28 0 0         if ( $res->is_success ) {
29 0           path($file)->spew_raw( $res->content );
30             } else {
31 0           die "Can't export the plot. Server returned: " . $res->status_line;
32             }
33 0           return;
34             }
35              
36             1;
37              
38             __END__
39              
40             =pod
41              
42             =encoding utf-8
43              
44             =head1 NAME
45              
46             Chart::Plotly::Image::Orca::Client - Export static images of Plotly charts using orca server
47              
48             =head1 VERSION
49              
50             version 0.041
51              
52             =head1 SYNOPSIS
53              
54             #!/usr/bin/env perl
55            
56             use strict;
57             use warnings;
58             use utf8;
59            
60             use Chart::Plotly::Plot;
61             use Chart::Plotly::Trace::Scatter;
62             use Chart::Plotly::Image::Orca::Client;
63            
64             my $plot = Chart::Plotly::Plot->new(traces => [ Chart::Plotly::Trace::Scatter->new( x => [ 1 .. 5 ], y => [ 1 .. 5 ] )]);
65            
66             Chart::Plotly::Image::Orca::Client::save_image(plot => $plot, file => "TestOrca.png", server => "http://[::]:9999");
67              
68             =head1 DESCRIPTION
69              
70             This module generate static images of Plotly charts without a browser using a
71             L<Orca|https://github.com/plotly/orca> server.
72              
73             The server must be up and running before using this module. You can get an Orca server
74             using a docker image or just installing and running Orca yourself. There are some
75             instructions in L<Chart::Plotly::Image::Orca>
76              
77             =head1 FUNCTIONS
78              
79             =head2 save_image
80              
81             save_image(plot => $plot, file => "TestOrca.png", server => "http://[::]:9999");
82              
83             Export L<Chart::Plotly::Plot> as a static image file.
84              
85             =over 4
86              
87             =item plot
88              
89             Object to export
90              
91             =item file
92              
93             Filename (with or without path) to export
94              
95             =item server
96              
97             Url where the server is listening
98              
99             =back
100              
101             =head1 BUGS
102              
103             Please report any bugs or feature requests via github: L<https://github.com/pablrod/p5-Chart-Plotly/issues>
104              
105             =head1 DISCLAIMER
106              
107             This is an unofficial Plotly Perl module. Currently I'm not affiliated in any way with Plotly.
108             But I think plotly.js is a great library and I want to use it with perl.
109              
110             If you like plotly.js please consider supporting them purchasing a pro subscription: L<https://plot.ly/products/cloud/>
111              
112             =head1 SEE ALSO
113              
114             L<Alien::Plotly::Orca>
115              
116             =head1 AUTHOR
117              
118             Pablo Rodríguez González <pablo.rodriguez.gonzalez@gmail.com>
119              
120             =head1 COPYRIGHT AND LICENSE
121              
122             This software is Copyright (c) 2020 by Pablo Rodríguez González.
123              
124             This is free software, licensed under:
125              
126             The MIT (X11) License
127              
128             =cut