class Contracto::Contract::Response

Public Class Methods

new(hash) click to toggle source
# File lib/contracto/contract/response.rb, line 5
def initialize(hash)
  @hash = hash
end

Public Instance Methods

body() click to toggle source
# File lib/contracto/contract/response.rb, line 52
def body
  File.read(Contracto::Config.root_dir + body_path)
end
body_path() click to toggle source
# File lib/contracto/contract/response.rb, line 21
def body_path
  @hash.fetch('response').fetch('body_path')
end
conditions_number() click to toggle source
# File lib/contracto/contract/response.rb, line 48
def conditions_number
  params.keys.size + headers.keys.size
end
headers() click to toggle source
# File lib/contracto/contract/response.rb, line 17
def headers
  request['headers'] || {}  #TODO: should it be optional or required?
end
headers_matches?(other_headers) click to toggle source
# 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
params() click to toggle source
# File lib/contracto/contract/response.rb, line 13
def params
  request['params'] || {}  #TODO: should it be optional or required?
end
params_matches?(request_params) click to toggle source
# 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
request() click to toggle source
# File lib/contracto/contract/response.rb, line 9
def request
  @hash['request'] || {}
end

Private Instance Methods

human_header_key_to_http_header_key(key) click to toggle source
# File lib/contracto/contract/response.rb, line 66
def human_header_key_to_http_header_key(key)
  key = key.upcase
  key = key.gsub('-', '_')
  key = 'HTTP_' + key
  key
end
string_to_number(number, string) click to toggle source
# File lib/contracto/contract/response.rb, line 58
def string_to_number(number, string)
  if number.is_a?(Integer)
    string.to_i
  elsif number.is_a?(Float)
    string.to_f
  end
end