Sha256: dfc86f7227381de3112502f5ca18cdd9cd738f73b22c6e741af9420441640168

Contents?: true

Size: 878 Bytes

Versions: 1

Compression:

Stored size: 878 Bytes

Contents

module Contracts
  class Response
    def initialize(definition)
      @definition = definition
    end

    def instantiate
      OpenStruct.new({
        'status' => @definition['status'],
        'headers' => @definition['headers'],
        'body' => JSON::Generator.generate(@definition['body'])
      })
    end

    def validate(response)
      @errors = []
      if @definition['status'] != response.status
        @errors << "Invalid status: expected #{@definition['status']} but got #{response.status}"
      end
      unless @definition['headers'].normalize_keys.subset_of?(response.headers.normalize_keys)
        @errors << "Invalid headers: expected #{@definition['headers'].inspect} to be a subset of #{response.headers.inspect}"
      end
      @errors << JSON::Validator.fully_validate(@definition['body'], response.body)
      @errors.flatten
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
contracts_api_test-0.0.1 lib/contracts/response.rb