Created
December 16, 2013 20:43
-
-
Save s1037989/7994037 to your computer and use it in GitHub Desktop.
Mojolicious Plugin CouchDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat lib/Mojolicious/Plugin/CouchDB.pm | |
package Mojolicious::Plugin::CouchDB; | |
use Mojo::Base 'Mojolicious::Plugin'; | |
use Mojo::Exception; | |
use Mojo::JSON 'j'; | |
our $VERSION = '0.01'; | |
has 'couch' => 'http://127.0.0.1:5984/'; | |
has 'app'; | |
has 'db'; | |
sub register { | |
my $self = shift; | |
my $app = shift; | |
my $config = shift || {}; | |
if ( defined $config->{url} ) { | |
$config->{url}.='/' unless $config->{url} =~ /\/$/; | |
$self->couch($config->{url}); | |
} | |
$app->helper(couchdb => sub { | |
my ($c, $db) = (@_); | |
$self->app($c); | |
$self->db($db) if defined $db; | |
unless ( $self->couch ) { | |
warn "Couch Server 'url' not defined\n"; | |
return undef; | |
} | |
return $self; | |
}); | |
} | |
sub info { | |
my $self = shift; | |
my $cb = ref $_[-1] eq 'CODE' ? pop : undef; | |
# Non-blocking | |
return $self->app->ua->get($self->couch => sub { | |
my ($ua, $tx) = @_; | |
$self->$cb($tx->res->json); | |
}) if $cb; | |
# Blocking | |
$self->app->ua->get($self->couch)->res->json; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment