Add `GET /api/v1/instance/languages` to REST API (#24443)
Co-authored-by: Eugen Rochko <eugen@zeonfederated.com> Co-authored-by: Claire <claire.github-309c@sitedethib.com>th-downstream
parent
5ec0d5faa9
commit
788d7a6a2a
@ -0,0 +1,21 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Api::V1::Instances::LanguagesController < Api::BaseController
|
||||
skip_before_action :require_authenticated_user!, unless: :limited_federation_mode?
|
||||
skip_around_action :set_locale
|
||||
|
||||
before_action :set_languages
|
||||
|
||||
vary_by ''
|
||||
|
||||
def show
|
||||
cache_even_if_authenticated!
|
||||
render json: @languages, each_serializer: REST::LanguageSerializer
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_languages
|
||||
@languages = LanguagesHelper::SUPPORTED_LOCALES.keys.map { |code| LanguagePresenter.new(code) }
|
||||
end
|
||||
end
|
@ -0,0 +1,20 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class LanguagePresenter < ActiveModelSerializers::Model
|
||||
attributes :code, :name, :native_name
|
||||
|
||||
def initialize(code)
|
||||
super()
|
||||
|
||||
@code = code
|
||||
@item = LanguagesHelper::SUPPORTED_LOCALES[code]
|
||||
end
|
||||
|
||||
def name
|
||||
@item[0]
|
||||
end
|
||||
|
||||
def native_name
|
||||
@item[1]
|
||||
end
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class REST::LanguageSerializer < ActiveModel::Serializer
|
||||
attributes :code, :name
|
||||
end
|
@ -0,0 +1,19 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Languages' do
|
||||
describe 'GET /api/v1/instance/languages' do
|
||||
before do
|
||||
get '/api/v1/instance/languages'
|
||||
end
|
||||
|
||||
it 'returns http success' do
|
||||
expect(response).to have_http_status(200)
|
||||
end
|
||||
|
||||
it 'returns the supported languages' do
|
||||
expect(body_as_json.pluck(:code)).to match_array LanguagesHelper::SUPPORTED_LOCALES.keys.map(&:to_s)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in new issue