-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#1035] Create convenience DW::RPC::out/err/alert methods
Instead of redefining the print logic in each handler, just do this once.
- Loading branch information
Showing
2 changed files
with
57 additions
and
30 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/perl | ||
# | ||
# Authors: | ||
# Afuna <coder.dw@afunamatata.com> | ||
# | ||
# Copyright (c) 2014 by Dreamwidth Studios, LLC. | ||
# | ||
# This program is free software; you may redistribute it and/or modify it under | ||
# the same terms as Perl itself. For a copy of the license, please reference | ||
# 'perldoc perlartistic' or 'perldoc perlgpl'. | ||
|
||
package DW::RPC; | ||
|
||
use strict; | ||
use LJ::JSON; | ||
|
||
=head1 NAME | ||
DW::RPC - Convenience methods to print output for RPC endpoints | ||
=head1 SYNOPSIS | ||
=cut | ||
|
||
sub out { | ||
my ( $class, %obj ) = @_; | ||
my $r = DW::Request->get; | ||
$r->print( to_json( \%obj ) ); | ||
return $r->OK; | ||
} | ||
|
||
# return error as { error => error or "" } | ||
sub err { | ||
my ( $class, $err ) = @_; | ||
my $r = DW::Request->get; | ||
$r->print( to_json( { error => $err ? $err : "" } ) ); | ||
return $r->OK; | ||
} | ||
|
||
# return error as { alert => ..., error => 1 } | ||
sub alert { | ||
my ( $class, $err ) = @_; | ||
my $r = DW::Request->get; | ||
$r->print( to_json( { alert => $err, error => 1 } ) ); | ||
return $r->OK; | ||
} | ||
|
||
1; |