lib/popit.rb in popit-0.0.6 vs lib/popit.rb in popit-0.0.7
- old
+ new
@@ -37,31 +37,37 @@
attr_reader :version
# A user name.
attr_reader :username
# The user's password.
attr_reader :password
+ # The maximum number of retries in case of HTTP 503 Service Unavailable errors.
+ attr_reader :max_retries
# Initializes a PopIt API client.
#
# @param [Hash] opts the API client's configuration
# @option opts [String] :instance_name the instance name
# @option opts [String] :host_name the PopIt API's host name, eg "popit.mysociety.org"
# @option opts [String] :post the PopIt API's port, eg 80
# @option opts [String] :version the PopIt API version, eg "v1"
# @option opts [String] :user a user name
# @option opts [String] :password the user's password
+ # @option opts [String] :max_retries the maximum number of retries in case of
+ # HTTP 503 Service Unavailable errors
def initialize(opts = {})
unless opts.has_key?(:instance_name)
raise ArgumentError, 'Missing key :instance_name'
end
@instance_name = opts[:instance_name]
- @host_name = opts[:host_name] || 'popit.mysociety.org'
- @port = opts[:port] || 80
- @version = opts[:version] || 'v0.1'
+ @host_name = opts[:host_name] || 'popit.mysociety.org'
+ @port = opts[:port] || 80
+ @version = opts[:version] || 'v0.1'
@username = opts[:user]
@password = opts[:password]
+ @max_retries = opts[:max_retries] || 0
+
end
# Sends a GET request.
#
# @param [String] path a path with no leading slash
@@ -99,10 +105,12 @@
end
private
def request(http_method, path, opts = {})
+ attempts ||= 0
+
path = "http://#{instance_name}.#{host_name}:#{port}/api/#{version}/#{path}"
response = case http_method
when :get
self.class.send(http_method, path, :query => opts)
@@ -136,9 +144,17 @@
raise PopIt::Error, message
end
end
response.parsed_response && response.parsed_response['result']
+ rescue PopIt::ServiceUnavailable
+ attempts += 1
+ if attempts <= max_retries
+ sleep attempts ** 2
+ retry
+ else
+ raise
+ end
end
def method_missing(*args)
Chain.new(self, args)
end