Class: FileUploader

Inherits:
Object
  • Object
show all
Defined in:
lib/captured/file_uploader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ FileUploader

Returns a new instance of FileUploader.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/captured/file_uploader.rb', line 10

def initialize(options)
  @growl_path = options[:growl_path] || "#{File.dirname(File.expand_path(__FILE__))}/../../resources/growlnotify"
  @config = YAML.load_file(options[:config_file])
  case @config['upload']['type']
  when "eval"
    require File.expand_path(File.dirname(__FILE__) + '/uploaders/eval_uploader')
    @uploader = EvalUploader.new(@config)
  when "scp"
    require File.expand_path(File.dirname(__FILE__) + '/uploaders/scp_uploader')
    @uploader = ScpUploader.new(@config)
  when "imgur"
    require File.expand_path(File.dirname(__FILE__) + '/uploaders/imgur_uploader')
    @uploader = ImgurUploader.new
  when "imageshack"
    require File.expand_path(File.dirname(__FILE__) + '/uploaders/imageshack_uploader')
    @uploader = ImageshackUploader.new(@config)
  else
    raise "Invalid Type"
  end
rescue
  growl "Unable to load config file"
  raise "Unable to load config file"
end

Class Method Details

.upload(file, options) ⇒ Object



6
7
8
# File 'lib/captured/file_uploader.rb', line 6

def self.upload(file, options)
  self.new(options).process_upload(file)
end

Instance Method Details

#growl(msg, image = "#{File.dirname(File.expand_path(__FILE__))}/../../resources/red_x.png") ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/captured/file_uploader.rb', line 60

def growl(msg, image = "#{File.dirname(File.expand_path(__FILE__))}/../../resources/red_x.png")
  puts "grr: #{msg}"
  if File.exists? @growl_path
    raise "Growl Failed" unless system("#{@growl_path} -t 'Captured' -m '#{msg}' --image '#{image}'")
  end
rescue
  puts "Growl Notify Error"
end

#pbcopy(str) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/captured/file_uploader.rb', line 34

def pbcopy(str)
  system "ruby -e \"print '#{str}'\" | pbcopy"
  # I prefer the following method but it was being intermitant about actually
  # coping to the clipboard.
  #IO.popen('pbcopy','w+') do |pbc|
  #  pbc.print str
  #end
rescue
  raise "Copy to clipboard failed"
end

#process_upload(file) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/captured/file_uploader.rb', line 45

def process_upload(file)
  remote_name = Digest::MD5.hexdigest(file+Time.now.to_i.to_s) +  File.extname(file)
  growl("Processing Upload", "#{File.dirname(File.expand_path(__FILE__))}/../../resources/action_run.png")
  @uploader.upload(file)
  remote_path = @uploader.url
  puts "Uploaded '#{file}' to '#{remote_path}'"
  pbcopy remote_path
  growl("Upload Succeeded", "#{File.dirname(File.expand_path(__FILE__))}/../../resources/green_check.png")
  History.append(file, remote_path)
rescue => e
  puts e
  puts e.backtrace
  growl(e)
end