Created
April 20, 2010 23:32
-
-
Save dhoss/373213 to your computer and use it in GitHub Desktop.
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
package Burn::ControllerBase::REST; | |
use Moose; | |
use namespace::autoclean; | |
BEGIN { extends qw/Catalyst::Controller::REST/ } | |
__PACKAGE__->config( | |
'default' => 'text/html', | |
map => { | |
'application/x-www-form-urlencoded' => [ 'View', 'TT' ], | |
'text/html' => [ 'View', 'TT' ], | |
'application/json' => [ 'View', 'JSON' ], | |
}, | |
); | |
__PACKAGE__->meta->make_immutable; | |
1; |
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
package Burn::View::JSON; | |
use strict; | |
use parent 'Catalyst::View::JSON'; | |
use JSON::XS; | |
sub encode_json($) { | |
my ( $self, $c, $data ) = @_; | |
my $encoder = JSON::XS->new->ascii->pretty->allow_nonref->allow_blessed; | |
$encoder->encode($data); | |
} |
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
use strict; | |
use warnings; | |
use Test::More; | |
use HTTP::Request::Common; | |
use Data::Dumper; | |
use utf8; | |
BEGIN { use_ok 'Catalyst::Test', 'Burn' } | |
BEGIN { use_ok 'Burn::Controller::Workout' } | |
ok( request('/workout')->is_success, 'Request should succeed' ); | |
use JSON::XS; | |
my $json = JSON::XS->new->utf8; | |
# The text/x-json should throw a warning | |
my $monkey_template = { monkey => 'likes chicken!', }; | |
my $post_data = { | |
'sushi' => 'is good for monkey', | |
}; | |
my $json_string = $json->encode($post_data); | |
my $mres_post = request( | |
POST '/workout', | |
Content_Type => "application/json", | |
Content => $json_string | |
); | |
warn "Json: " . Dumper $json_string; | |
warn "Request: " | |
. POST( | |
'/workout', | |
Content_Type => "application/json", | |
Content => [ json => $json_string] | |
)->as_string; | |
warn "Content: " . Dumper $mres_post->content; | |
ok( $mres_post->is_success, "POST to the monkey succeeded" ); | |
done_testing(); |
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
<script type="text/javascript" src="http://jquery-json.googlecode.com/files/jquery.json-1.3.min.js"></script> | |
<script type="text/javascript"> | |
$.fn.serializeObject = function() | |
{ | |
var o = {}; | |
var a = this.serializeArray(); | |
$.each(a, function() { | |
if (o[this.name]) { | |
if (!o[this.name].push) { | |
o[this.name] = [o[this.name]]; | |
} | |
o[this.name].push(this.value || ''); | |
} else { | |
o[this.name] = this.value || ''; | |
} | |
}); | |
return o; | |
}; | |
$(document).ready(function(){ | |
//$("#add-row").click(function(){ | |
// $("#workout-table >tbody tr:last").after(row) | |
//}); | |
$('#workout-submit').click(function(){ | |
$.ajax({ | |
type: 'POST', | |
url: "[% c.uri_for_action('/workout/setup') %]", | |
data: $.toJSON($('form').serializeObject()), | |
dataType: "json", | |
contentType: "application/json", | |
success: function(res){ | |
$("#status").html("status: " + res.rest.response); | |
}, | |
}); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<h2>Create a new workout</h2> | |
<div id="status"></div> | |
<form id="workout-form"> | |
<ol> | |
<li><label for="workout[name]">Workout Name:</label><input type="text" name="workout[name]" /></li> | |
<li><label for="workout[description]">Description</label></li> | |
<li><textarea name="workout[description]" rows="10" cols="45"></textarea></li> | |
</ol> | |
<table name="exercises" id="workout-table"> | |
<tr> | |
<th>Name</th> | |
<th>Reps/Intervals</th> | |
<th>Sets</th> | |
<th>Weight/Distance/Time</th> | |
</tr> | |
[%- i=0 %] | |
[% WHILE i<=10 %] | |
<tr> | |
<div class="workout-rows"> | |
<td><input type="text" name="workout[exercise][[% i %]][name]" /></td> | |
<td><input type="text" name="workout[exercise][[% i %]][reps]" size="3"/></td> | |
<td><input type="text" name="workout[exercise][[% i %]][sets]" size="3"/></td> | |
<td><input type="text" name="workout[exercise][[% i %]][weight]" size="4"/></td> | |
</div> | |
</tr> | |
[% i = i + 1 %] | |
[% END %] | |
</table> | |
<!-- fuck with me later <div id="add-row">add another row</div>--> | |
<ol> | |
<label for="star">Rate this workout:</label> | |
<input name="workout[rating]" type="radio" class="star" value="1" /> | |
<input name="workout[rating]" type="radio" class="star" value="2" /> | |
<input name="workout[rating]" type="radio" class="star" value="3" /> | |
<input name="workout[rating]" type="radio" class="star" value="4" /> | |
<input name="workout[rating]" type="radio" class="star" value="5" /> | |
</li> | |
</ol> | |
<div><input type="button" id="workout-submit" value="Add this workout"/></div> | |
</form> |
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
[info] *** Request 2 (2.000/s) [10296] [Tue Apr 20 17:32:19 2010] *** | |
[debug] "POST" request for "workout" from "127.0.0.1" | |
[debug] Path is "/workout/setup" | |
[debug] POST: $VAR1 = undef; | |
[debug] Serializing with Catalyst::Action::Serialize::View [JSON] | |
[info] Request took 0.015602s (64.094/s) | |
.------------------------------------------------------------+-----------. | |
| Action | Time | | |
+------------------------------------------------------------+-----------+ | |
| /workout/begin | 0.007291s | | |
| /workout | 0.000160s | | |
| /workout/setup | 0.000096s | | |
| /setup_POST | 0.000541s | | |
| /workout/end | 0.001811s | | |
'------------------------------------------------------------+-----------' | |
Json: $VAR1 = '{"sushi":"is good for monkey"}'; | |
Request: POST /workout | |
Content-Length: 30 | |
Content-Type: application/json | |
{"sushi":"is good for monkey"} | |
Content: $VAR1 = '{ | |
"rest" : { | |
"response" : "$VAR1 = undef;\\n" | |
} | |
} | |
'; | |
ok 4 - POST to the monkey succeeded | |
1..4 | |
ok | |
All tests successful. | |
Files=1, Tests=4, 2 wallclock secs ( 0.03 usr 0.01 sys + 1.87 cusr 0.11 csys = 2.02 CPU) | |
Result: PASS |
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
sub setup : Chained('.') PathPart('workout') Args('0') ActionClass('REST') { | |
my ( $self, $c ) = @_; | |
} | |
sub setup_POST { | |
my ( $self, $c ) = @_; | |
$c->log->debug( 'POST: ' . Dumper $c->req->data ); | |
$self->status_created( | |
$c, | |
entity => { response => Dumper $c->req->data }, | |
location => $c->req->uri->as_string, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment