| 1 | package Net::Google::Spreadsheets; |
|---|
| 2 | use Moose; |
|---|
| 3 | |
|---|
| 4 | extends 'Net::Google::Spreadsheets::Base'; |
|---|
| 5 | |
|---|
| 6 | use Carp; |
|---|
| 7 | use Net::Google::AuthSub; |
|---|
| 8 | use Net::Google::Spreadsheets::Spreadsheet; |
|---|
| 9 | use LWP::UserAgent; |
|---|
| 10 | use XML::Atom; |
|---|
| 11 | use XML::Atom::Feed; |
|---|
| 12 | use URI; |
|---|
| 13 | use HTTP::Headers; |
|---|
| 14 | |
|---|
| 15 | our $VERSION = '0.01'; |
|---|
| 16 | |
|---|
| 17 | BEGIN { |
|---|
| 18 | $XML::Atom::DefaultVersion = 1; |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | has contents => ( |
|---|
| 22 | is => 'ro', |
|---|
| 23 | default => 'http://spreadsheets.google.com/feeds/spreadsheets/private/full' |
|---|
| 24 | ); |
|---|
| 25 | |
|---|
| 26 | has username => ( isa => 'Str', is => 'ro', required => 1 ); |
|---|
| 27 | has password => ( isa => 'Str', is => 'ro', required => 1 ); |
|---|
| 28 | |
|---|
| 29 | has source => ( |
|---|
| 30 | isa => 'Str', |
|---|
| 31 | is => 'ro', |
|---|
| 32 | required => 1, |
|---|
| 33 | default => sub { __PACKAGE__.'-'.$VERSION }, |
|---|
| 34 | ); |
|---|
| 35 | |
|---|
| 36 | has auth => ( |
|---|
| 37 | isa => 'Str', |
|---|
| 38 | is => 'rw', |
|---|
| 39 | required => 1, |
|---|
| 40 | lazy => 1, |
|---|
| 41 | default => sub { |
|---|
| 42 | my $self = shift; |
|---|
| 43 | my $authsub = Net::Google::AuthSub->new( |
|---|
| 44 | service => 'wise', |
|---|
| 45 | source => $self->source, |
|---|
| 46 | ); |
|---|
| 47 | my $res = $authsub->login( |
|---|
| 48 | $self->username, |
|---|
| 49 | $self->password, |
|---|
| 50 | ); |
|---|
| 51 | $res->is_success or return; |
|---|
| 52 | return $res->auth; |
|---|
| 53 | }, |
|---|
| 54 | ); |
|---|
| 55 | |
|---|
| 56 | has ua => ( |
|---|
| 57 | isa => 'LWP::UserAgent', |
|---|
| 58 | is => 'ro', |
|---|
| 59 | required => 1, |
|---|
| 60 | lazy => 1, |
|---|
| 61 | default => sub { |
|---|
| 62 | my $self = shift; |
|---|
| 63 | my $ua = LWP::UserAgent->new( |
|---|
| 64 | agent => $self->source, |
|---|
| 65 | ); |
|---|
| 66 | $ua->default_headers( |
|---|
| 67 | HTTP::Headers->new( |
|---|
| 68 | Authorization => sprintf('GoogleLogin auth=%s', $self->auth), |
|---|
| 69 | GData_Version => 2, |
|---|
| 70 | ) |
|---|
| 71 | ); |
|---|
| 72 | return $ua; |
|---|
| 73 | } |
|---|
| 74 | ); |
|---|
| 75 | |
|---|
| 76 | sub spreadsheets { |
|---|
| 77 | my ($self, $args) = @_; |
|---|
| 78 | my $cond = $args->{title} ? |
|---|
| 79 | { |
|---|
| 80 | title => $args->{title}, |
|---|
| 81 | 'title-exact' => 'true' |
|---|
| 82 | } : {}; |
|---|
| 83 | my $feed = $self->feed( |
|---|
| 84 | $self->contents, |
|---|
| 85 | $cond |
|---|
| 86 | ); |
|---|
| 87 | |
|---|
| 88 | return grep { |
|---|
| 89 | (!%$args && 1) |
|---|
| 90 | || |
|---|
| 91 | ($args->{key} && $_->key eq $args->{key}) |
|---|
| 92 | || |
|---|
| 93 | ($args->{title} && $_->title eq $args->{title}) |
|---|
| 94 | } map { |
|---|
| 95 | Net::Google::Spreadsheets::Spreadsheet->new( |
|---|
| 96 | atom => $_, |
|---|
| 97 | service => $self |
|---|
| 98 | ) |
|---|
| 99 | } $feed->entries; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | sub spreadsheet { |
|---|
| 103 | my ($self, $args) = @_; |
|---|
| 104 | return ($self->spreadsheets($args))[0]; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | sub request { |
|---|
| 108 | my ($self, $args) = @_; |
|---|
| 109 | my $method = delete $args->{method}; |
|---|
| 110 | $method ||= $args->{content} ? 'POST' : 'GET'; |
|---|
| 111 | my $uri = URI->new($args->{'uri'}); |
|---|
| 112 | $uri->query_form($args->{query}) if $args->{query}; |
|---|
| 113 | my $req = HTTP::Request->new($method => "$uri"); |
|---|
| 114 | $req->content($args->{content}) if $args->{content}; |
|---|
| 115 | $req->header('Content-Type' => $args->{content_type}) if $args->{content_type}; |
|---|
| 116 | if ($args->{header}) { |
|---|
| 117 | while (my @pair = each %{$args->{header}}) { |
|---|
| 118 | $req->header(@pair); |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | my $res = $self->ua->request($req); |
|---|
| 122 | unless ($res->is_success) { |
|---|
| 123 | # warn $res->request->as_string; |
|---|
| 124 | # warn $res->as_string; |
|---|
| 125 | croak "request failed: ",$res->code; |
|---|
| 126 | } |
|---|
| 127 | return $res; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | sub feed { |
|---|
| 131 | my ($self, $url, $query) = @_; |
|---|
| 132 | my $res = $self->request( |
|---|
| 133 | { |
|---|
| 134 | uri => $url, |
|---|
| 135 | query => $query || undef, |
|---|
| 136 | } |
|---|
| 137 | ); |
|---|
| 138 | return XML::Atom::Feed->new(\($res->content)); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | sub entry { |
|---|
| 142 | my ($self, $url, $query) = @_; |
|---|
| 143 | my $res = $self->request( |
|---|
| 144 | { |
|---|
| 145 | uri => $url, |
|---|
| 146 | query => $query || undef, |
|---|
| 147 | } |
|---|
| 148 | ); |
|---|
| 149 | return XML::Atom::Entry->new(\($res->content)); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | sub post { |
|---|
| 153 | my ($self, $url, $entry, $header) = @_; |
|---|
| 154 | my $res = $self->request( |
|---|
| 155 | { |
|---|
| 156 | uri => $url, |
|---|
| 157 | content => $entry->as_xml, |
|---|
| 158 | header => $header || undef, |
|---|
| 159 | content_type => 'application/atom+xml', |
|---|
| 160 | } |
|---|
| 161 | ); |
|---|
| 162 | return (ref $entry)->new(\($res->content)); |
|---|
| 163 | # return XML::Atom::Entry->new(\($res->content)); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | sub put { |
|---|
| 167 | my ($self, $args) = @_; |
|---|
| 168 | my $res = $self->request( |
|---|
| 169 | { |
|---|
| 170 | method => 'PUT', |
|---|
| 171 | uri => $args->{self}->editurl, |
|---|
| 172 | content => $args->{entry}->as_xml, |
|---|
| 173 | header => {'If-Match' => $args->{self}->etag }, |
|---|
| 174 | content_type => 'application/atom+xml', |
|---|
| 175 | } |
|---|
| 176 | ); |
|---|
| 177 | return XML::Atom::Entry->new(\($res->content)); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | 1; |
|---|
| 181 | __END__ |
|---|
| 182 | |
|---|
| 183 | =head1 NAME |
|---|
| 184 | |
|---|
| 185 | Net::Google::Spreadsheets - A Perl module for using Google Spreadsheets API. |
|---|
| 186 | |
|---|
| 187 | =head1 SYNOPSIS |
|---|
| 188 | |
|---|
| 189 | use Net::Google::Spreadsheets; |
|---|
| 190 | |
|---|
| 191 | my $service = Net::Google::Spreadsheets->new( |
|---|
| 192 | username => 'myname@gmail.com', |
|---|
| 193 | password => 'mypassword' |
|---|
| 194 | ); |
|---|
| 195 | |
|---|
| 196 | my @spreadsheets = $service->spreadsheets(); |
|---|
| 197 | |
|---|
| 198 | # find a spreadsheet by key |
|---|
| 199 | my $spreadsheet = $service->spreadsheet({key => 'pZV-pns_sm9PtH2WowhU2Ew'}); |
|---|
| 200 | |
|---|
| 201 | # find a spreadsheet by title |
|---|
| 202 | my $spreadsheet = $service->spreadsheet({title => 'list for new year cards'}); |
|---|
| 203 | my $worksheet = $spreadsheet->worksheet(1); |
|---|
| 204 | |
|---|
| 205 | my @fields = $worksheet->fields(); |
|---|
| 206 | |
|---|
| 207 | my $inserted_row = $worksheet->insert( |
|---|
| 208 | { |
|---|
| 209 | name => 'danjou', |
|---|
| 210 | } |
|---|
| 211 | ); |
|---|
| 212 | |
|---|
| 213 | my @rows = $worksheet->rows; |
|---|
| 214 | |
|---|
| 215 | my $row = $worksheet->row(1); |
|---|
| 216 | |
|---|
| 217 | $row->update( |
|---|
| 218 | { |
|---|
| 219 | nick => 'lopnor', |
|---|
| 220 | mail => 'nobuo.danjou@gmail.com', |
|---|
| 221 | } |
|---|
| 222 | ); |
|---|
| 223 | |
|---|
| 224 | =head1 DESCRIPTION |
|---|
| 225 | |
|---|
| 226 | Net::Google::Spreadsheets is a Perl module for using Google Spreadsheets API. |
|---|
| 227 | |
|---|
| 228 | =head1 AUTHOR |
|---|
| 229 | |
|---|
| 230 | Nobuo Danjou E<lt>nobuo.danjou@gmail.comE<gt> |
|---|
| 231 | |
|---|
| 232 | =head1 SEE ALSO |
|---|
| 233 | |
|---|
| 234 | =head1 LICENSE |
|---|
| 235 | |
|---|
| 236 | This library is free software; you can redistribute it and/or modify |
|---|
| 237 | it under the same terms as Perl itself. |
|---|
| 238 | |
|---|
| 239 | =cut |
|---|