Class: RackBox::Bin
Instance Method Summary collapse
- #info ⇒ 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
Returns a new instance of Bin.
13 14 15 16 |
# File 'lib/rackbox/bin.rb', line 13 def initialize *args @default_command = :request super end |
Instance Method Details
#info ⇒ Object
122 123 124 |
# File 'lib/rackbox/bin.rb', line 122 def info puts "RackBox.app => #{ RackBox.app.inspect }" end |
#info_help ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/rackbox/bin.rb', line 113 def info_help <<doco Usage: #{ script_name } info Summary: Display information about the current Rack application end doco end |
#request(*args) ⇒ Object
70 71 72 73 74 75 76 77 78 79 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 |
# File 'lib/rackbox/bin.rb', line 70 def request *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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rackbox/bin.rb', line 52 def request_help <<doco Usage: #{ script_name } request '/some/path' Options: -m, --method The HTTP method to use, default: get -d, --data Data to that you can PUT/POST, eg. -d '<XML>' -s, --show What to show, eg. -s body,headers,status or call multiple times, eg. -s body -s headers -h, --header Add header to request, eg. -h accept=text/plain -j, --json Sets 'Accept' header to 'application/json' -x, --xml Sets 'Accept' header to 'application/xml' Summary: Run a request against a Rack app end doco end |
#usage(*args) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/rackbox/bin.rb', line 18 def usage *args puts <<doco rackbox == %{ For hitting your Rack applications } Usage: rackbox command [options] Examples: rackbox info # prints app info Further help: rackbox commands # list all available commands rackbox help <COMMAND> # show help for COMMAND rackbox help # show this help message doco end |