From 5748dc015db423e4469c4bfd43442ba6bea72160 Mon Sep 17 00:00:00 2001 From: nopedial Date: Sat, 26 Oct 2013 02:14:31 +0200 Subject: add primitive plain ascii API output support --- lib/oxidized/api/rest.rb | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'lib/oxidized/api') diff --git a/lib/oxidized/api/rest.rb b/lib/oxidized/api/rest.rb index d1bd7dc..9e7ebb8 100644 --- a/lib/oxidized/api/rest.rb +++ b/lib/oxidized/api/rest.rb @@ -5,10 +5,13 @@ module Oxidized class Rest module Helpers def send res, msg='OK', status=200 - msg = {:result => msg} - res['Content-Type'] = 'application/json' - res.status = status - res.body = JSON.dump msg + res.body = msg + if not res.body[-1] == '+' + msg = {:result => msg} + res['Content-Type'] = 'application/json' + res.status = status + res.body = JSON.dump msg + end end end include Oxidized::API::Rest::Helpers @@ -49,7 +52,12 @@ module Oxidized else group, node = 0, $1 end - send res, @nodes.fetch(node, group) + if node.include? '.txt' + node.gsub! '.txt', '' + send res, [ @nodes.fetch(node, group) + '+' ].join(':') # add local switch to trick 'send' out of json formatting + else + send res, @nodes.fetch(node, group) + end rescue Oxidized::NotSupported => e send res, e end -- cgit v1.2.1 From 4af4b982307f5abce1c01b3bd39aa1cd03d8b768 Mon Sep 17 00:00:00 2001 From: nopedial Date: Sat, 26 Oct 2013 10:57:53 +0200 Subject: clean ascii API output --- lib/oxidized/api/rest.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'lib/oxidized/api') diff --git a/lib/oxidized/api/rest.rb b/lib/oxidized/api/rest.rb index 9e7ebb8..f1a7ef5 100644 --- a/lib/oxidized/api/rest.rb +++ b/lib/oxidized/api/rest.rb @@ -4,9 +4,9 @@ module Oxidized module API class Rest module Helpers - def send res, msg='OK', status=200 + def send res, json, msg='OK', status=200 res.body = msg - if not res.body[-1] == '+' + if not json msg = {:result => msg} res['Content-Type'] = 'application/json' res.status = status @@ -52,12 +52,10 @@ module Oxidized else group, node = 0, $1 end - if node.include? '.txt' - node.gsub! '.txt', '' - send res, [ @nodes.fetch(node, group) + '+' ].join(':') # add local switch to trick 'send' out of json formatting - else - send res, @nodes.fetch(node, group) + json = if node[-4..-1] == '.txt' + node = node[0..-5] end + send res, json, @nodes.fetch(node, group) rescue Oxidized::NotSupported => e send res, e end -- cgit v1.2.1 From 52c5c00326d723c4649f862dde66fdaf331f7250 Mon Sep 17 00:00:00 2001 From: nopedial Date: Sat, 26 Oct 2013 11:23:48 +0200 Subject: cleanup ascii API output --- lib/oxidized/api/rest.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/oxidized/api') diff --git a/lib/oxidized/api/rest.rb b/lib/oxidized/api/rest.rb index f1a7ef5..1a426f5 100644 --- a/lib/oxidized/api/rest.rb +++ b/lib/oxidized/api/rest.rb @@ -4,9 +4,9 @@ module Oxidized module API class Rest module Helpers - def send res, json, msg='OK', status=200 + def send res, msg='OK', ascii=false, status=200 res.body = msg - if not json + if ascii msg = {:result => msg} res['Content-Type'] = 'application/json' res.status = status @@ -52,10 +52,10 @@ module Oxidized else group, node = 0, $1 end - json = if node[-4..-1] == '.txt' + ascii = if node[-4..-1] == '.txt' node = node[0..-5] end - send res, json, @nodes.fetch(node, group) + send res, @nodes.fetch(node, group), ascii rescue Oxidized::NotSupported => e send res, e end -- cgit v1.2.1 From 713bccb736d87283d98781e7c557cd48bae24aa3 Mon Sep 17 00:00:00 2001 From: nopedial Date: Sat, 26 Oct 2013 11:25:04 +0200 Subject: fix logic --- lib/oxidized/api/rest.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/oxidized/api') diff --git a/lib/oxidized/api/rest.rb b/lib/oxidized/api/rest.rb index 1a426f5..17fade1 100644 --- a/lib/oxidized/api/rest.rb +++ b/lib/oxidized/api/rest.rb @@ -6,7 +6,7 @@ module Oxidized module Helpers def send res, msg='OK', ascii=false, status=200 res.body = msg - if ascii + if not ascii msg = {:result => msg} res['Content-Type'] = 'application/json' res.status = status -- cgit v1.2.1 From 7982a7d3956910406e59952e1b215d5b4dd1d8e5 Mon Sep 17 00:00:00 2001 From: nopedial Date: Sat, 26 Oct 2013 11:30:26 +0200 Subject: set group to nil if no group is defined --- lib/oxidized/api/rest.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/oxidized/api') diff --git a/lib/oxidized/api/rest.rb b/lib/oxidized/api/rest.rb index 17fade1..cdf5c80 100644 --- a/lib/oxidized/api/rest.rb +++ b/lib/oxidized/api/rest.rb @@ -50,7 +50,7 @@ module Oxidized if $1.include? '/' group, node = $1.split("/")[1..2] else - group, node = 0, $1 + group, node = nil, $1 end ascii = if node[-4..-1] == '.txt' node = node[0..-5] -- cgit v1.2.1