Class: Irumugam::Contract
- Inherits:
-
Object
- Object
- Irumugam::Contract
- Defined in:
- lib/irumugam/contract.rb
Instance Attribute Summary collapse
-
#accept ⇒ Object
Returns the value of attribute accept.
-
#block ⇒ Object
Returns the value of attribute block.
-
#contract_body ⇒ Object
Returns the value of attribute contract_body.
-
#contract_json ⇒ Object
Returns the value of attribute contract_json.
-
#contract_json_ignore ⇒ Object
Returns the value of attribute contract_json_ignore.
-
#contract_status ⇒ Object
Returns the value of attribute contract_status.
-
#method ⇒ Object
Returns the value of attribute method.
-
#params ⇒ Object
Returns the value of attribute params.
-
#path ⇒ Object
Returns the value of attribute path.
-
#path_object_block ⇒ Object
Returns the value of attribute path_object_block.
-
#request_body ⇒ Object
Returns the value of attribute request_body.
-
#request_type ⇒ Object
Returns the value of attribute request_type.
-
#service ⇒ Object
Returns the value of attribute service.
-
#test_host ⇒ Object
Returns the value of attribute test_host.
Instance Method Summary collapse
- #body(val) ⇒ Object
-
#initialize(options = {}) ⇒ Contract
constructor
A new instance of Contract.
- #json(val, options = {}) ⇒ Object
- #json_for_spec ⇒ Object
- #matches?(request, req_body) ⇒ Boolean
- #path_match?(request) ⇒ Boolean
- #path_object(&po_block) ⇒ Object
- #prepare_response(response) ⇒ Object
- #status(val) ⇒ Object
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(={}) @method = [:method] @path = [:path] @test_host = [:test_host] @params = [:params] @request_body = [:body] @request_type = [:type] @accept = [:accept] @service = [:service] instance_eval &([:block]) end |
Instance Attribute Details
#accept ⇒ Object
Returns the value of attribute accept.
3 4 5 |
# File 'lib/irumugam/contract.rb', line 3 def accept @accept end |
#block ⇒ Object
Returns the value of attribute block.
3 4 5 |
# File 'lib/irumugam/contract.rb', line 3 def block @block end |
#contract_body ⇒ Object
Returns the value of attribute contract_body.
3 4 5 |
# File 'lib/irumugam/contract.rb', line 3 def contract_body @contract_body end |
#contract_json ⇒ Object
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_ignore ⇒ Object
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_status ⇒ Object
Returns the value of attribute contract_status.
3 4 5 |
# File 'lib/irumugam/contract.rb', line 3 def contract_status @contract_status end |
#method ⇒ Object
Returns the value of attribute method.
3 4 5 |
# File 'lib/irumugam/contract.rb', line 3 def method @method end |
#params ⇒ Object
Returns the value of attribute params.
3 4 5 |
# File 'lib/irumugam/contract.rb', line 3 def params @params end |
#path ⇒ Object
Returns the value of attribute path.
3 4 5 |
# File 'lib/irumugam/contract.rb', line 3 def path @path end |
#path_object_block ⇒ Object
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_body ⇒ Object
Returns the value of attribute request_body.
3 4 5 |
# File 'lib/irumugam/contract.rb', line 3 def request_body @request_body end |
#request_type ⇒ Object
Returns the value of attribute request_type.
3 4 5 |
# File 'lib/irumugam/contract.rb', line 3 def request_type @request_type end |
#service ⇒ Object
Returns the value of attribute service.
3 4 5 |
# File 'lib/irumugam/contract.rb', line 3 def service @service end |
#test_host ⇒ Object
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,={}) @contract_json = val @contract_json_ignore = .delete(:ignore) || [] end |
#json_for_spec ⇒ Object
33 34 35 |
# File 'lib/irumugam/contract.rb', line 33 def json_for_spec prepare_response(contract_json) end |
#matches?(request, req_body) ⇒ 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
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 |