Class: JOR::Server
- Inherits:
-
Object
- Object
- JOR::Server
- Defined in:
- lib/jor/server.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(redis = nil) ⇒ Server
constructor
A new instance of Server.
Constructor Details
Instance Method Details
#call(env) ⇒ Object
11 12 13 14 15 16 17 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 46 47 |
# File 'lib/jor/server.rb', line 11 def call(env) req = Rack::Request.new(env) method = req.path if env["REQUEST_METHOD"]!="PUT" return [422, {"Content-Type" => "application/json"}, [{"error" => "only method accepted is PUT"}.to_json]] end clean_methods = method.gsub("/","").split(".") clean_methods.map!(&:to_sym) args = req.params["args"] clean_args = [] body_str = req.body.read clean_args = JSON::parse(body_str) unless body_str.nil? || body_str.empty? begin obj = @jor res = nil clean_methods.each_with_index do |meth, i| if i==clean_methods.size()-1 res = obj.public_send(meth,*clean_args) else obj = obj.public_send(meth) end end if res.class==Hash || res.class==Array return [200, {"Content-Type" => "application/json"}, [res.to_json]] else return [200, {"Content-Type" => "application/json"}, [{"value" => res}.to_json]] end rescue Exception => e return [422, {"Content-Type" => "application/json"}, [{"error" => e.}.to_json]] end end |