Sha256: d94beb8bf4770f2f0bad036a1b5d2baa9b43827b50bea9787c8c719107279fbb
Contents?: true
Size: 1.91 KB
Versions: 14
Compression:
Stored size: 1.91 KB
Contents
module Swagger class Response require 'json' attr_accessor :raw def initialize(raw) self.raw = raw case self.code when 500..510 then raise(ServerError, self.body) when 400 then e = nil if self.body['errors'] e = ValidationError.new(self.body['errors']) if self.body['validation_failures'] e.validation_failures = self.body['validation_failures'] end else e = ValidationError.new(self.body) end raise(e) when 401 then raise(AuthenticationError, self.body) when 402..403 then raise(ClientError, self.body) when 299..399 then raise(ClientError, self.body) when 0 then raise(ClientError, raw.return_message) end end def code raw.code end # Account for error messages that take different forms... def error_message body['message'] rescue body end def validation_message body end # If body is JSON, parse it # Otherwise return raw string def body JSON.parse raw.body rescue raw.body end # `headers_hash` is a Typhoeus-specific extension of Hash, # so simplify it back into a regular old Hash. def headers h = {} raw.headers_hash.each {|k,v| h[k] = v } h end # Extract the response format from the header hash # e.g. {'Content-Type' => 'application/json'} def format headers['Content-Type'].split("/").last.downcase end def json? format == 'json' end def xml? format == 'xml' end def pretty_body return unless body.present? case format when 'json' then JSON.pretty_generate(body).gsub(/\n/, '<br/>') end end def pretty_headers JSON.pretty_generate(headers).gsub(/\n/, '<br/>') end end end
Version data entries
14 entries across 14 versions & 2 rubygems