From 128a5e410f4c51bbbcb1540435d3bea0b9a9c04d Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Wed, 19 Dec 2018 16:16:36 +0100 Subject: Expose method that returns GitLab API paths --- lib/api/api.rb | 8 ++++++++ spec/lib/api/api_spec.rb | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 spec/lib/api/api_spec.rb diff --git a/lib/api/api.rb b/lib/api/api.rb index 19da0b2c434..f9bb1201472 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -10,6 +10,14 @@ module API NAMESPACE_OR_PROJECT_REQUIREMENTS = { id: NO_SLASH_URL_PART_REGEX }.freeze COMMIT_ENDPOINT_REQUIREMENTS = NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(sha: NO_SLASH_URL_PART_REGEX).freeze + def self.root_path(version: 'v4') + unless versions.include?(version) + raise ArgumentError, 'Unknown API version!' + end + + File.join('/', prefix.to_s, version.to_s) + end + insert_before Grape::Middleware::Error, GrapeLogging::Middleware::RequestLogger, logger: Logger.new(LOG_FILENAME), diff --git a/spec/lib/api/api_spec.rb b/spec/lib/api/api_spec.rb new file mode 100644 index 00000000000..31881551980 --- /dev/null +++ b/spec/lib/api/api_spec.rb @@ -0,0 +1,36 @@ +require 'spec_helper' + +describe API::API do + describe '.prefix' do + it 'has a prefix defined' do + expect(described_class.prefix).to eq :api + end + end + + describe '.version' do + it 'uses most recent version of the API' do + expect(described_class.version).to eq 'v4' + end + end + + describe '.versions' do + it 'returns all available versions' do + expect(described_class.versions).to eq ['v3', 'v4'] + end + end + + describe '.root_path' do + it 'returns predefined API version path' do + expect(described_class.root_path).to eq '/api/v4' + end + + it 'returns a version provided as keyword argument' do + expect(described_class.root_path(version: 'v3')).to eq '/api/v3' + end + + it 'raises an error if version is not known' do + expect { described_class.root_path(version: 'v10') } + .to raise_error ArgumentError + end + end +end -- cgit v1.2.1