module Rmega::Net

Public Instance Methods

http_get_content(url) click to toggle source
# File lib/rmega/net.rb, line 18
def http_get_content(url)
  uri = URI(url)
  req = ::Net::HTTP::Get.new(uri.request_uri)
  resp = net_http(uri).request(req)

  if resp.code.to_i == 509 and resp.body.to_s.empty?
    raise BandwidthLimitExceeded.new
  end

  return resp.body
end
http_post(url, data) click to toggle source
# File lib/rmega/net.rb, line 30
def http_post(url, data)
  uri = URI(url)
  req = ::Net::HTTP::Post.new(uri.request_uri)
  req.body = data
  logger.debug("REQ POST #{url} #{cut_string(data)}")

  req['Connection'] = 'keep-alive'

  response = net_http(uri).request(req)
  logger.debug("REP #{response.code} #{cut_string(response.body)}")
  return response
end
survive(retries = options.max_retries) { || ... } click to toggle source
# File lib/rmega/net.rb, line 6
def survive(retries = options.max_retries, &block)
  yield
rescue ServerError
  raise
rescue Exception => error
  retries -= 1
  raise(error) if retries < 0
  logger.debug("[#{error.class}] #{error.message}. #{retries} attempt(s) left.")
  sleep(options.retry_interval)
  retry
end

Private Instance Methods

cut_string(string, max = 50) click to toggle source
# File lib/rmega/net.rb, line 59
def cut_string(string, max = 50)
  return "<binary data, #{string.size} bytes>" if string.encoding == ::Encoding::ASCII_8BIT
  string.size <= max ? string : string[0..max-1]+"..."
end
net_http(uri) click to toggle source
# File lib/rmega/net.rb, line 45
def net_http(uri)
  http = Rmega::ConnPool.get(uri)

  # apply common http options
  http.proxy_from_env = false if options.http_proxy_address

  options.marshal_dump.each do |name, value|
    setter_method = name.to_s.split('http_')[1]
    http.__send__("#{setter_method}=", value) if setter_method and value
  end

  return http
end