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
# File 'lib/rmega/net.rb', line 18

def http_get_content(url)
  uri = URI(url)
  req = ::Net::HTTP::Get.new(uri.request_uri)
  return net_http(uri).request(req).body
end

#http_post(url, data) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rmega/net.rb', line 24

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)}")

  # if you don't use Net::Http#start it will not keep the socket open even if you set
  # the connection header BUT setting the connection header to 'keep-alive' its enough
  # to fool MEGA servers and don't let them reset your connection!
  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