Skip to content
Browse files

Add support for json and xml

  • Loading branch information...
1 parent 280d341 commit 5a28bab0fda8368c3df2c9ca4c7798c79557d02b @joshuaclayton committed Oct 9, 2009
View
7 app/controllers/application_controller.rb
@@ -2,9 +2,6 @@
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
- helper :all # include all helpers, all the time
- protect_from_forgery # See ActionController::RequestForgeryProtection for details
-
- # Scrub sensitive parameters from your log
- # filter_parameter_logging :password
+ helper :all
+ protect_from_forgery
end
View
14 app/controllers/peas_controller.rb
@@ -1,7 +1,19 @@
class PeasController < InheritedResources::Base
- respond_to :html
+ respond_to :html, :xml, :json
actions :create, :show
+ def show
+ show! do |format|
+ @short_url = peapod_url(:pea_key => @pea.key)
+ format.json do
+ render :json => {
+ :long_url => @pea.long_url,
+ :short_url => @short_url
+ }
+ end
+ end
+ end
+
def create
if (pea = Pea.find_by_long_url(params[:pea][:long_url]))
return redirect_to(pea_path(pea))
View
5 app/views/peas/show.xml.builder
@@ -0,0 +1,5 @@
+xml.instruct!
+xml.pea do |node|
+ node.long_url @pea.long_url
+ node.short_url @short_url
+end
View
25 test/functional/peas_controller_test.rb
@@ -1,8 +1,25 @@
-require 'test_helper'
+require "test_helper"
class PeasControllerTest < ActionController::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
+ context "a GET to :show" do
+ setup do
+ @pea = Factory(:pea, :long_url => "http://www.google.com")
+ end
+
+ context "as HTML" do
+ setup { get :show, :id => @pea.to_param }
+ should_assign_to(:pea) { @pea }
+ should_render_template :show
+ end
+
+ context "as JSON" do
+ setup { get :show, :id => @pea.to_param, :format => "json" }
+ should_respond_with_content_type :json
+ end
+
+ context "as XML" do
+ setup { get :show, :id => @pea.to_param, :format => "xml" }
+ should_respond_with_content_type :xml
+ end
end
end

0 comments on commit 5a28bab

Please sign in to comment.
Something went wrong with that request. Please try again.