Skip to content
This repository has been archived by the owner on May 12, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:qrush/gemcutter
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Copeland committed Nov 21, 2009
2 parents b1ae138 + 225854b commit b5ee35b
Show file tree
Hide file tree
Showing 13 changed files with 294 additions and 178 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ApiKeysController < ApplicationController
class Api::V1::ApiKeysController < ApplicationController
before_filter :redirect_to_root, :unless => :signed_in?, :only => [:reset]

def show
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class OwnersController < ApplicationController
class Api::V1::OwnersController < ApplicationController

skip_before_filter :verify_authenticity_token, :only => [:create, :destroy]

Expand Down
21 changes: 21 additions & 0 deletions app/controllers/api/v1/rubygems_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Api::V1::RubygemsController < ApplicationController
skip_before_filter :verify_authenticity_token, :only => :create

before_filter :authenticate_with_api_key, :only => :create
before_filter :verify_authenticated_user, :only => :create
before_filter :find_gem, :only => :show

def show
if @rubygem.hosted?
render :json => @rubygem.to_json
else
render :json => "This gem does not exist.", :status => :not_found
end
end

def create
gemcutter = Gemcutter.new(current_user, request.body)
gemcutter.process
render :text => gemcutter.message, :status => gemcutter.code
end
end
18 changes: 18 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,22 @@ def verify_authenticated_user
render :text => "Access Denied. Please confirm your Gemcutter account.", :status => 403
end
end

def find_gem
@rubygem = Rubygem.find_by_name(params[:id])
if @rubygem.blank?
respond_to do |format|
format.html do
render :file => 'public/404.html'
end
format.json do
render :text => "This rubygem could not be found.", :status => :not_found
end
end
end
end
end

# Make the namespaced controllers happy.
module Api; end
module Api::V1; end
36 changes: 1 addition & 35 deletions app/controllers/rubygems_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
class RubygemsController < ApplicationController
skip_before_filter :verify_authenticity_token, :only => :create
before_filter :authenticate_with_api_key, :only => :create
before_filter :verify_authenticated_user, :only => :create
before_filter :redirect_to_root, :only => [:edit, :update], :unless => :signed_in?
before_filter :find_gem, :only => [:edit, :update, :show]
before_filter :load_gem, :only => [:edit, :update]
Expand All @@ -21,18 +18,7 @@ def index
end

def show
respond_to do |format|
format.html do
@latest_version = @rubygem.versions.latest
end
format.json do
if @rubygem.try(:hosted?)
render :json => @rubygem.to_json
else
render :json => "Not hosted here.", :status => :not_found
end
end
end
@latest_version = @rubygem.versions.latest
end

def edit
Expand All @@ -47,27 +33,7 @@ def update
end
end

def create
gemcutter = Gemcutter.new(current_user, request.body)
gemcutter.process
render :text => gemcutter.message, :status => gemcutter.code
end

protected
def find_gem
@rubygem = Rubygem.find_by_name(params[:id])
if @rubygem.blank?
respond_to do |format|
format.html do
render :file => 'public/404.html'
end
format.json do
render :text => "This rubygem could not be found.", :status => :not_found
end
end
end
end

def load_gem
if !@rubygem.owned_by?(current_user)
flash[:warning] = "You do not have permission to edit this gem."
Expand Down
4 changes: 2 additions & 2 deletions app/views/profiles/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

<pre><code>gemcutter_key: <%= @user.api_key %></code></pre>

<p><%= button_to "Reset my API key", reset_api_key_path,
:method => :put, :confirm => "Are you sure? This cannot be undone." %></p>
<p><%= button_to "Reset my API key", reset_api_v1_api_key_path,
:method => :put, :confirm => "Are you sure? This cannot be undone." %></p>
2 changes: 1 addition & 1 deletion app/views/rubygems/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<strong><%= number_with_delimiter(@rubygem.downloads) %></strong> total downloads
</div>
<div class="downloads">
<strong><%= number_with_delimiter(@latest_version.downloads_count) %></strong> version downloads
<strong><%= number_with_delimiter(@latest_version.downloads_count) %></strong> for this version
</div>
</div>

Expand Down
67 changes: 56 additions & 11 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,63 @@

ActionController::Routing::Routes.draw do |map|

map.json_gem "/gems/:id.json",
:controller => "rubygems",
:action => "show",
:format => "json",
:requirements => { :id => RUBYGEM_NAME_MATCHER }
################################################################################
# API v1

map.resource :dashboard, :only => :show
map.resource :profile
map.resources :statistics, :only => :index, :as => "stats"
map.namespace :api do |api|
api.namespace :v1 do |v1|
v1.resource :api_key,
:only => [:show, :reset],
:member => {:reset => :put}
v1.json_gem "/gems/:id.json",
:controller => "rubygems",
:action => "show",
:format => "json",
:requirements => { :id => RUBYGEM_NAME_MATCHER }
v1.resources :rubygems,
:as => "gems",
:only => [:create] do |rubygems|

rubygems.resource :owners,
:only => [:show, :create, :destroy]
end
end
end

################################################################################
# API v0

map.json_gem "/gems/:id.json",
:controller => "api/v1/rubygems",
:action => "show",
:format => "json",
:requirements => { :id => RUBYGEM_NAME_MATCHER }
map.resource :api_key,
:only => [:show, :reset],
:member => {:reset => :put},
:controller => "api/v1/api_keys"
map.resource :migrate,
:only => [:create, :update],
:controller => "migrations",
:path_prefix => "/gems/:rubygem_id",
:requirements => { :rubygem_id => RUBYGEM_NAME_MATCHER }

################################################################################
# UI

map.search "/search", :controller => "searches", :action => "new"
map.resource :dashboard, :only => :show
map.resource :profile
map.resources :statistics, :only => :index, :as => "stats"

map.resources :rubygems,
:as => "gems",
:except => [:create],
:requirements => { :id => RUBYGEM_NAME_MATCHER } do |rubygems|

rubygems.resource :owners, :only => [:show, :create, :destroy]
rubygems.resource :owners,
:only => [:show, :create, :destroy],
:controller => 'api/v1/owners'

rubygems.resource :subscription, :only => [:create, :destroy]

Expand All @@ -31,8 +67,13 @@
:requirements => { :rubygem_id => RUBYGEM_NAME_MATCHER, :id => RUBYGEM_NAME_MATCHER }
end

map.search "/search", :controller => "searches", :action => "new"
map.resource :api_key, :only => [:show, :reset], :member => {:reset => :put}
map.resources :rubygems,
:as => "gems",
:controller => "api/v1/rubygems",
:only => [:create]

################################################################################
# Clearance

map.sign_up 'sign_up', :controller => 'clearance/users', :action => 'new'
map.sign_in 'sign_in', :controller => 'clearance/sessions', :action => 'new'
Expand All @@ -41,5 +82,9 @@
:action => 'destroy',
:method => :delete

################################################################################
# Root

map.root :controller => "home", :action => "index"

end
2 changes: 1 addition & 1 deletion features/step_definitions/api_steps.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Given /^I have an api key for "([^\"]*)"$/ do |creds|
user, pass = creds.split('/')
basic_auth(user, pass)
visit api_key_path, :get
visit api_v1_api_key_path, :get
@api_key = response.body
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
require 'test_helper'

class ApiKeysControllerTest < ActionController::TestCase
class Api::V1::ApiKeysControllerTest < ActionController::TestCase
#should_route :get, "/api_key", :action => :show
#should_route :put, "/api_key/reset", :action => :reset

should "route old paths to new controller" do
get_route = {:controller => 'api/v1/api_keys', :action => 'show'}
assert_recognizes(get_route, '/api_key')
assert_recognizes(get_route, '/api/v1/api_key')

put_route = {:controller => 'api/v1/api_keys', :action => 'reset'}
assert_recognizes(put_route, :path => '/api_key/reset', :method => :put)
assert_recognizes(put_route, :path => '/api/v1/api_key/reset', :method => :put)
end

context "on GET to show with no credentials" do
setup do
get :show
Expand Down
30 changes: 30 additions & 0 deletions test/functional/api/v1/owners_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'test_helper'

class Api::V1::OwnersControllerTest < ActionController::TestCase
should "route GET to new controller" do
route = {:controller => 'api/v1/owners',
:action => 'show',
:rubygem_id => "rails",
:format => "json"}
assert_recognizes(route, '/gems/rails/owners.json')
assert_recognizes(route, '/api/v1/gems/rails/owners.json')
end

should "route POST to new controller" do
route = {:controller => 'api/v1/owners',
:action => 'create',
:rubygem_id => "rails",
:format => "json"}
assert_recognizes(route, :path => '/gems/rails/owners.json', :method => :post)
assert_recognizes(route, :path => '/api/v1/gems/rails/owners.json', :method => :post)
end

should "route DELETE to new controller" do
route = {:controller => 'api/v1/owners',
:action => 'destroy',
:rubygem_id => "rails",
:format => "json"}
assert_recognizes(route, :path => '/gems/rails/owners.json', :method => :delete)
assert_recognizes(route, :path => '/api/v1/gems/rails/owners.json', :method => :delete)
end
end
Loading

0 comments on commit b5ee35b

Please sign in to comment.