X-Git-Url: http://git.rot13.org/?p=angular-mojolicious.git;a=blobdiff_plain;f=angular-server.pl;h=948759928ee073f0f6a416978e284164c6181e9f;hp=57e40a475957de8dc64029e6d2de748b92f8d4f2;hb=1c641d9c92981bab0e0fe9ad2e89bdf2e6d3f088;hpb=d44d1dd771e6c733ee519b6ee7fae239e5b17566 diff --git a/angular-server.pl b/angular-server.pl index 57e40a4..9487599 100755 --- a/angular-server.pl +++ b/angular-server.pl @@ -6,6 +6,7 @@ use Mojolicious::Lite; use Data::Dump qw(dump); use Time::HiRes; use Clone qw(clone); +use Mojo::UserAgent; sub new_uuid { Time::HiRes::time * 100000 } @@ -13,8 +14,8 @@ sub new_uuid { Time::HiRes::time * 100000 } # http://docs.getangular.com/REST.Basic # http://angular.getangular.com/data -my $couchdb = 'http://localhost:5984'; -my $client = Mojo::Client->new; +my $couchdb = $ENV{COUCHDB} || 'http://localhost:5984'; +my $client = Mojo::UserAgent->new; sub _couchdb_put { my ( $url, $data ) = @_; @@ -26,20 +27,7 @@ sub _couchdb_put { my $rev; warn "# _couchdb_put $url = $json"; - $client->put( "$couchdb/$url" => $json => sub { - my ($client,$tx) = @_; - my ($message, $code) = $tx->error; - my $response = $tx->res->json; - warn "## response $code ",dump($response); - if ($tx->error) { - warn "ERROR $code $message"; - } - return - $rev = $response->{rev}; - })->process; - - warn "## rev = $rev"; - return $rev; + return $client->put( "$couchdb/$url" => $json)->res->json; } sub _couchdb_get { @@ -142,8 +130,14 @@ any [ 'post' ] => '/data/:database/:entity' => sub { $json->{'$id'} ||= $id; # make sure $id is in there - my $rev = _couchdb_put "/$database/$entity.$id" => $json; - $json->{_rev} = $rev; + my $new = _couchdb_put "/$database/$entity.$id" => $json; + warn "new: ",dump($new); + if ( $new->{ok} ) { + $json->{'_'.$_} = $new->{$_} foreach ( 'rev','id' ); + } else { + warn "ERROR: ",dump($new); + $json->{_error} = $new; + } _render_jsonp( $self, $json ); }; @@ -230,6 +224,46 @@ get '/json/:database/:entity' => sub { _render_jsonp( $self, $docs ) }; +# app/resevations +use Encode; +use iCal::Parser; + +get '/reservations/get/(*url)' => sub { + my $self = shift; + + my $text = $client->get( 'http://' . $self->param('url') )->res->body; + warn "# get ", $self->param('url'), dump($text); + + $text = decode( 'utf-8', $text ); + $text =~ s{\\,}{,}gs; + $text =~ s{\\n}{ }gs; + + my $c = iCal::Parser->new->parse_strings( $text ); + + warn "# iCal::Parser = ",dump($c); + + my $ical = { + cal => $c->{cals}->[0], # FIXME assume single calendar + }; + + my $e = $c->{events}; + my @events; + + foreach my $yyyy ( sort keys %$e ) { + foreach my $mm ( sort keys %{ $e->{$yyyy} } ) { + foreach my $dd ( sort keys %{ $e->{$yyyy}->{$mm} } ) { + push @events, values %{ $e->{$yyyy}->{$mm}->{$dd} }; + } + } + } + + $ical->{events} = [ sort { + $a->{DTSTART} cmp $b->{DTSTART} + } @events ]; + + _render_jsonp( $self, $ical ); +}; + app->start; __DATA__