Skip to content
This repository was archived by the owner on Jan 17, 2025. It is now read-only.

Feature/discern server error #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/disqus_api/api.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
12 changes: 12 additions & 0 deletions lib/disqus_api/api_server_error.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions lib/disqus_api/invalid_api_request_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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