class Swagger::Response

Attributes

raw[RW]

Public Class Methods

new(raw) click to toggle source
# File lib/swagger/response.rb, line 8
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

Public Instance Methods

body() click to toggle source

If body is JSON, parse it Otherwise return raw string

# File lib/swagger/response.rb, line 47
def body
  JSON.parse raw.body
rescue
  raw.body
end
code() click to toggle source
# File lib/swagger/response.rb, line 31
def code
  raw.code
end
error_message() click to toggle source

Account for error messages that take different forms…

# File lib/swagger/response.rb, line 36
def error_message
  body['message']
rescue
  body
end
format() click to toggle source

Extract the response format from the header hash e.g. {'Content-Type' => 'application/json'}

# File lib/swagger/response.rb, line 63
def format
  headers['Content-Type'].split("/").last.downcase
end
headers() click to toggle source

`headers_hash` is a Typhoeus-specific extension of Hash, so simplify it back into a regular old Hash.

# File lib/swagger/response.rb, line 55
def headers
  h = {}
  raw.headers_hash.each {|k,v| h[k] = v }
  h
end
json?() click to toggle source
# File lib/swagger/response.rb, line 67
def json?
  format == 'json'
end
pretty_body() click to toggle source
# File lib/swagger/response.rb, line 75
def pretty_body
  return unless body.present?
  case format
  when 'json' then JSON.pretty_generate(body).gsub(/\n/, '<br/>')
  end
end
pretty_headers() click to toggle source
# File lib/swagger/response.rb, line 82
def pretty_headers
  JSON.pretty_generate(headers).gsub(/\n/, '<br/>')
end
validation_message() click to toggle source
# File lib/swagger/response.rb, line 42
def validation_message
  body
end
xml?() click to toggle source
# File lib/swagger/response.rb, line 71
def xml?
  format == 'xml'
end