Sha256: ab8cee3070c9e946eeb9838d5a10911ce247ab9702fd76f9e72a6ebc9e4b0a53

Contents?: true

Size: 1.92 KB

Versions: 19

Compression:

Stored size: 1.92 KB

Contents

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

module Fitting
  class Response
    attr_reader :body

    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 within_prefix?(prefix)
      @request.within_prefix?(prefix)
    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
      index.map do |i|
        "#{@request.route} #{@status} #{i}"
      end
    end

    def strict_route
      strict_index.map do |i|
        "#{@request.route} #{@status} #{i}"
      end
    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).to_s)
      end.join("\n\n")
    end

    def json_schema
      @schemas[index.first]
    end

    def ignored?(ignore_list)
      @request.ignored?(ignore_list)
    end

    private

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

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

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
fitting-2.17.0 lib/fitting/response.rb
fitting-2.16.1 lib/fitting/response.rb
fitting-2.16.0 lib/fitting/response.rb
fitting-2.15.0 lib/fitting/response.rb
fitting-2.14.1 lib/fitting/response.rb
fitting-2.14.0 lib/fitting/response.rb
fitting-2.13.1 lib/fitting/response.rb
fitting-2.13.0 lib/fitting/response.rb
fitting-2.12.1 lib/fitting/response.rb
fitting-2.12.0 lib/fitting/response.rb
fitting-2.11.0 lib/fitting/response.rb
fitting-2.10.0 lib/fitting/response.rb
fitting-2.9.1 lib/fitting/response.rb
fitting-2.9.0 lib/fitting/response.rb
fitting-2.8.1 lib/fitting/response.rb
fitting-2.8.0 lib/fitting/response.rb
fitting-2.7.2 lib/fitting/response.rb
fitting-2.7.1 lib/fitting/response.rb
fitting-2.7.0 lib/fitting/response.rb