summaryrefslogtreecommitdiff
path: root/lib/oxidized/api/rest.rb
diff options
context:
space:
mode:
authorSaku Ytti <saku@ytti.fi>2013-04-19 12:36:45 +0300
committerSaku Ytti <saku@ytti.fi>2013-04-19 12:41:29 +0300
commit96668cc2dc0367e3d1a3ec94265ce889b62d8ada (patch)
tree340f92a6dd4b68e41a946729c8fae34db7aff40b /lib/oxidized/api/rest.rb
parent9d217025fac3e335c308f02e7377e14ccfdc0e66 (diff)
Add restful API
Diffstat (limited to 'lib/oxidized/api/rest.rb')
-rw-r--r--lib/oxidized/api/rest.rb43
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/oxidized/api/rest.rb b/lib/oxidized/api/rest.rb
new file mode 100644
index 0000000..ee06bc8
--- /dev/null
+++ b/lib/oxidized/api/rest.rb
@@ -0,0 +1,43 @@
+module Oxidized
+ require 'webrick'
+ require 'json'
+ module API
+ class Rest
+ def initialize nodes, listen
+ @nodes = nodes
+ addr, port = listen.to_s.split ':'
+ port, addr = addr, nil if not port
+ @web = WEBrick::HTTPServer.new :BindAddress=>addr, :Port=>port
+ maps
+ end
+ def work
+ req = select @web.listeners, nil, nil, Config::Sleep
+ while req
+ @web.run req.first.first.accept
+ req = select @web.listeners, nil, nil, 0
+ end
+ end
+ def maps
+ @web.mount_proc '/nodes' do |req, res|
+ #script_name, #path_info
+ case req.path_info[1..-1]
+ # /nodes/reload - reloads list of nodes
+ when 'reload'
+ @nodes.load
+ res.body = JSON.dump 'OK'
+ # /nodes/next/node - moves node to head of queue
+ when /next\/(.*)/
+ @nodes.next $1
+ res.body = JSON.dump 'OK'
+ # /nodes/list - returns list of nodes
+ when 'list'
+ res.body = JSON.dump @nodes.list
+ # /nodes/show/node - returns data about node
+ when /show\/(.*)/
+ res.body = JSON.dump @nodes.show $1
+ end
+ end
+ end
+ end
+ end
+end