-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathrest_api_spec.rb
45 lines (39 loc) · 1.51 KB
/
rest_api_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true
module ElastomerClient
class Client
# Provides access to the versioned REST API specs for Elasticsearch.
module RestApiSpec
# Returns an ApiSpec instance for the given Elasticsearcion version. This
# method will load the ApiSpec version class if it has not already been
# defined. This prevents bloat by only loading the version specs that are
# needed.
#
# Because of this lazy loading, this method is _not_ thread safe.
#
# version - the Elasticsearch version String
#
# Returns the requested ApiSpec version if available
def self.api_spec(version)
classname = "ApiSpecV#{to_class_version(version)}"
load_api_spec(version) if !self.const_defined? classname
self.const_get(classname).new
end
# Internal: Load the specific ApiSpec version class for the given version.
def self.load_api_spec(version)
path = File.expand_path("../rest_api_spec/api_spec_v#{to_class_version(version)}.rb", __FILE__)
if File.exist? path
load path
else
raise RuntimeError, "Unsupported REST API spec version: #{version}"
end
end
# Internal: Convert a dotted version String into an underscore format
# suitable for use in Ruby class names.
def self.to_class_version(version)
version.to_s.split(".").slice(0, 2).join("_")
end
end
end
end
require_relative "rest_api_spec/api_spec"
require_relative "rest_api_spec/rest_api"