diff --git a/lib/disqus_api/api.rb b/lib/disqus_api/api.rb index d457826..bb3b2be 100644 --- a/lib/disqus_api/api.rb +++ b/lib/disqus_api/api.rb @@ -1,6 +1,7 @@ module DisqusApi class Api DEFAULT_VERSION = '3.0' + SERVER_ERROR_CODES = [15, 20, 21].freeze attr_reader :version, :endpoint, :specifications, :namespaces # @param [String] version @@ -42,14 +43,14 @@ def connection # @param [String] path # @param [Hash] arguments def get(path, arguments = {}) - perform_request { connection.get(path, arguments).body } + perform_request { connection.get(path, arguments) } end # Performs custom POST request # @param [String] path # @param [Hash] arguments def post(path, arguments = {}) - perform_request { connection.post(path, arguments).body } + perform_request { connection.post(path, arguments) } end # DisqusApi.v3.---->>[users]<<-----.details @@ -67,8 +68,18 @@ def respond_to?(method_name, include_private = false) def perform_request yield.tap do |response| - raise InvalidApiRequestError.new(response) if response['code'] != 0 + return response.body if success? response + fail ApiServerError, response.body if server_error? response + fail InvalidApiRequestError, response.body end end + + def success?(response) + response.status == 200 && response.body['code'] == 0 + end + + def server_error?(response) + response.status / 100 == 5 || SERVER_ERROR_CODES.include?(response.body['code']) + end end end diff --git a/lib/disqus_api/api_server_error.rb b/lib/disqus_api/api_server_error.rb new file mode 100644 index 0000000..f222d63 --- /dev/null +++ b/lib/disqus_api/api_server_error.rb @@ -0,0 +1,12 @@ +module DisqusApi + class ApiServerError < StandardError + attr_reader :response + + # @param [Hash] response + # @param [String] message + def initialize(response, message = response.inspect) + @response = response + super(message) + end + end +end diff --git a/lib/disqus_api/invalid_api_request_error.rb b/lib/disqus_api/invalid_api_request_error.rb index 389402b..f893f46 100644 --- a/lib/disqus_api/invalid_api_request_error.rb +++ b/lib/disqus_api/invalid_api_request_error.rb @@ -3,11 +3,10 @@ class InvalidApiRequestError < StandardError attr_reader :response # @param [Hash] response - # @param [Stirng] message + # @param [String] message def initialize(response, message = response.inspect) @response = response super(message) end end end -