module Emblaze

Constants

VERSION

Public Class Methods

access_key() click to toggle source
# File lib/emblaze/emblaze.rb, line 7
def access_key; ENV['AWS_ACCESS_KEY']; end
bucket_name() click to toggle source
# File lib/emblaze/emblaze.rb, line 10
def bucket_name;  ENV['AWS_BUCKET_NAME']; end
file_types() click to toggle source
# File lib/emblaze/emblaze.rb, line 12
def file_types
  { ".html" => { type: "text/html" },
    ".css" => { type: "text/css" },
    ".js" => { type: "application/javascript" }
  }
end
gzip(data) click to toggle source
# File lib/emblaze/emblaze.rb, line 31
def gzip(data)
  sio = StringIO.new
  gz = Zlib::GzipWriter.new(sio)
  gz.write(data)
  gz.close
  sio.string
end
install_tasks() click to toggle source
# File lib/emblaze/tasks.rb, line 7
def install_tasks
  desc "Deploy via S3"
  task :s3 do

    Dotenv.load
    page = s3.list_objects(bucket: bucket_name)

    p "deleting content"
    loop do
      page.contents.each do |ob|
        s3.delete_object bucket: bucket_name, key: ob.key
      end
      break unless page.next_page?
      page = page.next_page
    end

    p "uploading _site"

    traverse_directory(local_dir).flatten.compact.each do |f|
      key = f.gsub(local_dir+"/", "")
      ext = File.extname(f)
      if file_types.keys.include? ext
        upload_compressed_object(key, f, ext)
      else
        upload_object(key, f, ext)
      end
    end

    p "s3 deploy complete"
  end

  # Shamelessly copied from
  # https://gist.github.com/rrevanth/9377cb1f1664bf610e38#file-rakefile-L43
  # https://github.com/stereobooster/jekyll-press/issues/26
  desc "Minify site"
  task :minify do
    puts "\n## Compressing static assets"
    original = 0.0
    compressed = 0
    Dir.glob("_site/**/*.*") do |file|
      case File.extname(file)
      when ".css", ".gif", ".html", ".jpg", ".jpeg", ".js", ".png", ".xml"
        puts "Processing: #{file}"
        original += File.size(file).to_f
        min = Reduce.reduce(file)
        File.open(file, "w") do |f|
          f.write(min)
        end
        compressed += File.size(file)
      else
        puts "Skipping: #{file}"
      end
    end
    puts "Total compression %0.2f\%" % (((original-compressed)/original)*100)
  end
end
local_dir() click to toggle source
# File lib/emblaze/emblaze.rb, line 5
def local_dir; './_site'; end
region() click to toggle source
# File lib/emblaze/emblaze.rb, line 9
def region;  ENV['AWS_REGION']; end
s3() click to toggle source
# File lib/emblaze/emblaze.rb, line 57
def s3
  @@s3 ||= Aws::S3::Client.new(
    region: region,
    credentials: Aws::Credentials.new(access_key, secret_key)
  )
end
secret_key() click to toggle source
# File lib/emblaze/emblaze.rb, line 8
def secret_key; ENV['AWS_SECRET_KEY']; end
traverse_directory(path) click to toggle source
# File lib/emblaze/emblaze.rb, line 19
def traverse_directory(path)
  Dir.entries(path).map do |f|
    next if [".", ".."].include? f
    f_path = File.join(path, f)
    if File.directory? f_path
      traverse_directory f_path
    else
      f_path
    end
  end
end
upload_compressed_object(key, f, ext) click to toggle source
# File lib/emblaze/emblaze.rb, line 39
def upload_compressed_object(key, f, ext)
  s3.put_object bucket: bucket_name,
                key: key,
                body: gzip(File.read(f)),
                acl: "public-read",
                content_type: file_types[ext][:type],
                content_encoding: "gzip",
                cache_control: "max-age=604800"
end
upload_object(key, f, ext) click to toggle source
# File lib/emblaze/emblaze.rb, line 49
def upload_object(key, f, ext)
  s3.put_object bucket: bucket_name,
                key: key,
                body: File.open(f),
                acl: "public-read",
                cache_control: "max-age=604800"
end