File Coverage

blib/lib/WebService/Plotly.pm
Criterion Covered Total %
statement 57 60 95.0
branch 8 14 57.1
condition 5 9 55.5
subroutine 15 17 88.2
pod 5 5 100.0
total 90 105 85.7


line stmt bran cond sub pod time code
1 1     1   88172 use strictures;
  1         3  
  1         9  
2              
3             package WebService::Plotly;
4              
5             our $VERSION = '1.133400'; # VERSION
6              
7             # ABSTRACT: access plot.ly programmatically
8              
9             #
10             # This file is part of WebService-Plotly
11             #
12             # This software is Copyright (c) 2013 by Plotly, Inc..
13             #
14             # This is free software, licensed under:
15             #
16             # The MIT (X11) License
17             #
18              
19              
20 1     1   1172 use JSON qw( decode_json encode_json );
  1         19690  
  1         7  
21 1     1   1393 use LWP::UserAgent;
  1         50378  
  1         36  
22 1     1   12 use version 0.77;
  1         27  
  1         9  
23              
24 1     1   6282 use Moo;
  1         24802  
  1         9  
25              
26             has $_ => ( is => 'rw', required => 1 ) for qw( un key );
27             has $_ => ( is => 'rw' ) for qw( fileopt filename );
28             has verbose => ( is => 'rw', default => sub { 1 } );
29              
30 4     4 1 644 sub version { __PACKAGE__->VERSION }
31 3     3   15 sub _platform { "Perl" }
32              
33             sub signup {
34 1     1 1 87 my ( $class, $un, $email ) = @_;
35 1         4 my $payload = { version => $class->version, un => $un, email => $email, platform => $class->_platform };
36 1         43 return $class->new( un => undef, key => undef )->_json_from_post( 'https://plot.ly/apimkacct', $payload );
37             }
38              
39 1     1 1 2033 sub plot { shift->_call_wrap( @_ ) }
40 0     0 1 0 sub style { shift->_call_wrap( @_ ) }
41 1     1 1 7379 sub layout { shift->_call_wrap( @_ ) }
42              
43             sub _makecall {
44 2     2   8 my ( $self, $args, $un, $key, $origin, %kwargs ) = @_;
45              
46 2         40 my ( $json_args, $json_kwargs );
47             {
48 1     1   2409 no warnings 'once';
  1         3  
  1         595  
  2         3  
49 2         8 my $required = 2.006;
50             local *PDL::TO_JSON = sub {
51 0 0   0   0 die "PDL version $required required to encode PDL data to JSON"
52             if version::->parse( PDL->VERSION ) < $required;
53 0         0 return shift->unpdl;
54 2         21 };
55 2     4   40 my $convert = sub { JSON->new->utf8->convert_blessed( 1 )->canonical( 1 )->encode( $_[0] ) };
  4         127  
56 2         9 $json_args = $convert->( $args );
57 2         20 $json_kwargs = $convert->( \%kwargs );
58             }
59              
60 2         16 my $payload = {
61             platform => $self->_platform,
62             version => $self->version,
63             args => $json_args,
64             un => $un,
65             key => $key,
66             origin => $origin,
67             kwargs => $json_kwargs,
68             };
69 2         18 my $content = $self->_json_from_post( 'https://plot.ly/clientresp', $payload );
70 1 50       8 $self->filename( $content->{filename} ) if $content->{filename};
71              
72 1         8 return $content;
73             }
74              
75             sub _call_wrap {
76 2     2   6 my $self = shift;
77 2         4 my @args;
78 2         18 push @args, shift @_ while ref $_[0];
79 2         7 my %kwargs = @_;
80              
81 2 50       5 my @login = map { $kwargs{$_} || $self->$_ } qw( un key );
  4         47  
82              
83 2   66     18 $kwargs{filename} ||= $self->filename;
84 2   33     14 $kwargs{fileopt} ||= $self->fileopt;
85              
86 2         14 my $full_func_name = ( caller 1 )[3];
87 2         12 my ( $origin ) = reverse split "::", $full_func_name;
88              
89 2         12 return $self->_makecall( \@args, @login, $origin, %kwargs );
90             }
91              
92             sub _json_from_post {
93 3     3   12 my ( $self, $url, $payload ) = @_;
94              
95 3         36 my $response = LWP::UserAgent->new->post( $url, $payload );
96 3 50       32672 die $response if !$response->is_success;
97              
98 3         57 my $content = decode_json $response->decoded_content;
99              
100 3 100       385 die $content->{error} if $content->{error};
101 2 50       12 print STDERR $content->{warning} if $content->{warning};
102 2 100 66     89 print $content->{message} if $self->verbose and $content->{message};
103              
104 2         21 return $content;
105             }
106              
107             1;
108              
109             __END__