Class: Treat::Core::Server
- Inherits:
-
Object
- Object
- Treat::Core::Server
- Defined in:
- lib/treat/core/server.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(handler = 'thin', options = {}) ⇒ Server
constructor
Refer to rack.rubyforge.org/doc/classes/Rack/Server.html for possible options to configure.
- #start ⇒ Object
Constructor Details
#initialize(handler = 'thin', options = {}) ⇒ Server
Refer to rack.rubyforge.org/doc/classes/Rack/Server.html for possible options to configure.
5 6 7 8 9 |
# File 'lib/treat/core/server.rb', line 5 def initialize(handler = 'thin', = {}) raise "Implementation not finished." require 'json'; require 'rack' @handler, @options = handler.capitalize, end |
Instance Method Details
#call(env) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/treat/core/server.rb', line 16 def call(env) headers = { 'content-type' => 'application/json' } rack_input = env["rack.input"].read if rack_input.strip == '' return [500, headers, { 'error' => 'Empty JSON request.' }] end rack_json = JSON.parse(rack_input) unless rack_json['type'] && rack_json['value'] && rack_json['do'] return [500, headers, { 'error' => 'Must specify "type", "value" and "do".' }] end if rack_json['conf'] # Set the configuration. end method = rack_json['type'].capitalize.intern resp = send(method, rack_json[value]).do(rack_json['do']) response = [rack_input.to_json] [200, headers, response] end |
#start ⇒ Object
11 12 13 14 |
# File 'lib/treat/core/server.rb', line 11 def start handler = Rack::Handler.const_get(@handler) handler.run(self, @options) end |