Class: Strobe::Connection::Response
- Inherits:
-
Object
- Object
- Strobe::Connection::Response
- Defined in:
- lib/strobe/connection.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
Instance Method Summary collapse
- #body ⇒ Object
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
- #status ⇒ Object
- #success? ⇒ Boolean
- #validate! ⇒ Object
Constructor Details
#initialize(response) ⇒ Response
Returns a new instance of Response.
43 44 45 46 47 48 49 50 |
# File 'lib/strobe/connection.rb', line 43 def initialize(response) @response = response @headers = {} response.each_header do |key, val| @headers[key] = val end end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
41 42 43 |
# File 'lib/strobe/connection.rb', line 41 def headers @headers end |
Instance Method Details
#body ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/strobe/connection.rb', line 56 def body return @body if @body case mime_type when :json @body = ActiveSupport::JSON.decode(@response.body) else @body = @response.body end @body end |
#status ⇒ Object
52 53 54 |
# File 'lib/strobe/connection.rb', line 52 def status @response.code.to_i end |
#success? ⇒ Boolean
69 70 71 |
# File 'lib/strobe/connection.rb', line 69 def success? status == 200 end |
#validate! ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/strobe/connection.rb', line 73 def validate! msg = body['errors'] && body['errors']['request'] case status when 401 # TODO Fix this on the server raise UnauthenticatedError, msg when 404 raise ResourceNotFoundError, msg when 500...600 raise ServerError, "The server puked :(. Don't worry, the error has been reported." end self end |