diff options
Diffstat (limited to 'lib/oxidized/source')
-rw-r--r-- | lib/oxidized/source/csv.rb | 2 | ||||
-rw-r--r-- | lib/oxidized/source/http.rb | 54 |
2 files changed, 55 insertions, 1 deletions
diff --git a/lib/oxidized/source/csv.rb b/lib/oxidized/source/csv.rb index 3b66e65..5064e5e 100644 --- a/lib/oxidized/source/csv.rb +++ b/lib/oxidized/source/csv.rb @@ -18,7 +18,7 @@ class CSV < Source def load nodes = [] - open(@cfg.file).each_line do |line| + open(File.expand_path @cfg.file).each_line do |line| next if line.match /^\s*#/ data = line.chomp.split @cfg.delimiter next if data.empty? diff --git a/lib/oxidized/source/http.rb b/lib/oxidized/source/http.rb new file mode 100644 index 0000000..d2e3ea6 --- /dev/null +++ b/lib/oxidized/source/http.rb @@ -0,0 +1,54 @@ +module Oxidized +class HTTP < Source + def initialize + @cfg = CFG.source.http + super + end + + def setup + if @cfg.url.empty? + raise NoConfig, 'no source http url config, edit ~/.config/oxidized/config' + end + end + + require "net/http" + require "uri" + require "json" + + def load + nodes = [] + uri = URI.parse(@cfg.url) + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true if uri.scheme == 'https' + + # map headers + headers = {} + @cfg.headers.each do |header, value| + headers[header] = value + end + + request = Net::HTTP::Get.new(uri.request_uri, headers) + + response = http.request(request) + data = JSON.parse(response.body) + data.each do |line| + next if line.empty? + # map node parameters + keys = {} + @cfg.map.each do |key, position| + keys[key.to_sym] = line[position] + end + keys[:model] = map_model keys[:model] if keys.key? :model + + # map node specific vars, empty value is considered as nil + vars = {} + @cfg.vars_map.each { |key, position| vars[key.to_sym] = line[position].to_s.empty? ? nil : line[position] } + keys[:vars] = vars unless vars.empty? + + nodes << keys + end + nodes + end + +end +end |