Completed
Push — master ( 8c5aaa...f46502 )
by Michael
01:05
created

RackThor.initialize()   A

Complexity

Conditions 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
dl 0
loc 21
ccs 13
cts 13
cp 1
crap 1
rs 9.3142
1 1
require 'hanami/router'
2 1
require 'stringio'
3
4 1
class RackThor
5 1
  attr_reader :router
6
7 1
  def initialize(cli)
8 2
    @cli = cli
9 2
    @router = Hanami::Router.new
10 2
    cli.all_commands.each do |name, c|
11 6
      @router.post "/#{name}", to: ->(env) do
12 2
        req = Rack::Request.new(env)
13 2
        begin
14 2
          $stdout = StringIO.new
15 2
          command = @cli.all_commands[name]
16
          @cli.new([], req.params['options'] || {}, {
17
            current_command: command,
18
            command_options: command.options
19 2
          }).invoke(name, req.params['args'] || [])
20 2
          $stdout.close
21 2
          [200, {}, [$stdout.string]]
22
        ensure
23 2
          $stdout = STDOUT
24
        end
25
      end
26
    end
27
  end
28
end
29