Class: APIBuilder

Inherits:
Object
  • Object
show all
Defined in:
bin/minecraftctlserver

Defined Under Namespace

Classes: DSL

Instance Method Summary collapse

Constructor Details

#initialize(sinatra, description, path = '', &block) ⇒ APIBuilder

Returns a new instance of APIBuilder.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'bin/minecraftctlserver', line 44

def initialize(sinatra, description, path = '', &block)
	@sinatra = sinatra
	@description = description

	@dsl = DSL.new(sinatra, path, &block).data
	@path = path
	root = self

	@sinatra.get(root_path) do
		root.help.join("\n") + "\n"
	end

	@dsl.gets.each do |op|
		@sinatra.get(path + '/' + op.name, &op.block)
	end

	post_map = {}

	@dsl.posts.each do |op|
		post_map[op.name] = op
	end

	@sinatra.post(root_path) do
		cmd, *args = request.body.read.split(' ')
		op = post_map[cmd] or return [400, "Unknown argument: #{cmd} for request: #{request.path_info}\n"]
		op.block.call(args)
	end
end

Instance Method Details

#helpObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'bin/minecraftctlserver', line 77

def help
	out = []
	out << "#{root_path} - #{@description}"

	@dsl.posts.each do |op|
		out << "#{root_path} #{op.name} - #{op.description}"
	end

	@dsl.gets.each do |op|
		out << "#{@path}/#{op.name} - #{op.description}"
	end

	@dsl.paths.each do |path|
		out += path.help
	end

	out
end

#root_pathObject



73
74
75
# File 'bin/minecraftctlserver', line 73

def root_path
	@path.empty? ? '/' : @path
end