| 1 | # $Id$ | ||
| 2 | |||
| 3 | package Google::Chart; | ||
| 4 | 1 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Google::Chart::Axis; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Google::Chart::Legend; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Google::Chart::Grid; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Google::Chart::Color; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use Google::Chart::Data; | ||
| 11 | use Google::Chart::Size; | ||
| 12 | use Google::Chart::Type; | ||
| 13 | use Google::Chart::Types; | ||
| 14 | use Google::Chart::Title; | ||
| 15 | use Google::Chart::Margin; | ||
| 16 | use LWP::UserAgent; | ||
| 17 | use URI; | ||
| 18 | use overload | ||
| 19 | '""' => \&as_uri, | ||
| 20 | fallback => 1, | ||
| 21 | ; | ||
| 22 | |||
| 23 | use constant BASE_URI => URI->new("http://chart.apis.google.com/chart"); | ||
| 24 | |||
| 25 | our $VERSION = '0.05014'; | ||
| 26 | our $AUTHORITY = 'cpan:DMAKI'; | ||
| 27 | |||
| 28 | my %COMPONENTS = ( | ||
| 29 | type => { | ||
| 30 | is => 'rw', | ||
| 31 | does => 'Google::Chart::Type', | ||
| 32 | coerce => 1, | ||
| 33 | required => 1, | ||
| 34 | }, | ||
| 35 | data => { | ||
| 36 | is => 'rw', | ||
| 37 | does => 'Google::Chart::Data', | ||
| 38 | coerce => 1, | ||
| 39 | }, | ||
| 40 | color => { | ||
| 41 | is => 'rw', | ||
| 42 | isa => 'Google::Chart::Color', | ||
| 43 | coerce => 1, | ||
| 44 | }, | ||
| 45 | legend => { | ||
| 46 | is => 'rw', | ||
| 47 | does => 'Google::Chart::Legend', | ||
| 48 | coerce => 1, | ||
| 49 | }, | ||
| 50 | grid => { | ||
| 51 | is => 'rw', | ||
| 52 | isa => 'Google::Chart::Grid', | ||
| 53 | coerce => 1, | ||
| 54 | }, | ||
| 55 | size => { | ||
| 56 | is => 'rw', | ||
| 57 | isa => 'Google::Chart::Size', | ||
| 58 | coerce => 1, | ||
| 59 | required => 1, | ||
| 60 | lazy => 1, | ||
| 61 | default => sub { Google::Chart::Size->new( width => 400, height => 300 ) }, | ||
| 62 | }, | ||
| 63 | marker => { | ||
| 64 | is => 'rw', | ||
| 65 | isa => 'Google::Chart::Marker', | ||
| 66 | coerce => 1, | ||
| 67 | }, | ||
| 68 | axis => { | ||
| 69 | is => 'rw', | ||
| 70 | isa => 'Google::Chart::Axis', | ||
| 71 | coerce => 1, | ||
| 72 | }, | ||
| 73 | fill => { | ||
| 74 | is => 'rw', | ||
| 75 | does => 'Google::Chart::Fill', | ||
| 76 | coerce => 1 | ||
| 77 | }, | ||
| 78 | title => { | ||
| 79 | is => 'rw', | ||
| 80 | does => 'Google::Chart::Title', | ||
| 81 | coerce => 1 | ||
| 82 | }, | ||
| 83 | margin => { | ||
| 84 | is => 'rw', | ||
| 85 | does => 'Google::Chart::Margin', | ||
| 86 | coerce => 1 | ||
| 87 | }, | ||
| 88 | ); | ||
| 89 | my @COMPONENTS = keys %COMPONENTS; | ||
| 90 | |||
| 91 | { | ||
| 92 | while (my ($name, $spec) = each %COMPONENTS ) { | ||
| 93 | has $name => %$spec; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | has 'ua' => ( | ||
| 98 | is => 'rw', | ||
| 99 | isa => 'LWP::UserAgent', | ||
| 100 | required => 1, | ||
| 101 | lazy_build => 1, | ||
| 102 | ); | ||
| 103 | |||
| 104 | __PACKAGE__->meta->make_immutable; | ||
| 105 | |||
| 106 | no Moose; | ||
| 107 | |||
| 108 | sub _build_ua { | ||
| 109 | my $self = shift; | ||
| 110 | my $ua = LWP::UserAgent->new( | ||
| 111 | agent => "perl/Google-Chart-$VERSION", | ||
| 112 | env_proxy => exists $ENV{GOOGLE_CHART_ENV_PROXY} ? $ENV{GOOGLE_CHART_ENV_PROXY} : 1, | ||
| 113 | ); | ||
| 114 | return $ua; | ||
| 115 | } | ||
| 116 | |||
| 117 | # XXX | ||
| 118 | # We need a trigger function that gets called whenever a component | ||
| 119 | # is set, so we can validate if the combination of components are | ||
| 120 | # actually feasible. | ||
| 121 | |||
| 122 | sub as_uri { | ||
| 123 | my $self = shift; | ||
| 124 | |||
| 125 | my %query; | ||
| 126 | foreach my $c (@COMPONENTS) { | ||
| 127 | my $component = $self->$c; | ||
| 128 | next unless $component; | ||
| 129 | my @params = $component->as_query; | ||
| 130 | while (@params) { | ||
| 131 | my ($name, $value) = splice(@params, 0, 2); | ||
| 132 | next unless length $value; | ||
| 133 | $query{$name} = $value; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | # If in case you want to change this for debugging or whatever... | ||
| 138 | my $uri = $ENV{GOOGLE_CHART_URI} ? | ||
| 139 | URI->new($ENV{GOOGLE_CHART_URI}) : | ||
| 140 | BASE_URI->clone; | ||
| 141 | $uri->query_form( %query ); | ||
| 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 | 1; | ||
| 172 | |||
| 173 | __END__ | ||