Class: Speedtest::TransferWorker

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Logging
Defined in:
lib/speedtest/transfer_worker.rb

Instance Method Summary collapse

Methods included from Logging

#error, #log

Constructor Details

#initialize(url, logger) ⇒ TransferWorker

Returns a new instance of TransferWorker.



8
9
10
11
# File 'lib/speedtest/transfer_worker.rb', line 8

def initialize(url, logger)
  @url = url
  @logger = logger
end

Instance Method Details

#downloadObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/speedtest/transfer_worker.rb', line 13

def download
  log "  downloading: #{@url}"
  status = ThreadStatus.new(false, 0)

  begin
    page = HTTParty.get(@url, timeout: 10)
    unless page.code / 100 == 2
      error "GET #{@url} failed with code #{page.code}"
      status.error = true
    end
    status.size = page.body.length
  rescue Timeout::Error, Net::HTTPNotFound, Net::OpenTimeout, Errno::ENETUNREACH => e
    error "GET #{@url} failed with exception #{e.class}"
    status.error = true
  end
  status
end

#upload(content) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/speedtest/transfer_worker.rb', line 31

def upload(content)
  log "  uploading: #{@url}"
  status = ThreadStatus.new(false, 0)

  begin
    page = HTTParty.post(@url, :body => { "content" => content }, timeout: 10)
    unless page.code / 100 == 2
      error "POST #{@url} failed with code #{page.code}"
      status.error = true
    end
    status.size = page.body.split('=')[1].to_i
  rescue Timeout::Error, Net::HTTPNotFound, Net::OpenTimeout, Errno::ENETUNREACH => e
    error "POST #{@url} failed with exception #{e.class}"
    status.error = true
  end
  status
end