Class: Elastomer::Client::RestApiSpec::ApiSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/elastomer/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 aprams for that particular version.

Direct Known Subclasses

ApiSpecV2_3, ApiSpecV2_4, ApiSpecV5_6

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApiSpec

Returns a new instance of ApiSpec.



13
14
15
16
17
# File 'lib/elastomer/client/rest_api_spec/api_spec.rb', line 13

def initialize
  @rest_apis ||= {}
  @common_params ||= {}
  @common_params_set = Set.new(@common_params.keys)
end

Instance Attribute Details

#common_paramsObject (readonly)

Returns the value of attribute common_params.



11
12
13
# File 'lib/elastomer/client/rest_api_spec/api_spec.rb', line 11

def common_params
  @common_params
end

#rest_apisObject (readonly)

Returns the value of attribute rest_apis.



10
11
12
# File 'lib/elastomer/client/rest_api_spec/api_spec.rb', line 10

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.



115
116
117
# File 'lib/elastomer/client/rest_api_spec/api_spec.rb', line 115

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



78
79
80
81
# File 'lib/elastomer/client/rest_api_spec/api_spec.rb', line 78

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



26
27
28
29
30
# File 'lib/elastomer/client/rest_api_spec/api_spec.rb', line 26

def select_params(api:, from:)
  rest_api = get(api)
  return from if rest_api.nil?
  rest_api.select_params(from: 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



53
54
55
56
57
# File 'lib/elastomer/client/rest_api_spec/api_spec.rb', line 53

def select_parts(api:, from:)
  rest_api = get(api)
  return from if rest_api.nil?
  rest_api.select_parts(from: from)
end

#valid_common_param?(param) ⇒ Boolean

Returns ‘true` if the param is a common request parameter.

Returns:

  • (Boolean)


84
85
86
# File 'lib/elastomer/client/rest_api_spec/api_spec.rb', line 84

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.

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/elastomer/client/rest_api_spec/api_spec.rb', line 40

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.

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/elastomer/client/rest_api_spec/api_spec.rb', line 67

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.



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/elastomer/client/rest_api_spec/api_spec.rb', line 97

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 ::Elastomer::Client::IllegalArgument, "'#{key}' is not a valid parameter for the '#{api}' API"
    end
  end
  params
end