Sha256: d371b6973f2049071ea961f6e901b44bde1a79a80b1ffde64f6bb064eac926da

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

require 'fitting/request'
require 'fitting/response/fully_validates'
require 'json'
require 'multi_json'

module Fitting
  class Response
    def initialize(env_response, tomogram)
      @request = Fitting::Request.new(env_response.request, tomogram)
      @status = env_response.status
      @body = env_response.body
      @schemas = @request.schemas_of_possible_responses(status: @status)
    end

    def fully_validates
      @fully_validates ||= Fitting::Response::FullyValidates.craft(@schemas, @body, false)
    end

    def strict_fully_validates
      @strict_fully_validates ||= Fitting::Response::FullyValidates.craft(@schemas, @body, true)
    end

    def documented?
      @schemas && @schemas.present?
    end

    def route
      "#{@request.route} #{@status} #{index}"
    end

    def strict_route
      "#{@request.route} #{@status} #{strict_index}"
    end

    def real_request_with_status
      "#{@request.real_method_with_path} #{@status}"
    end

    def got
      JSON.pretty_generate(MultiJson.load(@body))
    end

    def expected
      @expected ||= @schemas.inject([]) do |res, schema|
        res.push("#{JSON.pretty_generate(schema)}")
      end.join("\n\n")
    end

    private

    def index
      @schemas.size.times do |i|
        if fully_validates[i] == []
          return i
        end
      end
    end

    def strict_index
      @schemas.size.times do |i|
        if strict_fully_validates[i] == []
          return i
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fitting-2.0.2 lib/fitting/response.rb
fitting-2.0.1 lib/fitting/response.rb
fitting-2.0.0 lib/fitting/response.rb