Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 Nickname API を実装した #87

Merged
merged 1 commit into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/controllers/api/v1/nicknames_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Api
module V1
class NicknamesController < ApplicationController
def index
render json: Nickname.pluck(:name)
end

def show
nickname = Nickname.find_by(name: params[:name])

if nickname.blank?
render json: []
else
render json: nickname.gss_characters.pluck(:name)
end
end
end
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace :api, { format: 'json' } do
namespace :v1 do
resources :stars, only: [:index]
resources :nicknames, only: %i[index show], param: :name
end
end
end
35 changes: 35 additions & 0 deletions spec/requests/api/v1/nicknames_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'rails_helper'

RSpec.describe 'Api::V1::Nicknames', type: :request do
describe 'GET /index' do
it 'returns http success' do
get '/api/v1/nicknames'

expect(response).to have_http_status(:success)
expect(Nickname.count).to be > 500
end
end

describe 'GET /show' do
it 'returns http success' do
# 坊ちゃん
get '/api/v1/nicknames/%E5%9D%8A%E3%81%A1%E3%82%83%E3%82%93'

expect(response).to have_http_status(:success)
end

it 'returns expected contents' do
# 坊ちゃん
get '/api/v1/nicknames/%E5%9D%8A%E3%81%A1%E3%82%83%E3%82%93'

expect(response.body).to eq '["幻水1主人公(坊ちゃん)"]'
end

it 'Nickname が存在しない場合は空っぽのレスポンスであること' do
get '/api/v1/nicknames/hogehoge'

expect(response).to have_http_status(:success)
expect(response.body).to eq '[]'
end
end
end