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

#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_helpObject



135
136
137
138
139
140
141
142
143
# File 'lib/rackbox/bin.rb', line 135

def info_help
  <<doco
Usage: #{ script_name } info

Summary:
  Display information about the current Rack application
end
doco
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

  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



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
  <<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
36
37
38
39
40
41
42
43
44
45
# 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
    rackbox commands           # list commands
    rackbox get /              # request / path
    rackbox post -d '<XML>' /  # POST data to / path
    rackbox /                  # GET / 
    rackbox --data '<XML>'     # POST data to / path
    rackbox /dogs --xml        # get /dogs, accepting xml

  Options:
    -a, --app "MyApp.new"      # ruby to eval to get Rack app
    -r, --require file[.rb]    # ruby file(s) to require

  Further help:
    rackbox commands         # list all available commands
    rackbox help <COMMAND>   # show help for COMMAND
    rackbox help             # show this help message

doco
end