class Happo::Uploader
Handles uploading diffs (serialized html doc + images) to an Amazon S3 account.
Public Class Methods
new()
click to toggle source
# File lib/happo/uploader.rb, line 9 def initialize @s3_access_key_id = ENV['S3_ACCESS_KEY_ID'] @s3_secret_access_key = ENV['S3_SECRET_ACCESS_KEY'] @s3_bucket_name = ENV['S3_BUCKET_NAME'] @s3_bucket_path = ENV['S3_BUCKET_PATH'] end
Public Instance Methods
upload_diffs(triggered_by_url)
click to toggle source
# File lib/happo/uploader.rb, line 16 def upload_diffs(triggered_by_url) result_summary = Happo::Utils.last_result_summary return [] if result_summary[:diff_examples].empty? && result_summary[:new_examples].empty? diff_images = result_summary[:diff_examples].map do |example| example[:previous] = upload_image(example, 'previous') example[:current] = upload_image(example, 'current') example end new_images = result_summary[:new_examples].map do |example| example[:current] = upload_image(example, 'current') example end html = build_object('index.html') path = File.expand_path( File.join(File.dirname(__FILE__), 'views', 'diffs.erb') ) html.content = ERB.new(File.read(path)).result(binding) html.content_encoding = 'utf-8' html.content_type = 'text/html' html.save html.url end
Private Instance Methods
build_object(file_name)
click to toggle source
# File lib/happo/uploader.rb, line 56 def build_object(file_name) find_or_build_bucket.objects.build("#{directory}/#{file_name}") end
directory()
click to toggle source
# File lib/happo/uploader.rb, line 60 def directory @directory ||= begin if @s3_bucket_path.nil? || @s3_bucket_path.empty? SecureRandom.uuid else File.join(@s3_bucket_path, SecureRandom.uuid) end end end
find_or_build_bucket()
click to toggle source
# File lib/happo/uploader.rb, line 46 def find_or_build_bucket @bucket ||= begin service = S3::Service.new(access_key_id: @s3_access_key_id, secret_access_key: @s3_secret_access_key) bucket = service.bucket(@s3_bucket_name) bucket.save(location: :us) unless bucket.exists? bucket end end
upload_image(example, variant)
click to toggle source
# File lib/happo/uploader.rb, line 70 def upload_image(example, variant) img_name = "#{example[:description]}_#{example[:viewport]}_#{variant}.png" image = build_object(img_name) image.content = open(Happo::Utils.path_to(example[:description], example[:viewport], "#{variant}.png")) image.content_type = 'image/png' image.save # http://stackoverflow.com/questions/2834034/how-do-i-raw-url-encode-decode-in-javascript-and-ruby-to-get-the-same-values-in-b URI.escape(img_name, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) end