Class: ApiTester::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/api-tester/definition/endpoint.rb

Overview

Class for defining and interacting with endpoints in a contract

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, relative_url:) ⇒ Endpoint

Returns a new instance of Endpoint.



21
22
23
24
25
26
27
28
29
30
# File 'lib/api-tester/definition/endpoint.rb', line 21

def initialize(name:, relative_url:)
  self.relative_url = relative_url
  self.name = name
  self.methods = []
  self.path_params = []
  self.test_helper = ApiTester::TestHelper.new
  self.bad_request_response = ApiTester::Response.new status_code: 400
  self.not_allowed_response = ApiTester::Response.new status_code: 415
  self.not_found_response = ApiTester::Response.new status_code: 404
end

Instance Attribute Details

#bad_request_responseObject

Returns the value of attribute bad_request_response.



17
18
19
# File 'lib/api-tester/definition/endpoint.rb', line 17

def bad_request_response
  @bad_request_response
end

#methodsObject

Returns the value of attribute methods.



15
16
17
# File 'lib/api-tester/definition/endpoint.rb', line 15

def methods
  @methods
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/api-tester/definition/endpoint.rb', line 12

def name
  @name
end

#not_allowed_responseObject

Returns the value of attribute not_allowed_response.



18
19
20
# File 'lib/api-tester/definition/endpoint.rb', line 18

def not_allowed_response
  @not_allowed_response
end

#not_found_responseObject

Returns the value of attribute not_found_response.



19
20
21
# File 'lib/api-tester/definition/endpoint.rb', line 19

def not_found_response
  @not_found_response
end

#path_paramsObject

Returns the value of attribute path_params.



14
15
16
# File 'lib/api-tester/definition/endpoint.rb', line 14

def path_params
  @path_params
end

#relative_urlObject

Returns the value of attribute relative_url.



13
14
15
# File 'lib/api-tester/definition/endpoint.rb', line 13

def relative_url
  @relative_url
end

#test_helperObject

Returns the value of attribute test_helper.



16
17
18
# File 'lib/api-tester/definition/endpoint.rb', line 16

def test_helper
  @test_helper
end

Instance Method Details

#add_method(verb:, response:, request: Request.new) ⇒ Object



68
69
70
71
72
73
# File 'lib/api-tester/definition/endpoint.rb', line 68

def add_method(verb:, response:, request: Request.new)
  methods << ApiTester::Method.new(verb: verb,
                                   response: response,
                                   request: request)
  self
end

#add_path_param(param) ⇒ Object



75
76
77
78
# File 'lib/api-tester/definition/endpoint.rb', line 75

def add_path_param(param)
  path_params << param
  self
end

#call(base_url:, method:, query: '', payload: {}, headers: {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/api-tester/definition/endpoint.rb', line 53

def call(base_url:, method:, query: '', payload: {}, headers: {})
  test_helper.before
  url = query ? "#{base_url}#{self.url}?#{query}" : "#{base_url}#{self.url}"
  begin
    response = RestClient::Request.execute(method: method.verb,
                                           url: url,
                                           payload: payload.to_json,
                                           headers: headers)
  rescue RestClient::ExceptionWithResponse => e
    response = e.response
  end
  test_helper.after
  response
end

#default_call(base_url) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/api-tester/definition/endpoint.rb', line 40

def default_call(base_url)
  test_helper.before
  method_defaults = methods[0].default_request
  method_defaults[:url] = "#{base_url}#{url}"
  begin
    response = RestClient::Request.execute(method_defaults)
  rescue RestClient::ExceptionWithResponse => e
    response = e.response
  end
  test_helper.after
  response
end

#urlObject



32
33
34
35
36
37
38
# File 'lib/api-tester/definition/endpoint.rb', line 32

def url
  temp_url = relative_url
  path_params.each do |param|
    temp_url.sub! "{#{param}}", test_helper.retrieve_param(param)
  end
  temp_url
end

#verbsObject



80
81
82
# File 'lib/api-tester/definition/endpoint.rb', line 80

def verbs
  methods.map(&:verb)
end