Class: Wrest::Native::Session
- Inherits:
-
Object
- Object
- Wrest::Native::Session
- Defined in:
- lib/wrest/native/session.rb
Overview
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.
Instance Attribute Summary collapse
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #connection ⇒ Object
- #delete(path = '', parameters = {}, headers = {}) ⇒ Object
- #get(path = '', parameters = {}, headers = {}) ⇒ Object
-
#initialize(uri) {|_self| ... } ⇒ Session
constructor
A new instance of Session.
- #maybe_destroy_connection(response) ⇒ Object
- #post(path = '', body = '', headers = {}, params = {}) ⇒ Object
- #put(path = '', body = '', headers = {}, params = {}) ⇒ Object
Constructor Details
#initialize(uri) {|_self| ... } ⇒ Session
Returns a new instance of Session.
22 23 24 25 26 27 |
# File 'lib/wrest/native/session.rb', line 22 def initialize(uri) @uri = Wrest::Uri.new(uri) @default_headers = { Wrest::Native::StandardHeaders::Connection => Wrest::Native::StandardTokens::KeepAlive } yield(self) if block_given? end |
Instance Attribute Details
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
21 22 23 |
# File 'lib/wrest/native/session.rb', line 21 def uri @uri end |
Instance Method Details
#connection ⇒ Object
29 30 31 |
# File 'lib/wrest/native/session.rb', line 29 def connection @connection ||= @uri.create_connection end |
#delete(path = '', parameters = {}, headers = {}) ⇒ Object
45 46 47 |
# File 'lib/wrest/native/session.rb', line 45 def delete(path = '', parameters = {}, headers = {}) maybe_destroy_connection @uri[path, {:connection => self.connection}].delete(parameters, headers.merge(@default_headers)) end |
#get(path = '', parameters = {}, headers = {}) ⇒ Object
33 34 35 |
# File 'lib/wrest/native/session.rb', line 33 def get(path = '', parameters = {}, headers = {}) maybe_destroy_connection @uri[path, {:connection => self.connection}].get(parameters, headers.merge(@default_headers)) end |
#maybe_destroy_connection(response) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/wrest/native/session.rb', line 49 def maybe_destroy_connection(response) if response.connection_closed? Wrest.logger.warn "Connection #{@connection.hash} has been closed by the server" @connection = nil end response end |
#post(path = '', body = '', headers = {}, params = {}) ⇒ Object
37 38 39 |
# File 'lib/wrest/native/session.rb', line 37 def post(path = '', body = '', headers = {}, params = {}) maybe_destroy_connection @uri[path, {:connection => self.connection}].post(body, headers.merge(@default_headers), params) end |
#put(path = '', body = '', headers = {}, params = {}) ⇒ Object
41 42 43 |
# File 'lib/wrest/native/session.rb', line 41 def put(path = '', body = '', headers = {}, params = {}) maybe_destroy_connection @uri[path, {:connection => self.connection}].put(body, headers.merge(@default_headers), params) end |