Compare commits

...

2 Commits

Author SHA1 Message Date
  huang 6d68f96f39 V2 10 years ago
  huang ac8034ebc4 获取user的json数据 10 years ago
16 changed files with 93 additions and 0 deletions
Split View
  1. +14
    -0
      app/controllers/api/v1/base_controller.rb
  2. +8
    -0
      app/controllers/api/v1/users_controller.rb
  3. +15
    -0
      app/controllers/api/v2/base_controller.rb
  4. +9
    -0
      app/controllers/api/v2/users_controller.rb
  5. +2
    -0
      app/helpers/api/v1/base_helper.rb
  6. +2
    -0
      app/helpers/api/v1/users_helper.rb
  7. +2
    -0
      app/helpers/api/v2/base_helper.rb
  8. +2
    -0
      app/helpers/api/v2/users_helper.rb
  9. +5
    -0
      app/views/api/v1/users/show.json.jbuilder
  10. +3
    -0
      app/views/api/v2/users/show.json.jbuilder
  11. +7
    -0
      config/routes.rb
  12. +4
    -0
      db/migrate/20160307071054_add_authentication_token_to_users.rb
  13. +5
    -0
      spec/controllers/api/v1/base_controller_spec.rb
  14. +5
    -0
      spec/controllers/api/v1/users_controller_spec.rb
  15. +5
    -0
      spec/controllers/api/v2/base_controller_spec.rb
  16. +5
    -0
      spec/controllers/api/v2/users_controller_spec.rb

+ 14
- 0
app/controllers/api/v1/base_controller.rb View File

@@ -0,0 +1,14 @@
class Api::V1::BaseController < ApplicationController
# disable the CSRF token
protect_from_forgery with: :null_session

# disable cookies (no set-cookies header in response)
before_action :destroy_session

# disable the CSRF token
skip_before_action :verify_authenticity_token

def destroy_session
request.session_options[:skip] = true
end
end

+ 8
- 0
app/controllers/api/v1/users_controller.rb View File

@@ -0,0 +1,8 @@
class Api::V1::UsersController < ApplicationController
def show
@user = User.find(params[:id])

# 我们现在使用 app/views/api/v1/users/show.json.jbuilder
# render(json: Api::V1::UserSerializer.new(user).to_json)
end
end

+ 15
- 0
app/controllers/api/v2/base_controller.rb View File

@@ -0,0 +1,15 @@
class Api::V2::BaseController < ApplicationController
# disable the CSRF token
protect_from_forgery with: :null_session

# disable cookies (no set-cookies header in response)
before_action :destroy_session

# disable the CSRF token
skip_before_action :verify_authenticity_token

def destroy_session
request.session_options[:skip] = true
end

end

+ 9
- 0
app/controllers/api/v2/users_controller.rb View File

@@ -0,0 +1,9 @@
class Api::V2::UsersController < ApplicationController
def show
@user = User.find(params[:id])

# 原文使用 Api::V1::UserSerializer
# 我们现在使用 app/views/api/v1/users/show.json.jbuilder
# render(json: Api::V1::UserSerializer.new(user).to_json)
end
end

+ 2
- 0
app/helpers/api/v1/base_helper.rb View File

@@ -0,0 +1,2 @@
module Api::V1::BaseHelper
end

+ 2
- 0
app/helpers/api/v1/users_helper.rb View File

@@ -0,0 +1,2 @@
module Api::V1::UsersHelper
end

+ 2
- 0
app/helpers/api/v2/base_helper.rb View File

@@ -0,0 +1,2 @@
module Api::V2::BaseHelper
end

+ 2
- 0
app/helpers/api/v2/users_helper.rb View File

@@ -0,0 +1,2 @@
module Api::V2::UsersHelper
end

+ 5
- 0
app/views/api/v1/users/show.json.jbuilder View File

@@ -0,0 +1,5 @@
# JsonBuilder将对象动态转为json
json.user do
# status: 用户状态(eg:激活、锁定)
json.(@user, :id, :mail, :login, :first_name, :last_name, :status, :admin, :created_at, :updated_at)
end

+ 3
- 0
app/views/api/v2/users/show.json.jbuilder View File

@@ -0,0 +1,3 @@
json.user do
json.(@user, :id, :email, :name, :activated, :admin, :created_at, :updated_at)
end

+ 7
- 0
config/routes.rb View File

@@ -28,6 +28,13 @@
RedmineApp::Application.routes.draw do
mount Mobile::API => '/api'
namespace :api do
namespace :v2 do
resources :users, only: [:index, :create, :show, :update, :destroy]
# 原文有 microposts, 我们现在把它注释掉
# resources :microposts, only: [:index, :create, :show, :update, :destroy]
end
end
# Enable Grack support
# mount Trustie::Grack.new, at: '/', constraints: lambda { |request| /[-\/\w\.]+\.git\//.match(request.path_info) }, via: [:get, :post]


+ 4
- 0
db/migrate/20160307071054_add_authentication_token_to_users.rb View File

@@ -0,0 +1,4 @@
class AddAuthenticationTokenToUsers < ActiveRecord::Migration
def change
end
end

+ 5
- 0
spec/controllers/api/v1/base_controller_spec.rb View File

@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Api::V1::BaseController, :type => :controller do

end

+ 5
- 0
spec/controllers/api/v1/users_controller_spec.rb View File

@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Api::V1::UsersController, :type => :controller do

end

+ 5
- 0
spec/controllers/api/v2/base_controller_spec.rb View File

@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Api::V2::BaseController, :type => :controller do

end

+ 5
- 0
spec/controllers/api/v2/users_controller_spec.rb View File

@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Api::V2::UsersController, :type => :controller do

end

Loading…
Cancel
Save