Module: Rmega::Net

Includes:
Loggable, Options
Included in:
Rmega::Nodes::Downloadable, Rmega::Nodes::Uploadable, Session
Defined in:
lib/rmega/net.rb

Instance Method Summary collapse

Methods included from Options

included, #options

Methods included from Loggable

included, #logger

Instance Method Details

#http_get_content(url) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# 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) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# 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, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# 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