Class: Net::HTTPClient
- Inherits:
-
Object
- Object
- Net::HTTPClient
- Defined in:
- lib/net/http_client.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#keep_alive ⇒ Object
writeonly
Sets the attribute keep_alive.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Class Method Summary collapse
Instance Method Summary collapse
- #get(path, header = {}, options = {}) ⇒ Object
-
#initialize(host, port = 80, timeout = 15) ⇒ HTTPClient
constructor
A new instance of HTTPClient.
- #keep_alive? ⇒ Boolean
- #to_uri(path = nil) ⇒ Object
Constructor Details
#initialize(host, port = 80, timeout = 15) ⇒ HTTPClient
Returns a new instance of HTTPClient.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/net/http_client.rb', line 28 def initialize(host, port=80, timeout=15) @host = host.to_s.strip @port = port.to_i @keep_alive = true @timeout = timeout.to_i @user_agent = "Net::HTTPClient/0.1 (Ruby #{RUBY_VERSION})" @http = Net::HTTP.new(self.host, self.port) @http.read_timeout = self.timeout @http.open_timeout = self.timeout end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
24 25 26 |
# File 'lib/net/http_client.rb', line 24 def host @host end |
#keep_alive=(value) ⇒ Object (writeonly)
Sets the attribute keep_alive
25 26 27 |
# File 'lib/net/http_client.rb', line 25 def keep_alive=(value) @keep_alive = value end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
24 25 26 |
# File 'lib/net/http_client.rb', line 24 def port @port end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
24 25 26 |
# File 'lib/net/http_client.rb', line 24 def timeout @timeout end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
26 27 28 |
# File 'lib/net/http_client.rb', line 26 def user_agent @user_agent end |
Class Method Details
.from_storage(host, port = 80, renew = false) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/net/http_client.rb', line 13 def from_storage(host, port=80, renew=false) if self.storage.has_key?(host) and not renew connection = self.storage[host] else connection = self.new(host, port) self.storage[host] = connection end return connection end |
.storage ⇒ Object
9 10 11 |
# File 'lib/net/http_client.rb', line 9 def storage @storage ||= {} end |
Instance Method Details
#get(path, header = {}, options = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/net/http_client.rb', line 43 def get(path, header={}, ={}) uri = path.is_a?(URI) ? path : self.to_uri(path) header['Accept'] ||= '*/*' header['Connection'] ||= (self.keep_alive? ? 'Keep-Alive' : 'Close') header['Referer'] = uri.to_s if [:referer_self] header['User-Agent'] ||= self.user_agent @http.start unless @http.started? response = nil begin response = @http.request_get(uri.path, header) rescue EOFError @http = Net::HTTP.new(self.host, self.port) @http.start response = @http.request_get(uri.path, header) end if response.is_a?(Net::HTTPRedirection) && [:follow_redirects] uri_redirect = URI.parse(response['Location']) header_redirect = [:follow_with_header] ? header : [:follow_header] || {} = [:follow_with_options] ? : [:follow_options] || {} ['Referer'] ||= uri.to_s connection = self.class.from_storage(uri_redirect.host, uri_redirect.port) response = connection.get(uri_redirect, header_redirect, ) end return response end |
#keep_alive? ⇒ Boolean
39 40 41 |
# File 'lib/net/http_client.rb', line 39 def keep_alive? @keep_alive ? true : false end |
#to_uri(path = nil) ⇒ Object
69 70 71 |
# File 'lib/net/http_client.rb', line 69 def to_uri(path=nil) return URI::HTTP.build({:host => self.host, :port => self.port, :path => path}) end |