class TranslationApiClient::Swagger::Response
Attributes
raw[RW]
Public Class Methods
new(raw)
click to toggle source
# File lib/translationApi_client/swagger/response.rb, line 13 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/translationApi_client/swagger/response.rb, line 35 def body if headers["Content-Type"] && (headers["Content-Type"].include? "multipart/") adapter = HTTPAdapter::TyphoeusAdapter.new result = adapter.adapt_response(raw) req = Rack::Request.new(result) parts = req.env[2][0].force_encoding("UTF-8").split("--"+headers["Content-Type"][27...67]) parts.delete_if {|x| !x.include? "part-name: "} parts.map! { |value| arr = value.split(' ') arr[0] = '' arr[1] = '"'+arr[1]+'":' if arr[2][0] != '{' for i in 2..arr.length-1 arr[i] = arr[i].gsub('"', '\"') end arr[2] = '{ "ouput":"'+arr[2] end if arr[-1][-1] != '}' arr[-1] = arr[-1]+'"}' end arr[-1] = arr[-1]+',' arr = arr.join(" ") value = arr } parts = parts.join(" ").sub! /\,$/, '' parts = "{"+parts+'}' #JSON.parse(parts) #JSON.parse req.env[2].gsub('=>', ':') puts parts#JSON.parse(parts) # Rack::Multipart.parse_multipart(req.env) result else begin JSON.parse(raw.body, :symbolize_names => true) rescue raw.body end end end
code()
click to toggle source
# File lib/translationApi_client/swagger/response.rb, line 22 def code raw.code end
error_message()
click to toggle source
Account for error messages that take different forms…
# File lib/translationApi_client/swagger/response.rb, line 27 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/translationApi_client/swagger/response.rb, line 89 def format headers['Content-Type'].split("/").last.downcase end
headers()
click to toggle source
json?()
click to toggle source
# File lib/translationApi_client/swagger/response.rb, line 93 def json? format == 'json' end
pretty_body()
click to toggle source
# File lib/translationApi_client/swagger/response.rb, line 101 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/translationApi_client/swagger/response.rb, line 108 def pretty_headers JSON.pretty_generate(headers).gsub(/\n/, '<br/>') end
xml?()
click to toggle source
# File lib/translationApi_client/swagger/response.rb, line 97 def xml? format == 'xml' end