Class: RackBox::Bin
Instance Method Summary collapse
- #info(*args) ⇒ Object
- #info_help ⇒ Object
-
#initialize(*args) ⇒ Bin
constructor
A new instance of Bin.
- #request(*args) ⇒ Object
- #request_help ⇒ Object
- #usage(*args) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Bin
13 14 15 16 |
# File 'lib/rackbox/bin.rb', line 13 def initialize *args @default_command = :request super end |
Instance Method Details
#info(*args) ⇒ Object
144 145 146 147 |
# File 'lib/rackbox/bin.rb', line 144 def info *args set_rackbox_app args puts "RackBox.app => #{ RackBox.app( :silent => true ).inspect }" end |
#info_help ⇒ Object
135 136 137 138 139 140 141 142 143 |
# File 'lib/rackbox/bin.rb', line 135 def info_help "Usage: \#{ script_name } info\n\nSummary:\n Display information about the current Rack application\nend\n" end |
#request(*args) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/rackbox/bin.rb', line 80 def request *args # if args is ampty, we likely just called: $ rackbox # which came to #request because it's the default command, # so we should print out the usage for rackbox and exit if args.empty? usage exit end set_rackbox_app args = { :show => [], :headers => {} } opts = OptionParser.new do |opts| opts.on('-j', '--json'){ [:headers]['Accept'] = 'application/json' } opts.on('-x', '--xml'){ [:headers]['Accept'] = 'application/xml' } opts.on('-m','--method [m]'){|m| [:method] = m } opts.on('-d','--data [d]'){|d| [:data] = d } opts.on('-s','--show [s]'){|s| [:show] += s.split(',') } opts.on('-h','--header [h]'){|h| name,value = h.split('=') [:headers][name] = value } end opts.parse! args = { } [:method] = [:method] if [:method] [:data] = [:data] if [:data] [:headers].each do |name, value| [name] = value end url = args.pop response = RackBox.request url, [:show] = %w( body headers status ) if [:show].empty? if [:show].include? 'body' body_text = '' response.body.each {|str| body_text << str } end output = "Response:\n" output << " Status: #{ response.status }\n" if [:show].include? 'status' output << " Headers: \n#{ response.headers.to_yaml.strip.indent(4) }\n" if [:show].include? 'headers' output << " Body: \n#{ body_text.indent(4) }\n" if [:show].include? 'body' puts output end |
#request_help ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rackbox/bin.rb', line 62 def request_help "Usage: \#{ script_name } request '/some/path'\n\nOptions:\n -m, --method The HTTP method to use, default: get\n -d, --data Data to that you can PUT/POST, eg. -d '<XML>'\n -s, --show What to show, eg. -s body,headers,status or\n call multiple times, eg. -s body -s headers\n -h, --header Add header to request, eg. -h accept=text/plain\n -j, --json Sets 'Accept' header to 'application/json'\n -x, --xml Sets 'Accept' header to 'application/xml'\n\nSummary:\n Run a request against a Rack app\nend\n" end |
#usage(*args) ⇒ Object
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 |
# File 'lib/rackbox/bin.rb', line 18 def usage *args puts "\nrackbox == %{ For hitting your Rack applications }\n\n Usage:\n rackbox command [options]\n\n Examples:\n rackbox info # prints app info\n rackbox commands # list commands\n rackbox get / # request / path\n rackbox post -d '<XML>' / # POST data to / path\n rackbox / # GET / \n rackbox --data '<XML>' # POST data to / path\n rackbox /dogs --xml # get /dogs, accepting xml\n\n Options:\n -a, --app \"MyApp.new\" # ruby to eval to get Rack app\n -r, --require file[.rb] # ruby file(s) to require\n\n Further help:\n rackbox commands # list all available commands\n rackbox help <COMMAND> # show help for COMMAND\n rackbox help # show this help message\n\n" end |