Class: Fbox::HttpClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ HttpClient

Returns a new instance of HttpClient.



32
33
34
35
36
# File 'lib/fbox/http_client.rb', line 32

def initialize(options)
  @options = options
  @options.freeze
  @cookies = {}
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



30
31
32
# File 'lib/fbox/http_client.rb', line 30

def options
  @options
end

Instance Method Details

#http_conn(uri) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fbox/http_client.rb', line 49

def http_conn(uri)
  if @options[:proxy_address]
      http_class = Net::HTTP::Proxy(@options[:proxy_address], @options[:proxy_port] ? @options[:proxy_port] : 80)
  else
      http_class = Net::HTTP
  end
  http_conn = http_class.new(uri.host, uri.port)
  http_conn.use_ssl = @options[:use_ssl]
  http_conn.verify_mode = @options[:ssl_verify_mode]
  http_conn
end

#make_request(http_method, path, body = '', headers = {}, form_params = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/fbox/http_client.rb', line 38

def make_request(http_method, path, body='', headers={}, form_params = {})
  request = Net::HTTP.const_get(http_method.to_s.capitalize).new(path, headers)
  request.body = body unless body.nil?
  add_cookies(request) if options[:use_cookies]
  request.set_form_data(form_params) if !form_params.empty? && http_method.to_s.capitalize == 'Post'
  
  response = http_conn(uri).request(request)
  store_cookies(response) if options[:use_cookies]
  response
end

#request(*args) ⇒ Object

Raises:

  • (HTTPError)


65
66
67
68
69
# File 'lib/fbox/http_client.rb', line 65

def request(*args)
  response = make_request(*args)
  raise HTTPError.new(response) unless response.kind_of?(Net::HTTPSuccess)
  response
end

#uriObject



61
62
63
# File 'lib/fbox/http_client.rb', line 61

def uri
  URI.parse(@options[:site])
end