11. apps/controllers/api/messages_controller.rb
class Api::MessagesController < ApplicationController
# GET /api/messages.json
def index
@messages = Message.all
respond_to do |format|
format.json { render json: @messages }
end
end
# GET /api/messages/1.json
def show
@message = Message.find(params[:id])
respond_to do |format|
format.json { render json: @message }
end
end
end
13. Start Alone but
Test is Your Partner
rake test:functionals
rake test:units
rake test:integration
14. test/functional/api/messages_controller_test.rb
class Api::MessagesControllerTest < ActionController::TestCase
setup do
@message = messages(:one)
end
test "should get index" do
get :index, format: :json
assert_response :success
assert_not_nil assigns(:messages)
end
test "should show message" do
get :show, format: :json, id: @message.to_param
assert_response :success
end
end
16. built-in Namespaced
controllers
http://localhost:3000/api/v2/messages
Rocweibo::Application.routes.draw do
namespace :api do
resources :messages
namespace :v2 do
resources :messages
end
end
end
17. test/functional/api/v2/messages_controller_test.rb
class Api::V2::MessagesController < Api::ApplicationController
before_filter :authenticate, :only => :create
...
# curl -X POST -H "Accept: application/json" --user shaokun.wu@gmail.com:a
# http://localhost:3000/api/v2/messages -d "message[body]=abcdefg"
def create
@message = Message.new(params[:message])
@message.user = @current_user
@message.save!
render json: @message
end
private
def authenticate
if user = authenticate_with_http_basic { |u, p| User.authenticate(u, p) }
@current_user = user
else
request_http_basic_authentication
end
end
end
19. class Api::V2::MessagesController < Api::ApplicationController
...
private
def authenticate
if user = authenticate_with_http_basic { |u, p| User.authenticate(u, p) }
@current_user = user
else
request_http_basic_authentication
end
end
end
class Api::ApplicationController < ActionController::Base
private
def authenticate
if user = authenticate_with_http_basic { |u, p| User.authenticate(u, p) }
@current_user = user
else
request_http_basic_authentication
end
end
end
20. /app/controllers/api/application_controller.rb
class Api::ApplicationController < ActionController::Base
end
/app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
helper_method :current_user, :logged_in?
protect_from_forgery
before_filter :require_logged_in
...
end
22. class Rocweibo::V1::API < Grape::API
prefix 'api'
version 'v1'
resource :messages do
get do
Message.limit(20)
end
get ':id' do
Message.find(params[:id])
end
end
end
Rocweibo::Application.routes.draw do
mount Rocweibo::V1::API => "/" # mount API routes
...
end
30. • Display database tables
• Create new data
• Easily update data
• Safely delete data
• Automatic form validation
• Search and filtering
• Export data to CSV/JSON/XML
• Authentication (via Devise)
• User action history
31. • add the line below to your Gemfile
gem 'rails_admin', :git => 'git://github.com/sferik/rails_admin.git'
• $ bundle install
• $ rails g rails_admin:install
36. def index
@messages = Rails.cache.fetch('all_messages', :expires_in => 30.seconds) do
Message.all
end
respond_to do |format|
format.json { render json: @messages }
end
end
37. Started GET "/api/v2/messages.json" for 127.0.0.1 at 2011-09-15 17:29:53 +0800
Processing by Api::V2::MessagesController#index as JSON
Message Load (0.1ms) SELECT "messages".* FROM "messages"
Completed 200 OK in 46ms (Views: 11.4ms | ActiveRecord: 0.4ms)
Started GET "/api/v2/messages.json" for 127.0.0.1 at 2011-09-15 17:29:58 +0800
Processing by Api::V2::MessagesController#index as JSON
Completed 200 OK in 7ms (Views: 6.1ms | ActiveRecord: 0.2ms)
39. Rocweibo::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# Code is not reloaded between requests
config.cache_classes = true
...
# Use a different cache store in production
# config.cache_store = :mem_cache_store
...
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
end