Class: Contracts::Response
- Inherits:
-
Object
- Object
- Contracts::Response
- Defined in:
- lib/contracts/response.rb
Instance Method Summary collapse
-
#initialize(definition) ⇒ Response
constructor
A new instance of Response.
- #instantiate ⇒ Object
- #validate(response) ⇒ Object
Constructor Details
#initialize(definition) ⇒ Response
Returns a new instance of Response.
3 4 5 |
# File 'lib/contracts/response.rb', line 3 def initialize(definition) @definition = definition end |
Instance Method Details
#instantiate ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/contracts/response.rb', line 7 def instantiate OpenStruct.new({ 'status' => @definition['status'], 'headers' => @definition['headers'], 'body' => JSON::Generator.generate(@definition['body']) }) end |
#validate(response) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/contracts/response.rb', line 15 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 |