Class: Fitting::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/fitting/response.rb

Defined Under Namespace

Classes: NotDocumented, Unsuitable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env_response, expect_request) ⇒ Response

Returns a new instance of Response.



8
9
10
11
12
13
14
15
# File 'lib/fitting/response.rb', line 8

def initialize(env_response, expect_request)
  @status = env_response.status
  @body = env_response.body
  @schemas = expect_request.find_responses(status: @status).map{|response|response['body']} if expect_request
  @fully_validates = set_fully_validate if @schemas
  raise Response::NotDocumented unless (@schemas && @schemas.first) || Fitting.configuration.skip_not_documented
  self
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



3
4
5
# File 'lib/fitting/response.rb', line 3

def body
  @body
end

#schemasObject

Returns the value of attribute schemas.



3
4
5
# File 'lib/fitting/response.rb', line 3

def schemas
  @schemas
end

#statusObject

Returns the value of attribute status.



3
4
5
# File 'lib/fitting/response.rb', line 3

def status
  @status
end

Instance Method Details

#set_fully_validateObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/fitting/response.rb', line 21

def set_fully_validate
  @valid = false
  fully_validates = []
  @schemas.map do |old_schema|
    fully_validate = JSON::Validator.fully_validate(old_schema, @body)
    fully_validates.push(fully_validate)
    @valid = true if fully_validate == []
  end
  fully_validates
end

#to_hashObject



36
37
38
39
40
41
42
43
44
# File 'lib/fitting/response.rb', line 36

def to_hash
  {
    'status' => @status,
    'body' => @body,
    'schemas' => @schemas,
    'fully_validates' => @fully_validates,
    'valid' => @valid
  }
end

#valid!Object

Raises:



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

def valid!
  raise Unsuitable unless @valid
end

#validate?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/fitting/response.rb', line 32

def validate?
  @schemas && @schemas.first && Fitting.configuration.validation_response
end