Skip to content

Commit

Permalink
Add YAML response format for rubygems API
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jul 18, 2011
1 parent 610e88a commit 440f79c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 23 deletions.
18 changes: 12 additions & 6 deletions app/controllers/api/v1/rubygems_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@ class Api::V1::RubygemsController < Api::BaseController

def index
@rubygems = current_user.rubygems.with_versions
respond_to do |wants|
wants.any(:json, :all) { render :json => @rubygems }
wants.xml { render :xml => @rubygems }
respond_to do |format|
format.any(:json, :all) { render :json => @rubygems }
format.xml { render :xml => @rubygems }
# Convert object to JSON and back before converting to YAML in order to
# strip the object type (e.g. !ruby/ActiveRecord:Rubygem) from response
format.yaml { render :text => JSON.load(@rubygems.to_json).to_yaml }
end
end

def show
if @rubygem.hosted?
respond_to do |wants|
wants.json { render :json => @rubygem }
wants.xml { render :xml => @rubygem }
respond_to do |format|
format.json { render :json => @rubygem }
format.xml { render :xml => @rubygem }
# Convert object to JSON and back before converting to YAML in order to
# strip the object type (e.g. !ruby/ActiveRecord:Rubygem) from response
format.yaml { render :text => JSON.load(@rubygem.to_json).to_yaml }
end
else
render :text => "This gem does not exist.", :status => :not_found
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/searches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def show
format.xml { render :xml => @gems }
# Convert object to JSON and back before converting to YAML in order to
# strip the object type (e.g. !ruby/ActiveRecord:Rubygem) from response
format.yaml { render :text => JSON.load(@gems.to_json).to_yaml}
format.yaml { render :text => JSON.load(@gems.to_json).to_yaml }
end
end

Expand Down
73 changes: 57 additions & 16 deletions test/functional/api/v1/rubygems_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Api::V1::RubygemsControllerTest < ActionController::TestCase
sign_in_as(@user)
end

context "On GET to show with json for a gem that's hosted" do
context "On GET to show with JSON for a gem that's hosted" do
setup do
@rubygem = Factory(:rubygem, :name => "foo")
Factory(:version, :rubygem => @rubygem)
Expand All @@ -24,12 +24,14 @@ class Api::V1::RubygemsControllerTest < ActionController::TestCase

should assign_to(:rubygem) { @rubygem }
should respond_with :success
should "return a json hash" do
assert_not_nil JSON.parse(@response.body)
should "return a JSON hash" do
response = JSON.parse(@response.body)
assert_not_nil response
assert_kind_of Hash, response
end
end

context "On GET to show with json for a gem that's hosted with a period in its name" do
context "On GET to show with JSON for a gem that's hosted with a period in its name" do
setup do
@rubygem = Factory(:rubygem, :name => "foo.rb")
Factory(:version, :rubygem => @rubygem)
Expand All @@ -38,12 +40,14 @@ class Api::V1::RubygemsControllerTest < ActionController::TestCase

should assign_to(:rubygem) { @rubygem }
should respond_with :success
should "return a json hash" do
assert_not_nil JSON.parse(@response.body)
should "return a JSON hash" do
response = JSON.parse(@response.body)
assert_not_nil response
assert_kind_of Hash, response
end
end

context "On GET to show with xml for a gem that's hosted" do
context "On GET to show with XML for a gem that's hosted" do
setup do
@rubygem = Factory(:rubygem)
Factory(:version, :rubygem => @rubygem)
Expand All @@ -52,12 +56,14 @@ class Api::V1::RubygemsControllerTest < ActionController::TestCase

should assign_to(:rubygem) { @rubygem }
should respond_with :success
should "return a json hash" do
assert_not_nil Nokogiri.parse(@response.body).root
should "return an XML document" do
response = Nokogiri.parse(@response.body).root
assert_not_nil response
assert_kind_of Nokogiri::XML::Element, response
end
end

context "On GET to show with xml for a gem that's hosted with a period in its name" do
context "On GET to show with XML for a gem that's hosted with a period in its name" do
setup do
@rubygem = Factory(:rubygem, :name => "foo.rb")
Factory(:version, :rubygem => @rubygem)
Expand All @@ -66,10 +72,45 @@ class Api::V1::RubygemsControllerTest < ActionController::TestCase

should assign_to(:rubygem) { @rubygem }
should respond_with :success
should "return a json hash" do
assert_not_nil Nokogiri.parse(@response.body).root
should "return an XML document" do
response = Nokogiri.parse(@response.body).root
assert_not_nil response
assert_kind_of Nokogiri::XML::Element, response
end
end

context "On GET to show with YAML for a gem that's hosted" do
setup do
@rubygem = Factory(:rubygem)
Factory(:version, :rubygem => @rubygem)
get :show, :id => @rubygem.to_param, :format => "yaml"
end

should assign_to(:rubygem) { @rubygem }
should respond_with :success
should "return a YAML hash" do
response = YAML.load(@response.body)
assert_not_nil response
assert_kind_of Hash, response
end
end

context "On GET to show with YAML for a gem that's hosted with a period in its name" do
setup do
@rubygem = Factory(:rubygem, :name => "foo.rb")
Factory(:version, :rubygem => @rubygem)
get :show, :id => @rubygem.to_param, :format => "yaml"
end

should assign_to(:rubygem) { @rubygem }
should respond_with :success
should "return an YAML hash" do
response = YAML.load(@response.body)
assert_not_nil response
assert_kind_of Hash, response
end
end

context "On GET to show for a gem that doesn't match the slug" do
setup do
@rubygem = Factory(:rubygem, :name => "ZenTest", :slug => "zentest")
Expand All @@ -79,7 +120,7 @@ class Api::V1::RubygemsControllerTest < ActionController::TestCase

should assign_to(:rubygem) { @rubygem }
should respond_with :success
should "return a json hash" do
should "return a JSON hash" do
assert_not_nil JSON.parse(@response.body)
end
end
Expand Down Expand Up @@ -345,7 +386,7 @@ class Api::V1::RubygemsControllerTest < ActionController::TestCase
end
end

context "On GET to index with json for a list of owned gems" do
context "On GET to index with JSON for a list of owned gems" do
setup do
@mygems = [ Factory(:rubygem, :name => "SomeGem"), Factory(:rubygem, :name => "AnotherGem") ]
@mygems.each do |rubygem|
Expand All @@ -363,7 +404,7 @@ class Api::V1::RubygemsControllerTest < ActionController::TestCase

should assign_to(:rubygems) { [@rubygem] }
should respond_with :success
should "return a json hash" do
should "return a JSON hash" do
assert_not_nil JSON.parse(@response.body)
end
should "only return my gems" do
Expand All @@ -374,7 +415,7 @@ class Api::V1::RubygemsControllerTest < ActionController::TestCase
end

context "No signed in-user" do
context "On GET to index with json for a list of gems" do
context "On GET to index with JSON for a list of gems" do
setup do
get :index, :format => "json"
end
Expand Down

0 comments on commit 440f79c

Please sign in to comment.