Skip to content

Commit

Permalink
Add multiple response formats for versions
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jul 26, 2011
1 parent 2a0b290 commit 5d1f8b0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 23 deletions.
9 changes: 6 additions & 3 deletions app/controllers/api/v1/versions_controller.rb
@@ -1,8 +1,11 @@
class Api::V1::VersionsController < Api::BaseController
def show
gem_name = params[:id].try(:chomp, ".json")
if rubygem = Rubygem.find_by_name(gem_name)
render :json => rubygem.public_versions
if rubygem = Rubygem.find_by_name(params[:id])
respond_to do |format|
format.json { render :json => rubygem.public_versions }
format.xml { render :xml => rubygem.public_versions }
format.yaml { render :text => rubygem.public_versions.to_yaml }
end
else
render :text => "This rubygem could not be found.", :status => 404
end
Expand Down
5 changes: 5 additions & 0 deletions config/routes.rb
Expand Up @@ -9,7 +9,12 @@
end
resources :downloads, :only => :index
constraints :id => Rubygem::ROUTE_PATTERN do
# In Rails 3.1, the following line can be replaced with
# resources :downloads, :only => :show, :format => true
get 'downloads/:id.:format', :to => 'downloads#show', :as => 'download'
# In Rails 3.1, the next TWO lines can be replaced with
# resources :versions, :only => :show, :format => true do
get 'versions/:id.:format', :to => 'versions#show', :as => 'version'
resources :versions, :only => :show do
resources :downloads, :only => :index, :controller => "versions/downloads" do
collection do
Expand Down
97 changes: 77 additions & 20 deletions test/functional/api/v1/versions_controller_test.rb
@@ -1,8 +1,8 @@
require 'test_helper'

class Api::V1::VersionsControllerTest < ActionController::TestCase
def get_show(rubygem)
get :show, :id => "#{rubygem.name}.json"
def get_show(rubygem, format='json')
get :show, :id => rubygem.name, :format => format
end

context "on GET to show" do
Expand All @@ -18,30 +18,87 @@ def get_show(rubygem)
Factory(:version, :rubygem => @rubygem2, :number => '1.0.0')
end

should "have some json with the list of versions for the first gem" do
get_show(@rubygem)
assert_equal 2, JSON.parse(@response.body).size
end
context "with JSON" do
should "have some JSON with the list of versions for the first gem" do
get_show(@rubygem)
assert_equal 2, JSON.parse(@response.body).size
end

should "be ordered by position with prereleases" do
get_show(@rubygem)
json = JSON.parse(@response.body)
assert_equal "2.0.0", json.first["number"]
assert_equal "1.0.0.pre", json.second["number"]
should "be ordered by position with prereleases" do
get_show(@rubygem)
json = JSON.parse(@response.body)
assert_equal "2.0.0", json.first["number"]
assert_equal "1.0.0.pre", json.second["number"]
end

should "be ordered by position" do
get_show(@rubygem2)
json = JSON.parse(@response.body)
assert_equal "3.0.0", json.first["number"]
assert_equal "2.0.0", json.second["number"]
assert_equal "1.0.0", json.third["number"]
end

should "have some JSON with the list of versions for the second gem" do
get_show(@rubygem2)
assert_equal 3, JSON.parse(@response.body).size
end
end

should "be ordered by position" do
get_show(@rubygem2)
json = JSON.parse(@response.body)
assert_equal "3.0.0", json.first["number"]
assert_equal "2.0.0", json.second["number"]
assert_equal "1.0.0", json.third["number"]
context "with XML" do
should "have some XML with the list of versions for the first gem" do
get_show(@rubygem, 'xml')
assert_equal 2, Nokogiri.parse(@response.body).css('version').size
end

should "be ordered by position with prereleases" do
get_show(@rubygem, 'xml')
xml = Nokogiri.parse(@response.body).css('number')
assert_equal "2.0.0", xml[0].content
assert_equal "1.0.0.pre", xml[1].content
end

should "be ordered by position" do
get_show(@rubygem2, 'xml')
xml = Nokogiri.parse(@response.body).css('number')
assert_equal "3.0.0", xml[0].content
assert_equal "2.0.0", xml[1].content
assert_equal "1.0.0", xml[2].content
end

should "have some XML with the list of versions for the second gem" do
get_show(@rubygem2, 'xml')
assert_equal 3, Nokogiri.parse(@response.body).css('version').size
end
end

should "have some json with the list of versions for the second gem" do
get_show(@rubygem2)
assert_equal 3, JSON.parse(@response.body).size
context "with YAML" do
should "have some YAML with the list of versions for the first gem" do
get_show(@rubygem, 'yaml')
assert_equal 2, YAML.load(@response.body).size
end

should "be ordered by position with prereleases" do
get_show(@rubygem, 'yaml')
yaml = YAML.load(@response.body)
assert_equal "2.0.0", yaml.first["number"]
assert_equal "1.0.0.pre", yaml.second["number"]
end

should "be ordered by position" do
get_show(@rubygem2, 'yaml')
yaml = YAML.load(@response.body)
assert_equal "3.0.0", yaml.first["number"]
assert_equal "2.0.0", yaml.second["number"]
assert_equal "1.0.0", yaml.third["number"]
end

should "have some YAML with the list of versions for the second gem" do
get_show(@rubygem2, 'yaml')
assert_equal 3, YAML.load(@response.body).size
end
end

end

context "on GET to show for an unknown gem" do
Expand Down

0 comments on commit 5d1f8b0

Please sign in to comment.