Skip to content

Commit 2782332

Browse files
committed
Add basic index and show api controllers
1 parent 3f7dd06 commit 2782332

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Api::V1::RepositoriesController < ApplicationController
2+
3+
# GET /api/v1/repositories
4+
def index
5+
repositories = Repository.all
6+
7+
render json: repositories
8+
end
9+
10+
# GET /api/v1/repositories/1
11+
def show
12+
repository = Repository.find(params[:id].to_i)
13+
render json: repository
14+
end
15+
16+
private
17+
# Only allow a trusted parameter "white list" through.
18+
def repository_params
19+
params.fetch(:repository, {})
20+
end
21+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Api::V1::UsersController < ApplicationController
2+
3+
# GET /api/v1/users
4+
def index
5+
users = User.all
6+
7+
render json: users
8+
end
9+
10+
# GET /api/v1/users/1
11+
def show
12+
user = User.find(params[:id].to_i)
13+
render json: user
14+
end
15+
16+
private
17+
# Only allow a trusted parameter "white list" through.
18+
def user_params
19+
params.fetch(:user, {})
20+
end
21+
end

config/routes.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
Rails.application.routes.draw do
2+
namespace :api do
3+
namespace :v1 do
4+
resources :users, only: [:show, :index]
5+
resources :repositories, only: [:show, :index]
6+
end
7+
end
28
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
39
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe Api::V1::RepositoriesController, type: :controller do
4+
let(:valid_attributes) {
5+
{ name: SecureRandom.urlsafe_base64(5) }
6+
}
7+
8+
# this should return the minimal set of values that should be in the session
9+
# in order to pass any filters (e.g. authentication) defined in
10+
# api::v1::repositoryscontroller. be sure to keep this updated too.
11+
let(:valid_session) { {} }
12+
13+
describe "get #index" do
14+
it "returns a success response" do
15+
10.times { Repository.create! valid_attributes }
16+
get :index, params: {}, session: valid_session
17+
expect(response).to be_successful
18+
end
19+
end
20+
21+
describe "get #show" do
22+
it "returns a success response" do
23+
repository = Repository.create! valid_attributes.merge(id:1)
24+
get :show, params: {id: 1}, session: valid_session
25+
expect(response).to be_successful
26+
end
27+
end
28+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe Api::V1::UsersController, type: :controller do
4+
let(:valid_attributes) {
5+
{ login: SecureRandom.urlsafe_base64(5) }
6+
}
7+
8+
# this should return the minimal set of values that should be in the session
9+
# in order to pass any filters (e.g. authentication) defined in
10+
# api::v1::userscontroller. be sure to keep this updated too.
11+
let(:valid_session) { {} }
12+
13+
describe "get #index" do
14+
it "returns a success response" do
15+
10.times { User.create! valid_attributes }
16+
get :index, params: {}, session: valid_session
17+
expect(response).to be_successful
18+
end
19+
end
20+
21+
describe "get #show" do
22+
it "returns a success response" do
23+
user = User.create! valid_attributes.merge(id:1)
24+
get :show, params: {id: 1}, session: valid_session
25+
expect(response).to be_successful
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)