class APIWrapper

Constants

BASE_URL

Attributes

op_file[RW]

Public Class Methods

new(attributes) click to toggle source
# File lib/file_sentry/api_wrapper.rb, line 7
def initialize(attributes)
  attributes.each {|attribute, value| self.send("#{attribute}=", value)}
end

Public Instance Methods

scan_file() click to toggle source
# File lib/file_sentry/api_wrapper.rb, line 11
def scan_file
  hash_response = get_hash
  if does_hash_exist?(hash_response, op_file.hash)
    set_data_id(hash_response)
  else
    set_data_id(post_file)
  end
  monitor_scan
end

Private Instance Methods

api_key() click to toggle source
# File lib/file_sentry/api_wrapper.rb, line 23
def api_key
  FileSentry.configuration.access_key
end
does_hash_exist?(response, hash) click to toggle source
# File lib/file_sentry/api_wrapper.rb, line 73
def does_hash_exist?(response, hash)
  if response[hash]
    false
  else
    true
  end
end
error_check(response) click to toggle source
# File lib/file_sentry/api_wrapper.rb, line 81
def error_check(response)
  raise "Error: #{response.message}" if response.message != "OK"
end
find_progress(response) click to toggle source
# File lib/file_sentry/api_wrapper.rb, line 89
def find_progress(response)
  begin
    progress = response["scan_results"]["progress_percentage"]
  rescue
    progress = response["scan_results"]["scan_details"]["progress_percentage"]
  end
  progress ||= 0
end
get_data_id() click to toggle source
# File lib/file_sentry/api_wrapper.rb, line 59
def get_data_id
  raise "No data_id set" if op_file.data_id.nil?
  response = HTTParty.get(
    "#{BASE_URL}/file/#{op_file.data_id}",
    headers: {"apikey"=> api_key}
  )
  error_check(response)
  response
end
get_hash() click to toggle source
# File lib/file_sentry/api_wrapper.rb, line 49
def get_hash
  raise "No hash set" if op_file.hash.nil?
  response = HTTParty.get(
    "#{BASE_URL}/hash/#{op_file.hash}",
    headers: {"apikey"=> api_key}
  )
  error_check(response)
  response
end
is_scan_complete?(response) click to toggle source
# File lib/file_sentry/api_wrapper.rb, line 85
def is_scan_complete?(response)
  find_progress(response) == 100
end
monitor_scan() click to toggle source
# File lib/file_sentry/api_wrapper.rb, line 27
def monitor_scan
  response = get_data_id
  seconds = 0
  until is_scan_complete?(response)
    sleep(1)
    response = get_data_id
    seconds += 1
    raise "Error: API timeout" if seconds > 120
  end
  op_file.scan_results = response["scan_results"]
end
post_file() click to toggle source
# File lib/file_sentry/api_wrapper.rb, line 39
def post_file
  response = HTTParty.post(
      "#{BASE_URL}/file/",
      headers: {"apikey"=> api_key},
      body: {"filename" => File.open(op_file.filepath)}
    )
  error_check(response)
  response
end
set_data_id(response) click to toggle source
# File lib/file_sentry/api_wrapper.rb, line 69
def set_data_id(response)
  op_file.data_id = response["data_id"]
end