Permalink
Browse files

Add reverse_dependencies action for rubygems, shorter output version

  • Loading branch information...
1 parent 70c4d9c commit 7aaf0558de171c492a19480d1fd0f66d50c47ac6 @alup alup committed Mar 5, 2013
View
10 app/controllers/api/v1/rubygems_controller.rb
@@ -46,6 +46,16 @@ def unyank
end
end
+ def reverse_dependencies
+ rubygems = Rubygem.reverse_dependencies(params[:id])
+
+ if params[:short]
+ respond_with(rubygems.map(&:name), :yamlish => true)
+ else
+ respond_with(rubygems, :yamlish => true)
+ end
+ end
+
private
def validate_gem_and_version
View
12 app/controllers/api/v1/versions_controller.rb
@@ -16,10 +16,14 @@ def show
def reverse_dependencies
versions = Version.reverse_dependencies(params[:id])
- respond_to do |format|
- format.json { render :json => versions.map { |v| v.as_json.merge("full_name" => v.full_name) } }
- format.xml { render :xml => versions.map { |v| v.payload.merge("full_name" => v.full_name) } }
- format.yaml { render :text => versions.map { |v| v.payload.merge("full_name" => v.full_name) }.to_yaml }
+ if params[:short]
+ respond_with(versions.map(&:full_name), :yamlish => true)
+ else
+ respond_to do |format|
+ format.json { render :json => versions.map { |v| v.as_json.merge("full_name" => v.full_name) } }
+ format.xml { render :xml => versions.map { |v| v.payload.merge("full_name" => v.full_name) } }
+ format.yaml { render :text => versions.map { |v| v.payload.merge("full_name" => v.full_name) }.to_yaml }
+ end
end
end
end
View
3 config/routes.rb
@@ -48,6 +48,9 @@
resources :dependencies, :only => :index
resources :rubygems, :path => 'gems', :only => [:create, :show, :index], :id => Patterns::LAZY_ROUTE_PATTERN, :format => /json|xml|yaml/ do
+ member do
+ get :reverse_dependencies
+ end
collection do
delete :yank
put :unyank
View
46 test/functional/api/v1/rubygems_controller_test.rb
@@ -419,4 +419,50 @@ def should_return_just_updated_gems(gems)
end
end
end
+
+ context "on GET to reverse_dependencies" do
+ setup do
+ @dep_rubygem = create(:rubygem)
+ @gem_one = create(:rubygem)
+ @gem_two = create(:rubygem)
+ @gem_three = create(:rubygem)
+ @gem_four = create(:rubygem)
+ @version_one_latest = create(:version, :rubygem => @gem_one, :number => '0.2')
+ @version_one_earlier = create(:version, :rubygem => @gem_one, :number => '0.1')
+ @version_two_latest = create(:version, :rubygem => @gem_two, :number => '1.0')
+ @version_two_earlier = create(:version, :rubygem => @gem_two, :number => '0.5')
+ @version_three = create(:version, :rubygem => @gem_three, :number => '1.7')
+ @version_four = create(:version, :rubygem => @gem_four, :number => '3.9')
+
+ @version_one_latest.dependencies << create(:dependency, :version => @version_one_latest, :rubygem => @dep_rubygem)
+ @version_two_earlier.dependencies << create(:dependency, :version => @version_two_earlier, :rubygem => @dep_rubygem)
+ @version_three.dependencies << create(:dependency, :version => @version_three, :rubygem => @dep_rubygem)
+ end
+
+ should "give all reverse dependencies" do
+ get :reverse_dependencies, :id => @dep_rubygem.to_param, :format => "json"
+ gems = MultiJson.load(@response.body).map { |h| h["name"] }
+
+ assert_equal 3, gems.size
+
+ assert gems.include?(@gem_one.name)
+ assert gems.include?(@gem_two.name)
+ assert gems.include?(@gem_three.name)
+ assert ! gems.include?(@gem_four.name)
+ end
+
+ context "with 'short=true' param" do
+ should "return only names of reverse dependencies" do
+ get :reverse_dependencies, :id => @dep_rubygem.to_param, :format => "json", :short => true
+ gems = MultiJson.load(@response.body)
+
+ assert_equal 3, gems.size
+
+ assert gems.include?(@gem_one.name)
+ assert gems.include?(@gem_two.name)
+ assert gems.include?(@gem_three.name)
+ assert ! gems.include?(@gem_four.name)
+ end
+ end
+ end
end
View
29 test/functional/api/v1/versions_controller_test.rb
@@ -5,8 +5,8 @@ def get_show(rubygem, format='json')
get :show, :id => rubygem.name, :format => format
end
- def get_reverse_dependencies(rubygem, format='json')
- get :reverse_dependencies, :id => rubygem.name, :format => format
+ def get_reverse_dependencies(rubygem, options={ :format => 'json' })
+ get :reverse_dependencies, options.merge(:id => rubygem.name)
end
def self.should_respond_to(format)
@@ -136,11 +136,11 @@ def set_cache_header(timestamp)
@gem_one = create(:rubygem)
@gem_two = create(:rubygem)
@gem_three = create(:rubygem)
- @version_one_latest = create(:version, :rubygem => @gem_one, :number => '0.2')
- @version_one_earlier = create(:version, :rubygem => @gem_one, :number => '0.1')
- @version_two_latest = create(:version, :rubygem => @gem_two, :number => '1.0')
- @version_two_earlier = create(:version, :rubygem => @gem_two, :number => '0.5')
- @version_three = create(:version, :rubygem => @gem_three, :number => '1.7')
+ @version_one_latest = create(:version, :rubygem => @gem_one, :number => '0.2', :full_name => "gem_one-0.2")
+ @version_one_earlier = create(:version, :rubygem => @gem_one, :number => '0.1', :full_name => "gem_one-0.1")
+ @version_two_latest = create(:version, :rubygem => @gem_two, :number => '1.0', :full_name => "gem_two-1.0")
+ @version_two_earlier = create(:version, :rubygem => @gem_two, :number => '0.5', :full_name => "gem_two-0.5")
+ @version_three = create(:version, :rubygem => @gem_three, :number => '1.7', :full_name => "gem_three-1.7")
@version_one_latest.dependencies << create(:dependency, :version => @version_one_latest, :rubygem => @dep_rubygem)
@version_two_earlier.dependencies << create(:dependency, :version => @version_two_earlier, :rubygem => @dep_rubygem)
@@ -159,5 +159,20 @@ def set_cache_header(timestamp)
assert ! ret_versions.include?(@version_one_earlier.number)
assert ! ret_versions.include?(@version_two_latest.number)
end
+
+ context "with 'short=true' param" do
+ should "return only names of reverse dependencies" do
+ get_reverse_dependencies(@dep_rubygem, :format => "json", :short => true)
+ ret_versions = MultiJson.load(@response.body)
+
+ assert_equal 3, ret_versions.size
+
+ assert ret_versions.include?(@version_one_latest.full_name)
+ assert ret_versions.include?(@version_two_earlier.full_name)
+ assert ret_versions.include?(@version_three.full_name)
+ assert ! ret_versions.include?(@version_one_earlier.full_name)
+ assert ! ret_versions.include?(@version_two_latest.full_name)
+ end
+ end
end
end

0 comments on commit 7aaf055

Please sign in to comment.