Class: S3Share::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/s3share/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Runner

Returns a new instance of Runner.



3
4
5
6
# File 'lib/s3share/runner.rb', line 3

def initialize(*args)
  @args = Args.new(args)
  @filename = args.first
end

Instance Method Details

#clean_filenameObject

Returns only the filename, discarding any directory info.



37
38
39
# File 'lib/s3share/runner.rb', line 37

def clean_filename
  @filename.split("/").last
end

#clipboard_cmdObject

Returns the name of the command that allows you to pipe stuff into the clipboard. ‘pbcopy` for OS X, no idea what it is in other systems.



85
86
87
# File 'lib/s3share/runner.rb', line 85

def clipboard_cmd
  "pbcopy"
end

#get_directoryObject

The user can specify absolute paths, relative paths and individual filenames so we need to make sure we return the proper path to the directory.



26
27
28
29
30
31
32
33
34
# File 'lib/s3share/runner.rb', line 26

def get_directory
  if !@filename.include?("/") # single file name
    "#{Dir.pwd}"
  elsif @filename[0,1] == "/" # absolute path
    "#{@filename.split("/")[0..-2].join("/")}"
  else                        # relative path
    "#{Dir.pwd}/#{@filename.split("/")[0..-2].join("/")}"
  end
end

Finds an error by name and prints the associated error string.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/s3share/runner.rb', line 90

def print_error(err)
  errors = {
    :no_default_bucket =>
    ["\nENV variable AMAZON_S3_DEFAULT_BUCKET has not been set.",
     "Please run:",
     "     export AMAZON_S3_DEFAULT_BUCKET=\"bucket-name\"",
     "\nRead the documentation for more information.\n\n"],
    :wrong_aws_credentials =>
    ["\nAWS credentials are invalid. Make sure that you have set the ENV:",
     "\n     export AMAZON_ACCESS_KEY_ID=\"your-access-key\"",
     "     export AMAZON_SECRET_ACCESS_KEY=\"your-secret-key\"",
     "\nRead the documentation for more information.\n\n"]
  }
  errors[err].each { |msg| puts msg }
end

#runObject

Starts the execution.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/s3share/runner.rb', line 9

def run
  if @filename.nil?
    puts "usage: s3.rb [filename]"
    exit(-1)
  end

  @path = get_directory
  @filename = clean_filename

  # Upload the file and save the URL in the clipboard
  exit_code = set_clipboard_url(upload_file)
  exit(exit_code)
end

#set_clipboard_url(url) ⇒ Object

Saves ‘url` in the clipboard for easy access. `#clipboard_cmd` should be extended for other platforms (right now only OS X is supported).



78
79
80
# File 'lib/s3share/runner.rb', line 78

def set_clipboard_url(url)
  system "echo #{url} | #{clipboard_cmd}"
end

#upload_fileObject

Uploads the file to Amazon S3 and returns the URL for the object. Uploaded files are publicly readable.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/s3share/runner.rb', line 44

def upload_file
  bucket_name = ENV["AMAZON_S3_DEFAULT_BUCKET"] || ""
  access_key = ENV["AMAZON_ACCESS_KEY_ID"] || ""
  secret_key = ENV["AMAZON_SECRET_ACCESS_KEY"] || ""

  if bucket_name.empty?
    print_error(:no_default_bucket)
    exit(-2)
  end

  if access_key.empty? || secret_key.empty?
    print_error(:wrong_aws_credentials)
    exit(-3)
  end

  AWS::S3::Base.establish_connection!(
    :access_key_id     => access_key,
    :secret_access_key => secret_key
  )
  
  create_bucket_if_it_does_not_exist(bucket_name)

  AWS::S3::S3Object.store(@filename, open("#{@path}/#{@filename}"),
                          bucket_name,
                          :access => :public_read)

  url = "http://s3.amazonaws.com/#{bucket_name}/#{@filename}"
  puts "\n #{@filename} uploaded to: #{url}\n\n"
  url
end