Class: Speedtest::TransferWorker

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

Instance Method Summary collapse

Constructor Details

#initialize(url, logger) ⇒ TransferWorker

Returns a new instance of TransferWorker.



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

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

Instance Method Details

#downloadObject



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

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

  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
  status
end

#log(msg) ⇒ Object



12
13
14
15
# File 'lib/speedtest/transfer_worker.rb', line 12

def log(msg)
  return unless @logger
  @logger.debug msg
end

#upload(content) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/speedtest/transfer_worker.rb', line 30

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

  page = HTTParty.post(@url, :body => { "content" => content }, timeout: 10)
  log "upload response body = [#{page.body}]"
  unless page.code / 100 == 2
    error "GET #{@url} failed with code #{page.code}"
    status.error = true
  end
  status.size = page.body.split('=')[1].to_i
  status
end