Skip to content

Instantly share code, notes, and snippets.

@sgrif

sgrif/job.rb Secret

Last active August 29, 2015 14:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sgrif/5920ca3d46a76b9286a0 to your computer and use it in GitHub Desktop.
Save sgrif/5920ca3d46a76b9286a0 to your computer and use it in GitHub Desktop.
class S3FileCopier < Struct.new(:model_file, :filepath, :bucket_name, :additional_transformations)
def initialize(model_file, filepath, bucket_name, additional_transformations = [])
super
end
def copy
expected_file.write(transformed_file, content_encoding: :deflate)
end
handle_asynchronously :copy
private
def expected_file
bucket.objects[model_file.path.gsub(/^\//, '')]
end
def transformed_file
additional_transformations.reduce(original_file.read) do |file, transformer|
transformer.apply(file)
end
end
def original_file
bucket.objects[original_file_path]
end
def bucket
AWS.s3.buckets[bucket_name]
end
def original_file_path
CGI.unescape(stripped_file_name)
end
def stripped_file_name
filepath.gsub(/^\/#{bucket_name}\//, '')
end
end
module NullFileTransformer
def self.apply(data)
data
end
end
class ZlibFileTransformer
def initialize(level)
# Yes, this really does vary, we have a case where we can't use
# BEST_COMPRESSION
@level = level
end
def apply(data)
Zlib.deflate(data, level)
end
end
class BinaryFileConverter
def initialize(json_format_verson = 3)
@json_format_verson = json_format_version
end
def apply(json_string)
# Lots of intense binary conversion!
end
end
class ImageCompressor
def apply(image_data)
# Shells out to ImageMagick
end
end
class CodeMinifier
def initialize(whitelisted_method_names = [], include_block_arguments = true)
end
def apply(ruby_code)
# You get the picture
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment