Class: TranslationApiClient::Swagger::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/translationApi_client/swagger/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.error_message)
  when 299..426 then raise(ClientError, self.error_message)
  end
end

Instance Attribute Details

#rawObject

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

#bodyObject

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

#codeObject



22
23
24
# File 'lib/translationApi_client/swagger/response.rb', line 22

def code
  raw.code
end

#error_messageObject

Account for error messages that take different forms…



27
28
29
30
31
# File 'lib/translationApi_client/swagger/response.rb', line 27

def error_message
  body['message']
rescue
  body
end

#formatObject

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

#headersObject

‘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

Returns:

  • (Boolean)


93
94
95
# File 'lib/translationApi_client/swagger/response.rb', line 93

def json?
  format == 'json'
end

#pretty_bodyObject



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_headersObject



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

Returns:

  • (Boolean)


97
98
99
# File 'lib/translationApi_client/swagger/response.rb', line 97

def xml?
  format == 'xml'
end