Class: Bitly::HTTP::Adapters::NetHTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/bitly/http/adapters/net_http.rb

Constant Summary collapse

DEFAULT_OPTS =
{ use_ssl: true }

Instance Method Summary collapse

Constructor Details

#initialize(proxy_addr: nil, proxy_port: nil, proxy_user: nil, proxy_pass: nil, request_opts: {}) ⇒ NetHTTP

Returns a new instance of NetHTTP.



11
12
13
14
15
16
17
# File 'lib/bitly/http/adapters/net_http.rb', line 11

def initialize(proxy_addr: nil, proxy_port: nil, proxy_user: nil, proxy_pass: nil, request_opts: {})
  @request_opts = DEFAULT_OPTS.merge(request_opts)
  @proxy_addr = proxy_addr
  @proxy_port = proxy_port
  @proxy_user = proxy_user
  @proxy_pass = proxy_pass
end

Instance Method Details

#request(request) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bitly/http/adapters/net_http.rb', line 19

def request(request)
  Net::HTTP.start(request.uri.host, request.uri.port, @proxy_addr, @proxy_port, @proxy_user, @proxy_pass, @request_opts) do |http|
    method = Object.const_get("Net::HTTP::#{request.method.capitalize}")
    full_path = request.uri.path
    full_path += "?#{request.uri.query}" if request.uri.query
    http_request = method.new full_path
    http_request.body = request.body
    request.headers.each do |header, value|
      http_request[header] = value
    end
    response = http.request http_request
    success = response.kind_of? Net::HTTPSuccess
    return [response.code, response.body, response.to_hash, success]
  end
end