Class: ApiTester::Endpoint

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, url) ⇒ Endpoint

Returns a new instance of Endpoint.



18
19
20
21
22
23
24
25
26
27
# File 'lib/api-tester/definition/endpoint.rb', line 18

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

Instance Attribute Details

#bad_request_responseObject

Returns the value of attribute bad_request_response.



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

def bad_request_response
  @bad_request_response
end

#base_urlObject

Returns the value of attribute base_url.



10
11
12
# File 'lib/api-tester/definition/endpoint.rb', line 10

def base_url
  @base_url
end

#methodsObject

Returns the value of attribute methods.



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

def methods
  @methods
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/api-tester/definition/endpoint.rb', line 9

def name
  @name
end

#not_allowed_responseObject

Returns the value of attribute not_allowed_response.



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

def not_allowed_response
  @not_allowed_response
end

#not_found_responseObject

Returns the value of attribute not_found_response.



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

def not_found_response
  @not_found_response
end

#path_paramsObject

Returns the value of attribute path_params.



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

def path_params
  @path_params
end

#test_helperObject

Returns the value of attribute test_helper.



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

def test_helper
  @test_helper
end

Instance Method Details

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



61
62
63
64
# File 'lib/api-tester/definition/endpoint.rb', line 61

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

#add_path_param(param) ⇒ Object



66
67
68
69
# File 'lib/api-tester/definition/endpoint.rb', line 66

def add_path_param param
  self.path_params << param
  self
end

#call(method, payload = {}, headers = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/api-tester/definition/endpoint.rb', line 50

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

#default_callObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/api-tester/definition/endpoint.rb', line 37

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

#urlObject



29
30
31
32
33
34
35
# File 'lib/api-tester/definition/endpoint.rb', line 29

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

#verbsObject



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

def verbs
  self.methods.map(&:verb)
end