Class: Irumugam::Contract

Inherits:
Object
  • Object
show all
Defined in:
lib/irumugam/contract.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Contract

Returns a new instance of Contract.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/irumugam/contract.rb', line 4

def initialize(options={})
  @method = options[:method]
  @path = options[:path]
  @test_host = options[:test_host]
  @params = options[:params]
  @request_body = options[:body]
  @request_type = options[:type]
  @accept = options[:accept]
  @service = options[:service]
  instance_eval &(options[:block])
end

Instance Attribute Details

#acceptObject

Returns the value of attribute accept.



3
4
5
# File 'lib/irumugam/contract.rb', line 3

def accept
  @accept
end

#blockObject

Returns the value of attribute block.



3
4
5
# File 'lib/irumugam/contract.rb', line 3

def block
  @block
end

#contract_bodyObject

Returns the value of attribute contract_body.



3
4
5
# File 'lib/irumugam/contract.rb', line 3

def contract_body
  @contract_body
end

#contract_jsonObject

Returns the value of attribute contract_json.



3
4
5
# File 'lib/irumugam/contract.rb', line 3

def contract_json
  @contract_json
end

#contract_json_ignoreObject

Returns the value of attribute contract_json_ignore.



3
4
5
# File 'lib/irumugam/contract.rb', line 3

def contract_json_ignore
  @contract_json_ignore
end

#contract_statusObject

Returns the value of attribute contract_status.



3
4
5
# File 'lib/irumugam/contract.rb', line 3

def contract_status
  @contract_status
end

#methodObject

Returns the value of attribute method.



3
4
5
# File 'lib/irumugam/contract.rb', line 3

def method
  @method
end

#paramsObject

Returns the value of attribute params.



3
4
5
# File 'lib/irumugam/contract.rb', line 3

def params
  @params
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/irumugam/contract.rb', line 3

def path
  @path
end

#path_object_blockObject

Returns the value of attribute path_object_block.



3
4
5
# File 'lib/irumugam/contract.rb', line 3

def path_object_block
  @path_object_block
end

#request_bodyObject

Returns the value of attribute request_body.



3
4
5
# File 'lib/irumugam/contract.rb', line 3

def request_body
  @request_body
end

#request_typeObject

Returns the value of attribute request_type.



3
4
5
# File 'lib/irumugam/contract.rb', line 3

def request_type
  @request_type
end

#serviceObject

Returns the value of attribute service.



3
4
5
# File 'lib/irumugam/contract.rb', line 3

def service
  @service
end

#test_hostObject

Returns the value of attribute test_host.



3
4
5
# File 'lib/irumugam/contract.rb', line 3

def test_host
  @test_host
end

Instance Method Details

#body(val) ⇒ Object



20
21
22
# File 'lib/irumugam/contract.rb', line 20

def body(val)
  @contract_body = val
end

#json(val, options = {}) ⇒ Object



24
25
26
27
# File 'lib/irumugam/contract.rb', line 24

def json(val,options={})
  @contract_json = val
  @contract_json_ignore = options.delete(:ignore) || []
end

#json_for_specObject



33
34
35
# File 'lib/irumugam/contract.rb', line 33

def json_for_spec
  prepare_response(contract_json)
end

#matches?(request, req_body) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
# File 'lib/irumugam/contract.rb', line 56

def matches?(request, req_body)
  return false if !req_body.empty? ^ !self.request_body.nil?
  request_json = JSON.parse(req_body) if (!req_body.empty? && request.content_type=="application/json")
  result = path_match?(request) && 
    request.request_method==self.method && 
    request.params==self.params && 
    (request_json.nil? ? true : (request_json == self.request_body.stringify_keys))
  result
end

#path_match?(request) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/irumugam/contract.rb', line 44

def path_match?(request)
  match_pattern = /(\:\w+)/
  replace_pattern = /(\w+)/
  path_patterns = self.path.split("/").map { |part|
    part.index(":") ? Regexp.new(part.gsub(match_pattern, replace_pattern.source)) : /#{part}/
  }
  return false unless request.path.split("/").count==path_patterns.count
  path_patterns.each_index do |idx|
    return false unless path_patterns[idx]=~request.path.split("/")[idx]
  end
  true
end

#path_object(&po_block) ⇒ Object



29
30
31
# File 'lib/irumugam/contract.rb', line 29

def path_object(&po_block)
  @path_object_block = po_block
end

#prepare_response(response) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/irumugam/contract.rb', line 37

def prepare_response(response)
  response = JSON.parse(response) if response.is_a?(String)
  spec_json = response.clone
  spec_json.map{ |hsh| process_hash(hsh) } if spec_json.is_a?(Array)
  process_hash(spec_json) if spec_json.is_a?(Hash)
  spec_json
end

#status(val) ⇒ Object



16
17
18
# File 'lib/irumugam/contract.rb', line 16

def status(val)
  @contract_status = val
end