Class: Paste2::Client

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/paste2/client.rb', line 10

def initialize
  @options = {}
  parser = OptionParser.new do|opts|
    opts.separator ''
    opts.separator 'Examples:'
    opts.separator "    echo 'code' | paste2"
    opts.separator '    paste2 < file'
    opts.separator ''
    opts.separator 'Specific options:'
    opts.banner = 'Usage: paste2 [options]'
    opts.on( '-f', '--file FILE', 'Post content from file' ) do|file|
      @options[:file] = file
    end
    opts.on( '-d', '--description TEXT', 'Description for post' ) do|description|
      @options[:description] = description
    end
    opts.on( '-a', '--all', 'List supported languages' ) do
      puts 'Supported languages:'
      Paste2::LANGUAGES.each do |lang, name|
        printf "\t%-20s %s\n", lang, name
      end
      exit(0)
    end
    opts.on( '-l', '--lang LANG', 'Post content as language (default is text)' ) do|language|
      if Paste2::LANGUAGES.include? language
        @options[:language] = language
      else
        puts "Not supported language '#{language}'"
        exit(1)
      end
    end
    opts.on( '-h', '--help', 'Display this screen' ) do
      @options[:help] = true
    end
  end
  begin
    parser.parse ARGV
    if @options[:help]
      puts parser
    end
  rescue => e
    puts e.message
  end
end

Class Method Details

.post(code, description = '', lang = 'text') ⇒ Object



5
6
7
8
9
# File 'lib/paste2/client.rb', line 5

def self.post(code, description = '', lang = 'text')
  uri = URI(Paste2::URL)
  res = Net::HTTP.post_form(uri, :code => code, :description => description, :lang => lang)
  File.join(Paste2::URL,res['location'])
end

Instance Method Details

#runObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/paste2/client.rb', line 54

def run
  return if @options[:help]
  begin
    code ||= @options[:file].nil? ? STDIN.readlines : File.open(@options[:file]).readlines
    puts Paste2::Client.post(code.join(''), @options[:description], @options[:language])
  rescue => e
    puts e.message
    exit(1)
  end
end