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.
26 27 28 29 30 31 |
# File 'lib/wrest/native/session.rb', line 26 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.
24 25 26 |
# File 'lib/wrest/native/session.rb', line 24 def uri @uri end |
Instance Method Details
#connection ⇒ Object
33 34 35 |
# File 'lib/wrest/native/session.rb', line 33 def connection @connection ||= @uri.create_connection end |
#delete(path = '', parameters = {}, headers = {}) ⇒ Object
52 53 54 55 |
# File 'lib/wrest/native/session.rb', line 52 def delete(path = '', parameters = {}, headers = {}) maybe_destroy_connection @uri[path, { connection: connection }].delete(parameters, headers.merge(@default_headers)) end |
#get(path = '', parameters = {}, headers = {}) ⇒ Object
37 38 39 40 |
# File 'lib/wrest/native/session.rb', line 37 def get(path = '', parameters = {}, headers = {}) maybe_destroy_connection @uri[path, { connection: connection }].get(parameters, headers.merge(@default_headers)) end |
#maybe_destroy_connection(response) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/wrest/native/session.rb', line 57 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
42 43 44 45 |
# File 'lib/wrest/native/session.rb', line 42 def post(path = '', body = '', headers = {}, params = {}) maybe_destroy_connection @uri[path, { connection: connection }].post(body, headers.merge(@default_headers), params) end |
#put(path = '', body = '', headers = {}, params = {}) ⇒ Object
47 48 49 50 |
# File 'lib/wrest/native/session.rb', line 47 def put(path = '', body = '', headers = {}, params = {}) maybe_destroy_connection @uri[path, { connection: connection }].put(body, headers.merge(@default_headers), params) end |