class DearS3::Cli::ClientHelper

Attributes

s3_client[R]

Public Class Methods

new(s3_client) click to toggle source
# File lib/dears3/cli/client_helper.rb, line 6
def initialize s3_client
  @s3_client = s3_client

  name = get_bucket_name

  s3_client.set_bucket name
end

Public Instance Methods

current_dir_to_bucket_name() click to toggle source
# File lib/dears3/cli/client_helper.rb, line 28
def current_dir_to_bucket_name
  File.basename(Dir.getwd).gsub('_', '-')
end
publish() click to toggle source
# File lib/dears3/cli/client_helper.rb, line 32
def publish
  bucket_files = s3_client.files_in_bucket

  if bucket_files.empty?
    abort "Bucket is empty. Please upload at least one file before publishing"
  end

  say "Files currently in your bucket:"
  say bucket_files.join(" | "), :green
  index_doc = request_doc "Pick your bucket's index document:"
  error_doc = request_doc "Pick your bucket's error document:"

  say "Publishing your bucket. This may take a while..."
  bucket_url = s3_client.configure_website index_doc, error_doc
  say "Bucket published at #{ bucket_url }."
end
unpublish() click to toggle source
# File lib/dears3/cli/client_helper.rb, line 49
def unpublish
  bucket_url = s3_client.remove_website
  say "Removed #{ bucket_url } from the web."
end
upload() click to toggle source
# File lib/dears3/cli/client_helper.rb, line 14
def upload
  bucket_name = s3_client.bucket_name

  say "Uploading files to bucket '#{ bucket_name }'."

  begin
    s3_client.walk_and_upload ".", status_proc
  rescue ::AWS::S3::Errors::Forbidden
    alert_access_denied
    exit
  end
  say "Done syncing bucket."
end

Private Instance Methods

alert_access_denied() click to toggle source
# File lib/dears3/cli/client_helper.rb, line 94
def alert_access_denied
  say "Access denied!", :red
  say "Make sure your credentials are correct and your bucket name isn't already taken by someone else."
  say "Note: AWS bucket names are shared across all users."
  say
end
default_bucket_name() click to toggle source
# File lib/dears3/cli/client_helper.rb, line 78
def default_bucket_name
  File.basename(Dir.getwd).gsub('_', '-').downcase
end
get_bucket_name() click to toggle source
# File lib/dears3/cli/client_helper.rb, line 58
def get_bucket_name
  bucket_name = default_bucket_name

  if s3_client.validate_bucket_name(bucket_name)
    bucket_name = ask "Please select your bucket's name:"
  end

  while error = s3_client.validate_bucket_name(bucket_name)
    bucket_name = ask "#{ error } bucket name. Please select another:"
  end

  if s3_client.new_bucket? bucket_name
    choice = ask "Creating new bucket '#{ bucket_name }'. Continue? (y/n/abort)" end

  return get_bucket_name if %w( n no N No NO ).include? choice
  exit if choice == "abort"

  bucket_name
end
request_doc(request_message) click to toggle source
# File lib/dears3/cli/client_helper.rb, line 82
def request_doc request_message
  doc = ask request_message
  files_in_bucket = s3_client.files_in_bucket

  until files_in_bucket.include? doc
    say "No such file in your bucket. Please choose one from this list:"
    doc = ask files_in_bucket.join(" | ") + "\n", :green
  end

  doc
end
status_proc() click to toggle source
# File lib/dears3/cli/client_helper.rb, line 101
def status_proc
  # TODO: Confirm overriding files
  Proc.new do |entry, status|
    case status
    when :unchanged
      say "\tUnchanged: #{ entry }", :blue
    when :update
      say "\tUpdating: '#{ entry }'", :yellow
    when :upload
    say "\tUploading: '#{ entry }'", :green
    end
  end
end