Class: Haste::CLI

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

Instance Method Summary collapse

Constructor Details

#initializeCLI

Pull all of the data from STDIN



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/haste.rb', line 12

def initialize
  if STDIN.tty?
    abort 'No input file given' unless ARGV.length == 1
    abort "#{file}: No such path" unless File.exists?(file = ARGV[0])
    @input = open(file).read
  else
    @input = STDIN.readlines.join
  end
  # clean up
  @input.strip!
end

Instance Method Details

#startObject

Upload the and output the URL we get back



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/haste.rb', line 25

def start
  uri = URI.parse server
  http = Net::HTTP.new uri.host, uri.port
  response = http.post '/documents', @input
  if response.is_a?(Net::HTTPOK)
    data = JSON.parse(response.body)
    method = STDOUT.tty? ? :puts : :print
    STDOUT.send method, "#{server}/#{data['key']}"
  else
    abort "failure uploading: #{response.code}"
  end
rescue JSON::ParserError => e
  abort "failure uploading: #{response.code}"
rescue Errno::ECONNREFUSED => e
  abort "failure connecting: #{e.message}"
end