summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-12-19 16:16:36 +0100
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2019-01-03 14:18:05 +0100
commit128a5e410f4c51bbbcb1540435d3bea0b9a9c04d (patch)
tree4e640ff331f6af1814c39e3fcdc09cb398b690f4
parent32fbc12cc532f15392a4c4fa08b6229b06fe5bf0 (diff)
downloadgitlab-ce-128a5e410f4c51bbbcb1540435d3bea0b9a9c04d.tar.gz
Expose method that returns GitLab API paths
-rw-r--r--lib/api/api.rb8
-rw-r--r--spec/lib/api/api_spec.rb36
2 files changed, 44 insertions, 0 deletions
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