class VirusScanService::Courier

Constants

RequestNotSuccessful

Attributes

logger[RW]
num_of_scans[RW]
token[R]

Public Class Methods

new(options) click to toggle source
# File lib/virus_scan_service/courier.rb, line 10
def initialize(options)
  @token      = options.fetch(:token)
  @host  = options.fetch(:host)
  @num_of_scans = 1
  @logger = DefaultLogger.new
end

Public Instance Methods

call() { |fetch('file_url')| ... } click to toggle source
# File lib/virus_scan_service/courier.rb, line 17
def call
  scheduled_scans
    .first(num_of_scans)
    .each do |scheduled_scan|
      result = yield(scheduled_scan.fetch('file_url'))
      update_scan_result(scheduled_scan.fetch('id'), result)
    end
end

Private Instance Methods

check_status(response) { || ... } click to toggle source
# File lib/virus_scan_service/courier.rb, line 76
def check_status(response)
  if response.class == Net::HTTPOK
    logger.info "Response status OK 200"
    json(yield)
  else
    logger.info "Response status #{response.class}"
    logger.info yield
    raise RequestNotSuccessful
  end
end
json(body) click to toggle source
# File lib/virus_scan_service/courier.rb, line 69
def json(body)
  logger.debug "Response body #{body}"
  JSON
    .parse(body)
    .fetch("data")
end
scheduled_scans() click to toggle source
# File lib/virus_scan_service/courier.rb, line 32
def scheduled_scans
  uri.path = '/wd/virus_scans'
  logger.info "GET #{uri.to_s}"

  http = build_http
  scans_req = Net::HTTP::Get.new(uri.to_s)
  scans_req.add_field("Authorization", "Token #{token}")
  scans_req['Accept'] ='application/json'

  response = http.request(scans_req)

  check_status(response) {
    response.body  # array of virus_scans
  }
end
update_scan_result(scan_id, result) click to toggle source
# File lib/virus_scan_service/courier.rb, line 48
def update_scan_result(scan_id, result)
  uri.path = "/wd/virus_scans/#{scan_id}"
  logger.info "PUT #{uri.to_s}"

  http = build_http

  scan_push_req = Net::HTTP::Put.new(uri.to_s)
  scan_push_req.add_field("Authorization", "Token #{token}")
  scan_push_req['Accept'] = 'application/json'
  scan_push_req.add_field('Content-Type', 'application/json')
  scan_push_req
    .body = {"virus_scan" => {'scan_result' => result}}
    .to_json

  response = http.request(scan_push_req)

  check_status(response) {
    response.body  # result JSON
  }
end
uri() click to toggle source
# File lib/virus_scan_service/courier.rb, line 28
def uri
  @uri ||= URI.parse(@host)
end