diff options
author | ytti <saku@ytti.fi> | 2018-04-25 16:12:44 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-25 16:12:44 +0300 |
commit | 583bc422448a234a215113a15f8a6976c50b2296 (patch) | |
tree | b3f757e6f37aa058a0c80e4ded3ef82553bc98c1 /lib/oxidized/source/http.rb | |
parent | 9a7d16c00163c421d2c9f3cb2783c76633860336 (diff) | |
parent | dffae0f76656a57da8f5473ce21955638cbe1f7f (diff) |
Merge pull request #1296 from wk/the-great-makeover
the great makeover - standardize layout, alignment, indentation
Diffstat (limited to 'lib/oxidized/source/http.rb')
-rw-r--r-- | lib/oxidized/source/http.rb | 97 |
1 files changed, 48 insertions, 49 deletions
diff --git a/lib/oxidized/source/http.rb b/lib/oxidized/source/http.rb index 56a575b..4480cbb 100644 --- a/lib/oxidized/source/http.rb +++ b/lib/oxidized/source/http.rb @@ -1,64 +1,63 @@ module Oxidized -class HTTP < Source - def initialize - @cfg = Oxidized.config.source.http - super - end - - def setup - if @cfg.url.empty? - raise NoConfig, 'no source http url config, edit ~/.config/oxidized/config' + class HTTP < Source + def initialize + @cfg = Oxidized.config.source.http + super 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' - http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless @cfg.secure - - # map headers - headers = {} - @cfg.headers.each do |header, value| - headers[header] = value + def setup + if @cfg.url.empty? + raise NoConfig, 'no source http url config, edit ~/.config/oxidized/config' + end end - request = Net::HTTP::Get.new(uri.request_uri, headers) - if (@cfg.user? && @cfg.pass?) - request.basic_auth(@cfg.user,@cfg.pass) - end + require "net/http" + require "uri" + require "json" - response = http.request(request) - data = JSON.parse(response.body) - data.each do |node| - next if node.empty? - # map node parameters - keys = {} - @cfg.map.each do |key, want_position| - want_positions = want_position.split('.') - keys[key.to_sym] = node_var_interpolate node.dig(*want_positions) + def load + nodes = [] + uri = URI.parse(@cfg.url) + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true if uri.scheme == 'https' + http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless @cfg.secure + + # map headers + headers = {} + @cfg.headers.each do |header, value| + headers[header] = value end - keys[:model] = map_model keys[:model] if keys.has_key? :model - # map node specific vars - vars = {} - @cfg.vars_map.each do |key, want_position| - want_positions = want_position.split('.') - vars[key.to_sym] = node_var_interpolate node.dig(*want_positions) + request = Net::HTTP::Get.new(uri.request_uri, headers) + if (@cfg.user? && @cfg.pass?) + request.basic_auth(@cfg.user, @cfg.pass) end - keys[:vars] = vars unless vars.empty? - nodes << keys + response = http.request(request) + data = JSON.parse(response.body) + data.each do |node| + next if node.empty? + # map node parameters + keys = {} + @cfg.map.each do |key, want_position| + want_positions = want_position.split('.') + keys[key.to_sym] = node_var_interpolate node.dig(*want_positions) + end + keys[:model] = map_model keys[:model] if keys.has_key? :model + + # map node specific vars + vars = {} + @cfg.vars_map.each do |key, want_position| + want_positions = want_position.split('.') + vars[key.to_sym] = node_var_interpolate node.dig(*want_positions) + end + keys[:vars] = vars unless vars.empty? + + nodes << keys + end + nodes end - nodes end - -end end if RUBY_VERSION < '2.3' |