Class: Contracto::Contract::Response

Inherits:
Object
  • Object
show all
Includes:
Contracto::Constants
Defined in:
lib/contracto/contract/response.rb

Constant Summary

Constants included from Contracto::Constants

Contracto::Constants::CONTRACTO_DIR, Contracto::Constants::CONTRACTO_TMP_DIR, Contracto::Constants::CONTRACT_FILENAME, Contracto::Constants::CURRENT_DIR, Contracto::Constants::DEFAULT_ROOT_DIR, Contracto::Constants::GEM_DIR, Contracto::Constants::PORT, Contracto::Constants::RUBY_SERVER_DIR, Contracto::Constants::SAMPLE_CONTRACT_DIR, Contracto::Constants::SERVER_PIDFILE_NAME

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Response

Returns a new instance of Response.



5
6
7
# File 'lib/contracto/contract/response.rb', line 5

def initialize(hash)
  @hash = hash
end

Instance Method Details

#bodyObject



52
53
54
# File 'lib/contracto/contract/response.rb', line 52

def body
  File.read(Contracto::Config.root_dir + body_path)
end

#body_pathObject



21
22
23
# File 'lib/contracto/contract/response.rb', line 21

def body_path
  @hash.fetch('response').fetch('body_path')
end

#conditions_numberObject



48
49
50
# File 'lib/contracto/contract/response.rb', line 48

def conditions_number
  params.keys.size + headers.keys.size
end

#headersObject



17
18
19
# File 'lib/contracto/contract/response.rb', line 17

def headers
  request['headers'] || {}  #TODO: should it be optional or required?
end

#headers_matches?(other_headers) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# File 'lib/contracto/contract/response.rb', line 40

def headers_matches?(other_headers)
  return true if headers.empty?

  headers.keys.all? do |key|
    other_headers[human_header_key_to_http_header_key(key)] == headers[key]
  end
end

#paramsObject



13
14
15
# File 'lib/contracto/contract/response.rb', line 13

def params
  request['params'] || {}  #TODO: should it be optional or required?
end

#params_matches?(request_params) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/contracto/contract/response.rb', line 25

def params_matches?(request_params)
  return true if params.empty?

  params.keys.all? do |key|
    value_from_contract = params[key]
    value_from_request = request_params[key]

    if value_from_contract.is_a?(Numeric)
      value_from_request = string_to_number(value_from_contract, value_from_request)
    end

    value_from_request == value_from_contract
  end
end

#requestObject



9
10
11
# File 'lib/contracto/contract/response.rb', line 9

def request
  @hash['request'] || {}
end