Class: ElastomerClient::Client::RestApiSpec::ApiSpec
- Inherits:
-
Object
- Object
- ElastomerClient::Client::RestApiSpec::ApiSpec
- Defined in:
- lib/elastomer_client/client/rest_api_spec/api_spec.rb
Overview
This is the superclass for the version specific API Spec classes that will be generated using the ‘script/generate-rest-api-spec` script. Each version of Elasticsarch we support will have it’s own ApiSpec class that will validate the API request params for that particular version.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#common_params ⇒ Object
readonly
Returns the value of attribute common_params.
-
#rest_apis ⇒ Object
readonly
Returns the value of attribute rest_apis.
Instance Method Summary collapse
-
#get(api) ⇒ Object
Internal: Retrieve the ‘RestApi` descriptor for the given named `api`.
-
#initialize ⇒ ApiSpec
constructor
A new instance of ApiSpec.
-
#select_common_params(from:) ⇒ Object
Select the common request parameters from the given params.
-
#select_params(api:, from:) ⇒ Object
Given an API descriptor name and a set of request parameters, select those params that are accepted by the API endpoint.
-
#select_parts(api:, from:) ⇒ Object
Given an API descriptor name and a set of request path parts, select those parts that are accepted by the API endpoint.
-
#valid_common_param?(param) ⇒ Boolean
Returns ‘true` if the param is a common request parameter.
-
#valid_param?(api:, param:) ⇒ Boolean
Given an API descriptor name and a single request parameter, returns ‘true` if the parameter is valid for the given API.
-
#valid_part?(api:, part:) ⇒ Boolean
Given an API descriptor name and a single path part, returns ‘true` if the path part is valid for the given API.
-
#validate_params!(api:, params:) ⇒ Object
Given an API descriptor name and a set of request parameters, ensure that all the request parameters are valid for the API endpoint.
Constructor Details
#initialize ⇒ ApiSpec
Returns a new instance of ApiSpec.
14 15 16 17 18 |
# File 'lib/elastomer_client/client/rest_api_spec/api_spec.rb', line 14 def initialize @rest_apis ||= {} @common_params ||= {} @common_params_set = Set.new(@common_params.keys) end |
Instance Attribute Details
#common_params ⇒ Object (readonly)
Returns the value of attribute common_params.
12 13 14 |
# File 'lib/elastomer_client/client/rest_api_spec/api_spec.rb', line 12 def common_params @common_params end |
#rest_apis ⇒ Object (readonly)
Returns the value of attribute rest_apis.
11 12 13 |
# File 'lib/elastomer_client/client/rest_api_spec/api_spec.rb', line 11 def rest_apis @rest_apis end |
Instance Method Details
#get(api) ⇒ Object
Internal: Retrieve the ‘RestApi` descriptor for the given named `api`. If an unkonwn `api` is passed in, then `nil` is returned.
api - the api descriptor name as a String
Returns a RestApi instance or nil.
116 117 118 |
# File 'lib/elastomer_client/client/rest_api_spec/api_spec.rb', line 116 def get(api) rest_apis[api] end |
#select_common_params(from:) ⇒ Object
Select the common request parameters from the given params.
from - the Hash containing the request params
Returns a new Hash containing the valid common request params
79 80 81 82 |
# File 'lib/elastomer_client/client/rest_api_spec/api_spec.rb', line 79 def select_common_params(from:) return from if @common_params.empty? from.select { |k, v| valid_common_param?(k) } end |
#select_params(api:, from:) ⇒ Object
Given an API descriptor name and a set of request parameters, select those params that are accepted by the API endpoint.
api - the api descriptor name as a String from - the Hash containing the request params
Returns a new Hash containing the valid params for the api
27 28 29 30 31 |
# File 'lib/elastomer_client/client/rest_api_spec/api_spec.rb', line 27 def select_params(api:, from:) rest_api = get(api) return from if rest_api.nil? rest_api.select_params(from:) end |
#select_parts(api:, from:) ⇒ Object
Given an API descriptor name and a set of request path parts, select those parts that are accepted by the API endpoint.
api - the api descriptor name as a String from - the Hash containing the path parts
Returns a new Hash containing the valid path parts for the api
54 55 56 57 58 |
# File 'lib/elastomer_client/client/rest_api_spec/api_spec.rb', line 54 def select_parts(api:, from:) rest_api = get(api) return from if rest_api.nil? rest_api.select_parts(from:) end |
#valid_common_param?(param) ⇒ Boolean
Returns ‘true` if the param is a common request parameter.
85 86 87 |
# File 'lib/elastomer_client/client/rest_api_spec/api_spec.rb', line 85 def valid_common_param?(param) @common_params_set.include?(param.to_s) end |
#valid_param?(api:, param:) ⇒ Boolean
Given an API descriptor name and a single request parameter, returns ‘true` if the parameter is valid for the given API. This method always returns `true` if the API is unknown.
api - the api descriptor name as a String param - the request parameter name as a String
Returns ‘true` if the param is valid for the API.
41 42 43 44 45 |
# File 'lib/elastomer_client/client/rest_api_spec/api_spec.rb', line 41 def valid_param?(api:, param:) rest_api = get(api) return true if rest_api.nil? rest_api.valid_param?(param) end |
#valid_part?(api:, part:) ⇒ Boolean
Given an API descriptor name and a single path part, returns ‘true` if the path part is valid for the given API. This method always returns `true` if the API is unknown.
api - the api descriptor name as a String part - the path part name as a String
Returns ‘true` if the path part is valid for the API.
68 69 70 71 72 |
# File 'lib/elastomer_client/client/rest_api_spec/api_spec.rb', line 68 def valid_part?(api:, part:) rest_api = get(api) return true if rest_api.nil? rest_api.valid_part?(part) end |
#validate_params!(api:, params:) ⇒ Object
Given an API descriptor name and a set of request parameters, ensure that all the request parameters are valid for the API endpoint. If an invalid parameter is found then an IllegalArgument exception is raised.
api - the api descriptor name as a String from - the Hash containing the request params
Returns the params unmodified Raises an IllegalArgument exception if an invalid parameter is found.
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/elastomer_client/client/rest_api_spec/api_spec.rb', line 98 def validate_params!(api:, params:) rest_api = get(api) return params if rest_api.nil? params.keys.each do |key| unless rest_api.valid_param?(key) || valid_common_param?(key) raise ::ElastomerClient::Client::IllegalArgument, "'#{key}' is not a valid parameter for the '#{api}' API" end end params end |