Class: App

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

Instance Method Summary collapse

Instance Method Details

#mainObject



9
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/rest_client.rb', line 9

def main
  valid_methods = ['GET', 'PUT', 'POST', 'DELETE']
  @opts = Trollop::options do
    version "UDP RestClient (c) 2016 @reednj"
    banner "Usage: udp-rest [options] <url>"
    opt :method, "HTTP Method (GET, POST etc)", :type => :string, :default => 'GET'
    opt :headers, "Show the response headers", :default => false
  end

  Trollop::educate if ARGV.empty?
  url = ARGV.last
  url = "uhttp://" + url unless url.start_with? 'uhttp://'

  begin
    if !valid_methods.include? @opts[:method].upcase
      raise "Invalid REST method '#{@opts[:method]}'"
    end

    r = UDPRest::Client.uhttp(@opts[:method], url)
    print_response(r)
  rescue => e
    puts e
  end
end


34
35
36
37
38
39
40
41
# File 'lib/rest_client.rb', line 34

def print_response(r)
  if @opts[:headers]
    puts r.ok? ? r.status_line.green : r.status_line.red
    puts ''
  end

  puts r.text
end