Project

General

Profile

« Previous | Next » 

Revision 3d6d01c7

Added by Ohad Levy almost 12 years ago

  • ID 3d6d01c70e5a48e92f7beb16e98096aeae14cb0c

fixes #1775 - API versioning name space

View differences:

Gemfile
gem 'net-ldap'
gem "safemode", "~> 1.0.1"
gem 'uuidtools'
gem 'rabl'
# Previous versions collide with Environment model
gem "ruby_parser", ">= 2.3.1"
app/controllers/api/base_controller.rb
module Api
#TODO: inherit from application controller after cleanup
class BaseController < ActionController::Base
protected
# searches for an object based on its name and assign it to an instance variable
# required for models which implement the to_param method
#
# example:
# @host = Host.find_by_name params[:id]
def find_by_name
not_found and return if (id = params[:id]).blank?
obj = controller_name.singularize
# determine if we are searching for a numerical id or plain name
cond = "find_by_" + ((id =~ /^\d+$/ && (id=id.to_i)) ? "id" : "name")
not_found and return unless eval("@#{obj} = #{obj.camelize}.#{cond}(id)")
end
def not_found(exception = nil)
logger.debug "not found: #{exception}" if exception
head :status => 404
end
end
end
app/controllers/api/v1/base_controller.rb
module Api
module V1
class BaseController < Api::BaseController
respond_to :json
end
end
end
app/controllers/api/v1/bookmarks_controller.rb
module Api
module V1
class BookmarksController < BaseController
before_filter :find_by_name, :only => [:show, :update, :destroy]
def index
@bookmarks = Bookmark.paginate(:page => params[:page])
end
def show
end
def create
respond_with Bookmark.create(params[:bookmark])
end
def update
respond_with @bookmark.update_attributes(params[:bookmark])
end
def destroy
respond_with @bookmark.destroy
end
end
end
end
app/controllers/api/v1/home_controller.rb
module Api
module V1
class HomeController < BaseController
def index
end
def status
end
end
end
end
app/views/api/v1/bookmarks/index.json.rabl
collection @bookmarks
extends "api/v1/bookmarks/show"
app/views/api/v1/bookmarks/show.json.rabl
object @bookmark
attributes :name, :controller, :query, :public, :id
child :owner do
attributes :name
attributes :login
attributes :mail, :as => :email
end
app/views/api/v1/home/index.json.rabl
object false
child(:links => "links") do
node(:bookmarks) { api_bookmarks_path }
node(:status) { api_status_path }
end
app/views/api/v1/home/status.json.rabl
object false
node(:result) {"ok"}
node(:status) {200}
node(:version) {SETTINGS[:version]}
node(:api_version) {1}
config/routes.rb
require 'api_constraints'
Foreman::Application.routes.draw do
#ENC requests goes here
match "node/:name" => 'hosts#externalNodes', :constraints => { :name => /[^\.][\w\.-]+/ }
......
resources :tasks, :only => [:show]
#Keep this line the last route
#### API Namespace from here downwards ####
namespace :api, :defaults => {:format => 'json'} do
scope :module => :v1, :constraints => ApiConstraints.new(:version => 1, :default => true) do
resources :bookmarks, :except => [:new, :edit]
match '/', :to => 'home#index'
match 'status', :to => 'home#status', :as => "status"
end
# scope module: :v2, constraints: ApiConstraints.new(version: 2, default: true) do
# resources :bookmarks
# end
end
#Keep this line the last route
match '*a', :to => 'errors#routing'
end
lib/api_constraints.rb
class ApiConstraints
def initialize(options)
@version = options[:version]
@default = options[:default]
end
def matches?(req)
@default || req.headers['Accept'].each {|h| return true if h.grep("version=#{@version}")}
end
end

Also available in: Unified diff