Chart.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« Chart/DataSet.pm ↑ Summary Chart/Data.pm »
1  
2 package Google::Chart;
3 13 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 0 ▶use Google::Chart::Data;
Subroutine BEGIN: called CC=1 SCAR=2.6
5 use Google::Chart::Title;
6 use Google::Chart::Types;
7 use LWP::UserAgent;
8 use namespace::clean -except => qw(meta);
9  
10 our $VERSION = '0.09000_05';
11  
12 has title => (
13 is => 'ro',
14 isa => 'Google::Chart::Title',
15 coerce => 1,
16 predicate => 'has_title',
17 );
18  
19 has type => (
20 init_arg => undef, # should NOT be initialized by callers
21 is => 'ro',
22 isa => 'Str',
23 required => 1,
24 lazy_build => 1,
25 );
26  
27 has google_chart_uri => (
28 is => 'ro',
29 isa => 'URI',
30 lazy_build => 1
31 );
32  
33 has ua => (
34 is => 'ro',
35 isa => 'LWP::UserAgent',
36 lazy_build => 1,
37 );
38  
39 has width => (
40 is => 'rw',
41 isa => 'Int',
42 required => 1
43 );
44  
45 has height => (
46 is => 'rw',
47 isa => 'Int',
48 required => 1
49 );
50  
51 around BUILDARGS => sub {
52 my ($next, $self, @args) = @_;
53 my $args = $next->($self, @args);
54  
55 if (my $size = delete $args->{size}) {
56 my ($width, $height) = split /x/, $size;
57 $args->{width} = $width;
58 $args->{height} = $height;
59 }
60  
61 return $args;
62 };
63  
64 sub _build_google_chart_uri {
65 require URI;
66 return $ENV{GOOGLE_CHART_URI} ?
67 URI->new($ENV{GOOGLE_CHART_URI}) :
68 URI->new("http://chart.apis.google.com/chart");
69 }
70  
71 sub _build_type { confess "Unknown type $_[0] (Did you implement a _build_type() method ?" }
72  
73 sub _build_ua {
74 my $self = shift;
75 my $ua = LWP::UserAgent->new(
76 agent => "perl/Google-Chart-$VERSION",
77 env_proxy => exists $ENV{GOOGLE_CHART_ENV_PROXY} ? $ENV{GOOGLE_CHART_ENV_PROXY} : 1,
78 );
79 return $ua;
80 }
81  
82 sub create {
83 my ($class, $chart_class, @args) = @_;
84  
85 if ($chart_class !~ s/^\+//) {
86 $chart_class = "Google::Chart::Type::$chart_class";
87 }
88  
89 if (! Class::MOP::is_class_loaded($chart_class) ) {
90 Class::MOP::load_class($chart_class);
91 }
92  
93 return $chart_class->new(@args);
94 }
95  
96 sub prepare_query {
97 my $self = shift;
98  
99 my @query = (
100 cht => $self->type,
101 chs => join('x', $self->width, $self->height )
102 );
103  
104 foreach my $element (map { $self->$_() } qw(title)) {
105 next unless defined $element;
106 my @params = $element->as_query( $self );
107 while (@params) {
108 my ($name, $value) = splice(@params, 0, 2);
109 next unless length $value;
110 push @query, ($name => $value);
111 }
112 }
113  
114 return @query;
115 }
116  
117 sub as_uri {
118 my $self = shift;
119  
120 # If in case you want to change this for debugging or whatever...
121 my $uri = $self->google_chart_uri()->clone;
122 my @query = $self->prepare_query();
123  
124 # XXX Be paranoid and don't squash duplicates... unless we should!
125 my %seen;
126 my @final;
127 while (@query) {
128 my ($key, $value) = splice(@query, 0, 2);
129 if ($key =~ /^ch[mf]$/) {
130 if ($seen{$key}) {
131 ${$seen{$key}} = join( '|', ${$seen{$key}}, $value );
132 } else {
133 $seen{$key} = \$value;
134 push @final, ($key => $value);
135 }
136 } else {
137 push @final, ($key => $value);
138 }
139 }
140  
141 $uri->query_form( @final );
142 return $uri;
143 }
144  
145 sub render {
146 my $self = shift;
147 my $response = $self->ua->get($self->as_uri);
148  
149 if ($response->is_success) {
150 return $response->content;
151 } else {
152 die $response->status_line;
153 }
154 }
155  
156 sub render_to_file {
157 # XXX - This is done like this because there was a document-implementation
158 # mismatch. In the future, single argument form should be deprecated
159 my $self = shift;
160 my $filename = (@_ > 1) ? do {
161 my %args = @_;
162 $args{filename};
163 }: $_[0];
164  
165 open my $fh, '>', $filename or die "can't open $filename for writing: $!\n";
166 binmode($fh); # be nice to windows
167 print $fh $self->render;
168 close $fh or die "can't close $filename: $!\n";
169 }
170  
171 __PACKAGE__->meta->make_immutable;
172  
173 1;
174  
175 __END__
« Chart/DataSet.pm ↑ Summary Chart/Data.pm »