lib/popit.rb in popit-0.0.5 vs lib/popit.rb in popit-0.0.6

- old
+ new

@@ -20,10 +20,11 @@ # # @see https://github.com/mysociety/popit/blob/master/lib/apps/api/api_v1.js class PopIt class Error < StandardError; end class PageNotFound < Error; end + class ServiceUnavailable < Error; end class NotAuthenticated < Error; end include HTTParty # The instance name. @@ -110,19 +111,24 @@ when :post, :put self.class.send(http_method, path, :basic_auth => {:username => username, :password => password}, :body => JSON.dump(opts), :headers => {'Content-Type' => 'application/json', 'Accept' => 'application/json'}) end unless ['200', '201', '204'].include?(response.response.code) - message = if response.response.content_type == 'text/html' - "HTTP #{response.response.code}" - elsif response.parsed_response['error'] - response.parsed_response['error'] - elsif response.parsed_response['errors'] - response.parsed_response['errors'].join(', ') + message = if Hash === response.parsed_response + if response.parsed_response['error'] + response.parsed_response['error'] + elsif response.parsed_response['errors'] + response.parsed_response['errors'].join(', ') + else + response.parsed_response + end else response.parsed_response end + case response.response.code + when '503' + raise PopIt::ServiceUnavailable, message when '404' raise PopIt::PageNotFound, message when '401' raise PopIt::NotAuthenticated, message else