class Tpaga::Swagger::Response

Attributes

raw[RW]

Public Class Methods

new(raw) click to toggle source
# File lib/tpaga/swagger/response.rb, line 8
def initialize(raw)
  self.raw = raw

  case self.code
  when 500..510 then raise(ServerError, self.error_message)
  when 299..426 then raise(ClientError, self.error_message)
  end
end

Public Instance Methods

body() click to toggle source

If body is JSON, parse it Otherwise return raw string

# File lib/tpaga/swagger/response.rb, line 28
def body
  JSON.parse(raw.body, :symbolize_names => true)
rescue
  raw.body
end
code() click to toggle source
# File lib/tpaga/swagger/response.rb, line 17
def code
  raw.code
end
error_message() click to toggle source

Account for error messages that take different forms…

# File lib/tpaga/swagger/response.rb, line 22
def error_message
  {status: self.code, body: body}.to_json
end
format() click to toggle source

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

# File lib/tpaga/swagger/response.rb, line 44
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/tpaga/swagger/response.rb, line 36
def headers
  h = {}
  raw.headers_hash.each {|k,v| h[k] = v }
  h
end
json?() click to toggle source
# File lib/tpaga/swagger/response.rb, line 48
def json?
  format == 'json'
end
pretty_body() click to toggle source
# File lib/tpaga/swagger/response.rb, line 56
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/tpaga/swagger/response.rb, line 63
def pretty_headers
  JSON.pretty_generate(headers).gsub(/\n/, '<br/>')
end
xml?() click to toggle source
# File lib/tpaga/swagger/response.rb, line 52
def xml?
  format == 'xml'
end