Class: Swagger::Response
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
- #validation_message ⇒ Object
- #xml? ⇒ Boolean
Constructor Details
#initialize(raw) ⇒ Response
Returns a new instance of Response.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# 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.) end end |
Instance Attribute Details
#raw ⇒ Object
Returns the value of attribute raw.
6 7 8 |
# File 'lib/swagger/response.rb', line 6 def raw @raw end |
Instance Method Details
#body ⇒ Object
If body is JSON, parse it Otherwise return raw string
47 48 49 50 51 |
# File 'lib/swagger/response.rb', line 47 def body JSON.parse raw.body rescue raw.body end |
#code ⇒ Object
31 32 33 |
# File 'lib/swagger/response.rb', line 31 def code raw.code end |
#error_message ⇒ Object
Account for error messages that take different forms…
36 37 38 39 40 |
# File 'lib/swagger/response.rb', line 36 def body['message'] rescue body end |
#format ⇒ Object
Extract the response format from the header hash e.g. => ‘application/json’
63 64 65 |
# File 'lib/swagger/response.rb', line 63 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.
55 56 57 58 59 |
# File 'lib/swagger/response.rb', line 55 def headers h = {} raw.headers_hash.each {|k,v| h[k] = v } h end |
#json? ⇒ Boolean
67 68 69 |
# File 'lib/swagger/response.rb', line 67 def json? format == 'json' end |
#pretty_body ⇒ Object
75 76 77 78 79 80 |
# 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 ⇒ Object
82 83 84 |
# File 'lib/swagger/response.rb', line 82 def pretty_headers JSON.pretty_generate(headers).gsub(/\n/, '<br/>') end |
#validation_message ⇒ Object
42 43 44 |
# File 'lib/swagger/response.rb', line 42 def body end |
#xml? ⇒ Boolean
71 72 73 |
# File 'lib/swagger/response.rb', line 71 def xml? format == 'xml' end |