Class Wrest::Curl::Session

  1. lib/wrest/curl/session.rb
Parent: Object

This class is a wrapper for a keep-alive HTTP connection. It simply passes the same connection instance as an option to all Wrest::Native::Request instances created using it.

If at any point the server closes an existing connection during a Session by returning a Connection: Close header the current connection is destroyed and a fresh one created for the next request.

The Session constructor can accept either a String URI or a Wrest::Uri as a parameter. If you need HTTP authentication, you should use a Wrest::Uri.

Methods

public class

  1. new

public instance

  1. connection
  2. delete
  3. get
  4. maybe_destroy_connection
  5. post
  6. put

Attributes

uri [R]

Public class methods

new (uri) {|self| ...}
[show source]
    # File lib/wrest/curl/session.rb, line 22
22:     def initialize(uri)
23:       @uri = Wrest::Uri.new(uri)
24:       @default_headers = { StandardHeaders::Connection => StandardTokens::KeepAlive }
25: 
26:       yield(self) if block_given?
27:     end

Public instance methods

connection ()
[show source]
    # File lib/wrest/curl/session.rb, line 29
29:     def connection
30:       @connection ||= @uri.create_connection
31:     end
delete (path = '', parameters = {}, headers = {})
[show source]
    # File lib/wrest/curl/session.rb, line 45
45:     def delete(path = '', parameters = {}, headers = {})
46:       maybe_destroy_connection @uri[path, {:connection => self.connection}].delete(parameters, headers.merge(@default_headers))
47:     end
get (path = '', parameters = {}, headers = {})
[show source]
    # File lib/wrest/curl/session.rb, line 33
33:     def get(path = '', parameters = {}, headers = {})
34:       maybe_destroy_connection @uri[path, {:connection => self.connection}].get(parameters, headers.merge(@default_headers))
35:     end
maybe_destroy_connection (response)
[show source]
    # File lib/wrest/curl/session.rb, line 49
49:     def maybe_destroy_connection(response)
50:       if response.connection_closed?
51:         Wrest.logger.warn "Connection #{@connection.hash} has been closed by the server"
52:         @connection = nil 
53:       end
54:       response
55:     end
post (path = '', body = '', headers = {}, params = {})
[show source]
    # File lib/wrest/curl/session.rb, line 37
37:     def post(path = '', body = '', headers = {}, params = {})
38:       maybe_destroy_connection @uri[path, {:connection => self.connection}].post(body, headers.merge(@default_headers), params)
39:     end
put (path = '', body = '', headers = {}, params = {})
[show source]
    # File lib/wrest/curl/session.rb, line 41
41:     def put(path = '', body = '', headers = {}, params = {})
42:       maybe_destroy_connection @uri[path, {:connection => self.connection}].put(body, headers.merge(@default_headers), params)
43:     end