diff options
author | Patrick J Cherry <patrick@bytemark.co.uk> | 2011-08-31 14:04:16 +0100 |
---|---|---|
committer | Patrick J Cherry <patrick@bytemark.co.uk> | 2011-08-31 14:04:16 +0100 |
commit | dfb79527aeab7b973c0ece9e681b9813eb148ab6 (patch) | |
tree | fd44dac90ae9a1831f2accff5ea6375f2f164a86 /lib/mauve/calendar_interface.rb | |
parent | e34524d3562906d96ecfb276c17a0536d587b6c0 (diff) |
Fixed do_get timeout and redirection
Diffstat (limited to 'lib/mauve/calendar_interface.rb')
-rw-r--r-- | lib/mauve/calendar_interface.rb | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/lib/mauve/calendar_interface.rb b/lib/mauve/calendar_interface.rb index 4479bf4..f06c597 100644 --- a/lib/mauve/calendar_interface.rb +++ b/lib/mauve/calendar_interface.rb @@ -28,7 +28,7 @@ module Mauve # @param [String] url A Calendar API url. # @return [Array] A list of all the username on support. def get_users_on_support(url) - result = do_get(url) + result = do_get_with_cache(url) if result.is_a?(String) result = result.split("\n") @@ -57,11 +57,11 @@ module Mauve # @param [String] url A Calendar API url. # @param [String] usr User single sign on. # @return [Boolean] True if on holiday, false otherwise. - def is_user_on_holiday?(url, usr) - result = do_get(url) + def is_user_on_holiday?(url) + result = do_get_with_cache(url) if result.is_a?(String) and result =~ /^\d{4}(-\d\d){2}[ T](\d\d:){2}\d\d/ - return result + return true else return false end @@ -91,7 +91,10 @@ module Mauve http = Net::HTTP.new(uri.host, uri.port) - http.open_timeout = http.read_timeout = TIMEOUT + # + # Five second timeouts. + # + http.open_timeout = http.read_timeout = 5 if (uri.scheme == "https") http.use_ssl = true @@ -101,9 +104,9 @@ module Mauve response = http.start { http.get(uri.request_uri()) } if response.is_a?(Net::HTTPOK) - return response.body + return response.body - elsif response.is_a?(Net::HTTPRedirection) and response.has_key?('Location') + elsif response.is_a?(Net::HTTPRedirection) and response.key?('Location') location = response['Location'] # @@ -132,6 +135,21 @@ module Mauve return nil end + def do_get_with_cache(url, cache_until = Time.now + 5.minutes) + @cache ||= {} + + if @cache.has_key?(url.to_s) + result, cache_until = @cache[url.to_s] + + return result if cache_until >= Time.now and not result.nil? + end + + result = do_get(url) + @cache[url] = [result, cache_until] unless result.nil? + + return result + end + end end |