class Motion::Screenspecs::Servlet

Public Instance Methods

do_GET(request, response) click to toggle source
# File lib/motion-screenspecs.rb, line 104
def do_GET (request, response)
  if (title = request.query["title"]) &&
      (screenshot_path = request.query["screenshot_path"]) &&
      (screenshot_class = request.query['screenshot_class'])
    screenshot_path.gsub!("file://", "")
    screenshots_root = Motion::Screenspecs.screenshots_root(screenshot_class)
    expectation_path = File.expand_path(File.join(screenshots_root, "expectations", "#{title}.png"))
    result_path = File.expand_path(File.join(screenshots_root, "results", "#{title}.png"))
    failure_path = File.expand_path(File.join(screenshots_root, "failures", "#{title}.png"))
    FileUtils.mkdir_p(File.dirname(failure_path))
    FileUtils.mkdir_p(File.dirname(result_path))

    File.delete(result_path) if File.exists?(result_path)
    temp_result_path = File.join(File.dirname(result_path), File.basename(screenshot_path))
    FileUtils.cp(screenshot_path, File.dirname(result_path))
    FileUtils.mv(temp_result_path, result_path)

    File.delete(failure_path) if File.exists?(failure_path)

    percentage = Motion::Screenspecs::ImageDiff.new.percentage(expectation_path, screenshot_path, failure_path)
    success = percentage < Motion::Screenspecs.tolerance
    response.status = success ? 200 : 400
    response.content_type = "text/plain"
    response.body = "%05.2f" % percentage
  else
    response.status = 404
    response.body = "You did not provide the correct parameters"
  end
end