Class: TranslationApiClient::Swagger::Response
- Defined in:
- lib/translationApi_client/swagger/response.rb
Instance Attribute Summary collapse
-
#raw ⇒ Object
Returns the value of attribute raw.
Instance Method Summary collapse
-
#body ⇒ Object
If body is JSON, parse it Otherwise return raw string.
- #code ⇒ Object
-
#error_message ⇒ Object
Account for error messages that take different forms…
-
#format ⇒ Object
Extract the response format from the header hash e.g.
-
#headers ⇒ Object
‘headers_hash` is a Typhoeus-specific extension of Hash, so simplify it back into a regular old Hash.
-
#initialize(raw) ⇒ Response
constructor
A new instance of Response.
- #json? ⇒ Boolean
- #pretty_body ⇒ Object
- #pretty_headers ⇒ Object
- #xml? ⇒ Boolean
Constructor Details
#initialize(raw) ⇒ Response
Returns a new instance of Response.
13 14 15 16 17 18 19 20 |
# 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.) when 299..426 then raise(ClientError, self.) end end |
Instance Attribute Details
#raw ⇒ Object
Returns the value of attribute raw.
11 12 13 |
# File 'lib/translationApi_client/swagger/response.rb', line 11 def raw @raw end |
Instance Method Details
#body ⇒ Object
If body is JSON, parse it Otherwise return raw string
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# 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 ⇒ Object
22 23 24 |
# File 'lib/translationApi_client/swagger/response.rb', line 22 def code raw.code end |
#error_message ⇒ Object
Account for error messages that take different forms…
27 28 29 30 31 |
# File 'lib/translationApi_client/swagger/response.rb', line 27 def body['message'] rescue body end |
#format ⇒ Object
Extract the response format from the header hash e.g. => ‘application/json’
89 90 91 |
# File 'lib/translationApi_client/swagger/response.rb', line 89 def format headers['Content-Type'].split("/").last.downcase end |
#headers ⇒ Object
‘headers_hash` is a Typhoeus-specific extension of Hash, so simplify it back into a regular old Hash.
81 82 83 84 85 |
# File 'lib/translationApi_client/swagger/response.rb', line 81 def headers h = {} raw.headers_hash.each {|k,v| h[k] = v } h end |
#json? ⇒ Boolean
93 94 95 |
# File 'lib/translationApi_client/swagger/response.rb', line 93 def json? format == 'json' end |
#pretty_body ⇒ Object
101 102 103 104 105 106 |
# 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 ⇒ Object
108 109 110 |
# File 'lib/translationApi_client/swagger/response.rb', line 108 def pretty_headers JSON.pretty_generate(headers).gsub(/\n/, '<br/>') end |
#xml? ⇒ Boolean
97 98 99 |
# File 'lib/translationApi_client/swagger/response.rb', line 97 def xml? format == 'xml' end |