Class: JOR::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/jor/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(redis = nil) ⇒ Server

Returns a new instance of Server.



5
6
7
8
9
# File 'lib/jor/server.rb', line 5

def initialize(redis = nil)
  ## defaults to test db on redis
  redis ||= Redis.new(:db => 9, :driver => :hiredis)
  @jor = JOR::Storage.new(redis)
end

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.message}.to_json]]
  end
end