Class: HttpRequest::Client
- Inherits:
-
Object
- Object
- HttpRequest::Client
- Defined in:
- lib/http-request/client.rb
Instance Attribute Summary collapse
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
-
#open_url ⇒ Object
readonly
Returns the value of attribute open_url.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
- .delete_from_pool(key) ⇒ Object
-
.pool(key, options = {}) ⇒ Object
Syntax sugar to manage clients in the current thread.
Instance Method Summary collapse
- #close(url = nil) ⇒ Object
- #connection(url) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #open(url) ⇒ Object
- #request(method, url, headers = {}, body = nil) ⇒ Object
- #server_id(url) ⇒ Object
- #uri(url) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
15 16 17 18 |
# File 'lib/http-request/client.rb', line 15 def initialize(={}) @options = HttpRequest.config.merge() # Connection options @connections = [] end |
Instance Attribute Details
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
3 4 5 |
# File 'lib/http-request/client.rb', line 3 def connections @connections end |
#open_url ⇒ Object (readonly)
Returns the value of attribute open_url.
3 4 5 |
# File 'lib/http-request/client.rb', line 3 def open_url @open_url end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/http-request/client.rb', line 3 def @options end |
Class Method Details
.delete_from_pool(key) ⇒ Object
11 12 13 |
# File 'lib/http-request/client.rb', line 11 def self.delete_from_pool(key) Thread.current[key] = nil end |
Instance Method Details
#close(url = nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/http-request/client.rb', line 36 def close(url=nil) if !url.nil? conn = connection(url) conn.close connections.delete(conn) else connections.each {|conn| conn.close } @connections = [] end end |
#connection(url) ⇒ Object
84 85 86 |
# File 'lib/http-request/client.rb', line 84 def connection(url) connections.find {|conn| conn.server_id == server_id(url) } end |
#open(url) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/http-request/client.rb', line 20 def open(url) # Set default url.. @open_url ||= url # Let's close any dead connections as we open new ones sweep_dead_connections! # Reuse existing connection or open a new one.. conn = connection(url) if conn.nil? || conn.closed? conn = Connection.new(url, ) connections << conn end conn end |
#request(method, url, headers = {}, body = nil) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/http-request/client.rb', line 54 def request(method, url, headers={}, body=nil) url ||= open_url # TODO: raise an exception if the url is invalid... # ie. nil or something else... conn = open(url) raw_response = conn.send(method, url, headers, body) response = Response.new(raw_response, url) # TODO: .. max retries.. keeping track of state.. # is can create a bunch of connections in this process... # but how do we manage that plus the idea of threads..? # each thread has their own Client object.. which is a good thing.. if [:auto_redirect] != false && raw_response.kind_of?(Net::HTTPRedirection) && !response.header['location'].nil? # TODO: .. count max redirects... new_location = response.header['location'] if new_location =~ /^http/i request(method, new_location, headers, body) else absolute_url = uri(url).join(new_location).to_s request(method, absolute_url, headers, body) end else # reset redirect_count .. response end end |
#server_id(url) ⇒ Object
88 89 90 |
# File 'lib/http-request/client.rb', line 88 def server_id(url) uri(url).normalized_site end |
#uri(url) ⇒ Object
92 93 94 |
# File 'lib/http-request/client.rb', line 92 def uri(url) Addressable::URI.parse(url) end |