Module: Restspec::RSpec::ApiHelpers

Defined in:
lib/restspec/rspec/api_helpers.rb

Instance Method Summary collapse

Instance Method Details

#call_endpoint(endpoint_full_name = nil, body: {}, url_params: {}, query_params: {}, merge_example_params: true, execution_method: :execute) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/restspec/rspec/api_helpers.rb', line 10

def call_endpoint(endpoint_full_name = nil, body: {},
                                      url_params: {},
                                      query_params: {},
                                      merge_example_params: true,
                                      execution_method: :execute)
  endpoint = find_endpoint_in_test_context(endpoint_full_name)

  if merge_example_params
    query_params = (@query_params || {}).merge(query_params)
    url_params = (@url_params || {}).merge(url_params)
  end

  endpoint.send(execution_method, body: body, url_params: url_params, query_params: query_params)
end

#call_endpoint_once(endpoint_full_name = nil, options = {}) ⇒ Object



42
43
44
# File 'lib/restspec/rspec/api_helpers.rb', line 42

def call_endpoint_once(endpoint_full_name = nil, options = {})
  call_endpoint(endpoint_full_name, options.merge(:execution_method => :execute_once))
end

#execute_endpoint!Object



46
47
48
49
# File 'lib/restspec/rspec/api_helpers.rb', line 46

def execute_endpoint!
  execute_endpoint_lambda.call
  response
end

#find_endpoint_in_test_context(endpoint_full_name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/restspec/rspec/api_helpers.rb', line 25

def find_endpoint_in_test_context(endpoint_full_name)
  if endpoint_full_name.present?
    test_context = self.class

    test_context.[:endpoints] ||= {}
    test_context.[:endpoints][endpoint_full_name] ||= begin
      Restspec::EndpointStore.get(endpoint_full_name)
    end
  else
    endpoint
  end
end

#read_endpoint(endpoint_full_name = nil, options = {}) ⇒ Object



6
7
8
# File 'lib/restspec/rspec/api_helpers.rb', line 6

def read_endpoint(endpoint_full_name = nil, options = {})
  call_endpoint(endpoint_full_name, options).read_body
end

#read_endpoint_once(endpoint_full_name = nil, options = {}) ⇒ Object



38
39
40
# File 'lib/restspec/rspec/api_helpers.rb', line 38

def read_endpoint_once(endpoint_full_name = nil, options = {})
  call_endpoint_once(endpoint_full_name, options).read_body
end

#schema_example(schema_name = nil) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/restspec/rspec/api_helpers.rb', line 51

def schema_example(schema_name = nil)
  if schema_name.nil? && endpoint.present?
    schema_name = endpoint.schema_name
  end

  Restspec.example_for(schema_name)
end