Class: Net::HTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/net/http_client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#hostObject (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

Parameters:

  • value

    the value to set the attribute keep_alive to.



25
26
27
# File 'lib/net/http_client.rb', line 25

def keep_alive=(value)
  @keep_alive = value
end

#portObject (readonly)

Returns the value of attribute port.



24
25
26
# File 'lib/net/http_client.rb', line 24

def port
  @port
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



24
25
26
# File 'lib/net/http_client.rb', line 24

def timeout
  @timeout
end

#user_agentObject

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

.storageObject



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={}, options={})
  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 options[: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) && options[:follow_redirects]
    uri_redirect = URI.parse(response['Location'])
    header_redirect = options[:follow_with_header] ? header : options[:follow_header] || {}
    options_redirect = options[:follow_with_options] ? options : options[:follow_options] || {}
    options_redirect['Referer'] ||= uri.to_s 
    connection = self.class.from_storage(uri_redirect.host, uri_redirect.port)
    response = connection.get(uri_redirect, header_redirect, options_redirect)
  end
  return response
end

#keep_alive?Boolean

Returns:

  • (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