Class: RackBox::Bin

Inherits:
Object
  • Object
show all
Includes:
SimpleCLI
Defined in:
lib/rackbox/bin.rb

Instance Method Summary collapse

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

#infoObject



122
123
124
# File 'lib/rackbox/bin.rb', line 122

def info
  puts "RackBox.app => #{ RackBox.app.inspect }"
end

#info_helpObject



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
  options = {
    :show => [],
    :headers => {}
  }
  opts = OptionParser.new do |opts|
    opts.on('-j', '--json'){ options[:headers]['Accept'] = 'application/json' }
    opts.on('-x', '--xml'){ options[:headers]['Accept'] = 'application/xml' }
    opts.on('-m','--method [m]'){|m| options[:method] = m }
    opts.on('-d','--data [d]'){|d| options[:data] = d }
    opts.on('-s','--show [s]'){|s| options[:show] += s.split(',') }
    opts.on('-h','--header [h]'){|h|
      name,value = h.split('=')
      options[:headers][name] = value
    }
  end 
  opts.parse! args

  rackbox_options = { }
  rackbox_options[:method] = options[:method] if options[:method]
  rackbox_options[:data]   = options[:data] if options[:data]

  options[:headers].each do |name, value|
    rackbox_options[name] = value
  end

  url = args.pop
  response = RackBox.request url, rackbox_options
  options[:show] = %w( body headers status ) if options[:show].empty?

  if options[:show].include? 'body'
    body_text = ''
    response.body.each {|str| body_text << str }
  end

  output = "Response:\n"
  output << "  Status: #{ response.status }\n" if options[:show].include? 'status'
  output << "  Headers: \n#{ response.headers.to_yaml.strip.indent(4) }\n" if options[:show].include? 'headers'
  output << "  Body: \n#{ body_text.indent(4) }\n" if options[:show].include? 'body'

  puts output
end

#request_helpObject



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