aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/lib/active_resource/connection.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activeresource/lib/active_resource/connection.rb')
-rw-r--r--activeresource/lib/active_resource/connection.rb12
1 files changed, 9 insertions, 3 deletions
diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb
index c8cee7aaa3..3b61009f43 100644
--- a/activeresource/lib/active_resource/connection.rb
+++ b/activeresource/lib/active_resource/connection.rb
@@ -55,7 +55,7 @@ module ActiveResource
# This class is used by ActiveResource::Base to interface with REST
# services.
class Connection
- attr_reader :site, :user, :password
+ attr_reader :site, :user, :password, :timeout
attr_accessor :format
class << self
@@ -90,6 +90,11 @@ module ActiveResource
@password = password
end
+ # Set the number of seconds after which HTTP requests to the remote service should time out.
+ def timeout=(timeout)
+ @timeout = timeout
+ end
+
# Execute a GET request.
# Used to get (find) resources.
def get(path, headers = {})
@@ -167,18 +172,19 @@ module ActiveResource
http = Net::HTTP.new(@site.host, @site.port)
http.use_ssl = @site.is_a?(URI::HTTPS)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl
+ http.read_timeout = @timeout if @timeout # If timeout is not set, the default Net::HTTP timeout (60s) is used.
http
end
def default_header
@default_header ||= { 'Content-Type' => format.mime_type }
end
-
+
# Builds headers for request to remote service.
def build_request_headers(headers)
authorization_header.update(default_header).update(headers)
end
-
+
# Sets authorization header
def authorization_header
(@user || @password ? { 'Authorization' => 'Basic ' + ["#{@user}:#{ @password}"].pack('m').delete("\r\n") } : {})