lib/bitly/v3/client.rb in bitly-0.8.1 vs lib/bitly/v3/client.rb in bitly-0.9.0
- old
+ new
@@ -5,13 +5,15 @@
# all the rest of the actions available through the API.
class Client
include HTTParty
base_uri 'http://api.bit.ly/v3/'
- # Requires a login and api key. Get yours from your account page at http://bit.ly/a/account
- def initialize(login, api_key)
+ # Requires a login and api key. Get yours from your account page at https://bitly.com/a/your_api_key
+ # Visit your account at http://bit.ly/a/account
+ def initialize(login, api_key, timeout=nil)
@default_query_opts = { :login => login, :apiKey => api_key }
+ self.timeout = timeout
end
# Validates a login and api key
def validate(x_login, x_api_key)
response = get('/validate', :query => { :x_login => x_login, :x_apiKey => x_api_key })
@@ -107,10 +109,14 @@
def clicks_by_day(input, opts={})
opts.reject! { |k, v| k.to_s != 'days' }
get_method(:clicks_by_day, input, opts)
end
+ def timeout=(timeout=nil)
+ self.class.default_timeout(timeout) if timeout
+ end
+
private
def arrayize(arg)
if arg.is_a?(String)
[arg]
@@ -120,11 +126,17 @@
end
def get(method, opts={})
opts[:query] ||= {}
opts[:query].merge!(@default_query_opts)
- response = self.class.get(method, opts)
+
+ begin
+ response = self.class.get(method, opts)
+ rescue Timeout::Error
+ raise BitlyTimeout.new("Bitly didn't respond in time", "504")
+ end
+
if response['status_code'] == 200
return response
else
raise BitlyError.new(response['status_txt'], response['status_code'])
end
@@ -176,5 +188,7 @@
return results.length > 1 ? results : results[0]
end
end
end
end
+
+class BitlyTimeout < BitlyError; end