Class: BayUploader

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

Overview

A simple command line tool for uploading images to bayimg.com

Author

Britt Crawford ([email protected])

License

Licensed under Creative Commons Attribution-Share Alike 3.0 License

<a rel=“license” href=“creativecommons.org/licenses/by-sa/3.0/”> <img alt=“Creative Commons License” style=“border-width:0” src=“” /> </a> <br />This work is licensed under a <a rel=“license” href=“creativecommons.org/licenses/by-sa/3.0/”>Creative Commons Attribution-Share Alike 3.0 License</a>.

Constant Summary collapse

VERSION =
'1.0.1'
BayImgUrl =
'bayimg.com'
Usage =
'Usage: bay_uploader filename [options]'

Class Method Summary collapse

Class Method Details

.encode(file_name) ⇒ Object



74
75
76
# File 'lib/bayuploader.rb', line 74

def self.encode(file_name) 
  Base64.encode64(file_name.split(File::Separator).last.gsub(/\s/,'-')).gsub(/[^\w\d]/,'')
end

.parse_options(argv) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bayuploader.rb', line 56

def self.parse_options(argv)
  file_name = argv.shift
  opts = {:code => BayUploader.encode(file_name), :tags => []}
  
  parser = OptionParser.new do |parser|
    parser.on('-c CODE', '--code CODE',"Specify removal code") do |code|
      opts[:code] = code
    end
    
    parser.on('--tags [TAGS]', Array,"Specify tags to be applied") do |tags|
      opts[:tags] = tags
    end
  end
  
  parser.parse argv
  {:file => file_name, :code => opts[:code], :tags => opts[:tags]}
end

.process_response(html) ⇒ Object



50
51
52
53
54
# File 'lib/bayuploader.rb', line 50

def self.process_response(html)
  doc = Hpricot(html)
  link = doc.at("div#extra2/a")
  link.nil? ? nil : link['href']
end

.upload(argv) ⇒ Object



26
27
28
29
30
31
# File 'lib/bayuploader.rb', line 26

def self.upload(argv)
  options = BayUploader.parse_options(argv)
  response = BayUploader.upload_file(options[:file], options[:code], options[:tags])
  image_id = BayUploader.process_response(response)
  image_id.nil? ? nil : ["http://#{BayImgUrl}#{image_id}",options[:code]]
end

.upload_file(file_name, removal_code, tags) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bayuploader.rb', line 33

def self.upload_file(file_name, removal_code, tags)
  params = {}
  
  begin
    params['code'] = removal_code
    params['tags'] = tags.join(' ')
    file = File.new(file_name)
    params['file'] = file
    mp = Multipart::MultipartPost.new
    query,headers = mp.prepare_query(params)
  ensure
    file.close unless file.nil?
  end
  
  BayUploader.post(query,headers).body
end