class RainforestCli::Uploader::UploadableParser

Public Class Methods

new(rfml_test, test_id, uploaded_files) click to toggle source
# File lib/rainforest_cli/uploader/uploadable_parser.rb, line 6
def initialize(rfml_test, test_id, uploaded_files)
  @rfml_test = rfml_test
  @test_id = test_id
  @uploaded_files = uploaded_files
end

Public Instance Methods

add_file_to_uploaded_collection(file, aws_info) click to toggle source
# File lib/rainforest_cli/uploader/uploadable_parser.rb, line 106
def add_file_to_uploaded_collection(file, aws_info)
  @uploaded_files.push({
                         'id' => aws_info['file_id'],
                         'signature' => aws_info['file_signature'],
                         'digest' => file_digest(file),
                       })
end
file_already_uploaded?(file) click to toggle source
# File lib/rainforest_cli/uploader/uploadable_parser.rb, line 114
def file_already_uploaded?(file)
  @uploaded_files.any? { |f| f['digest'] == file_digest(file) }
end
get_aws_upload_info(file) click to toggle source
# File lib/rainforest_cli/uploader/uploadable_parser.rb, line 53
def get_aws_upload_info(file)
  if file_already_uploaded?(file)
    aws_info = get_uploaded_data(file)
  else
    aws_info = upload_to_rainforest(file)
    add_file_to_uploaded_collection(file, aws_info)
    upload_to_aws(file, aws_info)
  end

  aws_info
end
get_uploaded_data(file) click to toggle source
# File lib/rainforest_cli/uploader/uploadable_parser.rb, line 118
def get_uploaded_data(file)
  file_data = @uploaded_files.find { |f| f['digest'] == file_digest(file) }
  {
    'file_signature' => file_data['signature'],
    'file_id' => file_data['id'],
  }
end
parse_action_files(step) click to toggle source
# File lib/rainforest_cli/uploader/uploadable_parser.rb, line 20
def parse_action_files(step)
  step.uploadable_in_action.each do |match|
    step.action = replace_paths_in_text(step.action, match)
  end
end
parse_files!() click to toggle source
# File lib/rainforest_cli/uploader/uploadable_parser.rb, line 12
def parse_files!
  @rfml_test.steps.each do |step|
    next if step.type == :test
    parse_action_files(step) if step.uploadable_in_action
    parse_response_files(step) if step.uploadable_in_response
  end
end
parse_response_files(step) click to toggle source
# File lib/rainforest_cli/uploader/uploadable_parser.rb, line 26
def parse_response_files(step)
  step.uploadable_in_response.each do |match|
    step.response = replace_paths_in_text(step.response, match)
  end
end
replace_paths_in_text(text, match) click to toggle source
# File lib/rainforest_cli/uploader/uploadable_parser.rb, line 32
def replace_paths_in_text(text, match)
  step_var, relative_file_path = match
  file_path = File.expand_path(File.join(test_directory, relative_file_path))

  unless File.exist?(file_path)
    logger.warn "\tError for test: #{@rfml_test.file_name}:"
    logger.warn "\t\tNo such file exists: #{File.basename(file_path)}"
    return text
  end

  file = File.open(file_path, 'rb')
  aws_info = get_aws_upload_info(file)

  sig = aws_info['file_signature'][0...6]
  if step_var == 'screenshot'
    text.gsub(relative_file_path, "#{aws_info['file_id']}, #{sig}")
  elsif step_var == 'download'
    text.gsub(relative_file_path, "#{aws_info['file_id']}, #{sig}, #{File.basename(file_path)}")
  end
end
upload_to_aws(file, aws_info) click to toggle source
# File lib/rainforest_cli/uploader/uploadable_parser.rb, line 85
def upload_to_aws(file, aws_info)
  logger.info "\tUploading file data for #{file.path}..."

  resp = RainforestCli::Uploader::MultiFormPostRequest.request(
    aws_info['aws_url'],
    'key' => aws_info['aws_key'],
    'AWSAccessKeyId' => aws_info['aws_access_id'],
    'acl' => aws_info['aws_acl'],
    'policy' => aws_info['aws_policy'],
    'signature' => aws_info['aws_signature'],
    'Content-Type' => MimeMagic.by_path(file),
    'file' => file,
  )

  unless resp.code.between?(200, 299)
    logger.fatal "\tThere was a problem with uploading your file: #{file.path}."
    logger.fatal "\t\t#{resp.to_json}"
    exit 3
  end
end
upload_to_rainforest(file) click to toggle source
# File lib/rainforest_cli/uploader/uploadable_parser.rb, line 65
def upload_to_rainforest(file)
  logger.info "\tUploading file metadata for #{file.path}..."

  resp = http_client.post(
    "/tests/#{@test_id}/files",
    mime_type: MimeMagic.by_path(file).to_s,
    size: file.size,
    name: File.basename(file.path),
    digest: file_digest(file)
  )

  if resp['aws_url'].nil?
    logger.fatal "\tThere was a problem with uploading your file: #{file_path}."
    logger.fatal "\t\t#{resp.to_json}"
    exit 2
  end

  resp
end

Private Instance Methods

file_digest(file) click to toggle source
# File lib/rainforest_cli/uploader/uploadable_parser.rb, line 128
def file_digest(file)
  Digest::MD5.file(file).hexdigest
end
http_client() click to toggle source
# File lib/rainforest_cli/uploader/uploadable_parser.rb, line 136
def http_client
  RainforestCli.http_client
end
logger() click to toggle source
# File lib/rainforest_cli/uploader/uploadable_parser.rb, line 140
def logger
  RainforestCli.logger
end
test_directory() click to toggle source
# File lib/rainforest_cli/uploader/uploadable_parser.rb, line 132
def test_directory
  @test_directory ||= File.dirname(@rfml_test.file_name)
end