From 36575e0b70c9f5fafabce58ce57b99a034e82034 Mon Sep 17 00:00:00 2001 From: Osamu Takiya Date: Mon, 17 Jul 2023 02:59:41 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Nickname=20API=20?= =?UTF-8?q?=E3=82=92=E5=AE=9F=E8=A3=85=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/v1/nicknames_controller.rb | 19 ++++++++++ config/routes.rb | 1 + spec/requests/api/v1/nicknames_spec.rb | 35 +++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 app/controllers/api/v1/nicknames_controller.rb create mode 100644 spec/requests/api/v1/nicknames_spec.rb diff --git a/app/controllers/api/v1/nicknames_controller.rb b/app/controllers/api/v1/nicknames_controller.rb new file mode 100644 index 0000000..a383b00 --- /dev/null +++ b/app/controllers/api/v1/nicknames_controller.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 6482a42..f3b00e1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/requests/api/v1/nicknames_spec.rb b/spec/requests/api/v1/nicknames_spec.rb new file mode 100644 index 0000000..76f407a --- /dev/null +++ b/spec/requests/api/v1/nicknames_spec.rb @@ -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