Skip to content

Commit

Permalink
Added versions API and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
louispt1 authored and noracato committed Dec 17, 2024
1 parent 106732d commit 31e0e3d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/controllers/api/v1/versions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Api
module V1
class VersionsController < BaseController

def index
versions = Version.all
base_url = request.base_url

version_data = versions.map do |version|
{
version: version,
url: version_url(version, base_url)
}
end

render json: { versions: version_data }, status: :ok
end

private

# Generates a version URL given a version name and the base URL
def version_url(version, base_url)
"https://#{version}.#{base_url}"
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 @@ -84,6 +84,7 @@
resources :collections, only: [:index, :show, :create, :destroy]
resources :saved_scenarios, only: %i[index show create update destroy]
resources :featured_scenarios, only: %i[index show]
resources :versions, only: [:index]
end
end

Expand Down
26 changes: 26 additions & 0 deletions spec/controllers/api/v1/versions_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'rails_helper'

RSpec.describe 'API::V1::Versions', type: :request do
let(:user) { create(:user) }

describe 'GET /api/v1/versions' do
before do
get '/api/v1/versions',
as: :json,
headers: access_token_header(user, :read)
end

it 'returns a successful response' do
expect(response).to have_http_status(:ok)
end

it 'returns all versions with their URLs' do
parsed_response = JSON.parse(response.body)

expect(parsed_response['versions']).to be_present
expect(parsed_response['versions'].map { |v| v['version'] }).to match_array(Version.all)
expect(parsed_response['versions'].first).to have_key('url')
puts parsed_response
end
end
end

0 comments on commit 31e0e3d

Please sign in to comment.