root/lang/perl/Google-Chart/branches/moose/lib/Google/Chart.pm @ 16118

Revision 16118, 3.7 kB (checked in by daisuke, 6 years ago)

reuse user agent

  • Property svn:keywords set to Id
Line 
1# $Id$
2
3package Google::Chart;
4use Moose;
5use Google::Chart::Axis;
6use Google::Chart::Data;
7use Google::Chart::Size;
8use Google::Chart::Type;
9use Google::Chart::Types;
10use LWP::UserAgent;
11use URI;
12use overload
13    '""' => \&as_uri,
14    fallback => 1,
15;
16our $USER_AGENT;
17
18use constant BASE_URI => URI->new("http://chart.apis.google.com/chart");
19
20our $VERSION   = '0.00001';
21our $AUTHORITY = 'cpan:DMAKI';
22
23has 'type' => (
24    is       => 'rw',
25    does     => 'Google::Chart::Type',
26    coerce   => 1,
27    required => 1,
28);
29
30has 'data' => (
31    is       => 'rw',
32    does     => 'Google::Chart::Data',
33    coerce   => 1,
34    required => 1,
35);
36
37has 'size' => (
38    is       => 'rw',
39    isa      => 'Google::Chart::Size',
40    coerce   => 1,
41    required => 1,
42    lazy     => 1,
43    default  => sub { Google::Chart::Size->new( width => 400, height => 300 ) },
44);
45
46has 'marker' => (
47    is       => 'rw',
48    isa      => 'Google::Chart::Marker',
49    coerce   => 1,
50);
51
52has 'axis' => (
53    is       => 'rw',
54    isa      => 'Google::Chart::Axis',
55    coerce   => 1,
56);
57
58has 'ua' => (
59    is       => 'rw',
60    isa      => 'LWP::UserAgent',
61    required => 1,
62    lazy     => 1,
63    default  => sub { $USER_AGENT ||= LWP::UserAgent->new() }
64);
65
66__PACKAGE__->meta->make_immutable;
67
68no Moose;
69
70sub as_uri {
71    my $self = shift;
72
73    my %query;
74    foreach my $c qw( type data size marker axis ) {
75        my $component = $self->$c;
76        next unless $component;
77        my @params = $component->as_query;
78        while (@params) {
79            my ($name, $value) = splice(@params, 0, 2);
80            next unless length $value;
81            $query{$name} = $value;
82        }
83    }
84
85    # If in case you want to change this for debugging or whatever...
86    my $uri = $ENV{GOOGLE_CHART_URI} ?
87        URI->new($ENV{GOOGLE_CHART_URI}) :
88        BASE_URI->clone;
89    $uri->query_form( %query );
90    return $uri;
91}
92
93sub render {
94    my $self = shift;
95    my $response = $self->ua->get($self->as_uri);
96
97    if ($response->is_success) {
98        return $response->content;
99    } else {
100        die $response->status_line;
101    }
102}
103
104sub render_to_file {
105    my ($self, $filename) = @_;
106
107    open my $fh, '>', $filename or die "can't open $filename for writing: $!\n";
108    print $fh $self->render;
109    close $fh or die "can't close $filename: $!\n";
110}
111
1121;
113
114__END__
115
116=head1 NAME
117
118Google::Chart - Interface to Google Charts API
119
120=head1 SYNOPSIS
121
122  use Google::Chart;
123
124  my $chart = Google::Chart->new(
125    type => "Bar",
126    data => [ 1, 2, 3, 4, 5 ]
127  );
128  print $chart->as_uri, "\n";
129  print $chart->render_to_file( filename => 'filename.png' );
130
131=head1 METHODS
132
133=head2 new(%args)
134
135=over 4
136
137=item type
138
139Specifies the chart type, such as line, bar, pie, etc. If given a string like
140'Bar', it will instantiate an instance of Google::Chart::Type::Bar by
141invoking argument-less constructor.
142
143If you want to pass parameters to the constructor, either pass in an
144already instanstiated object, or pass in a hash, which will be coerced to
145the appropriate object
146
147  my $chart = Google::Chart->new(
148    type => {
149      module => "Bar",
150      args   => {
151        orientation => "horizontal"
152      }
153    }
154  );
155
156=item size
157
158Specifies the chart size. Strings like "400x300", hash references, or already
159instantiated objects can be used:
160
161  my $chart = Google::Chart->new(
162    size => "400x300",
163  );
164
165  my $chart = Google::Chart->new(
166    size => {
167      width => 400,
168      height => 300
169    }
170  );
171
172=back
173
174=head2 as_uri()
175
176Returns the URI that represents the chart object.
177
178=head2 render()
179
180Generates the chart image, and returns the contents.
181
182=head2 render_to_file( %args )
183
184Generates the chart, and writes the contents out to the file specified by
185`filename' parameter
186
187=cut
Note: See TracBrowser for help on using the browser.