From 483a9901280ea436db34f61ef220cc2f4d90b0da Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Fri, 22 Jun 2018 18:49:46 +0300 Subject: remove source/http load smells --- lib/oxidized/source/http.rb | 46 +++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) (limited to 'lib/oxidized/source') diff --git a/lib/oxidized/source/http.rb b/lib/oxidized/source/http.rb index bf1e74c..36fa764 100644 --- a/lib/oxidized/source/http.rb +++ b/lib/oxidized/source/http.rb @@ -17,28 +17,7 @@ module Oxidized def load node_want = nil 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 - - req_uri = uri.request_uri - if node_want - req_uri = "#{req_uri}/#{node_want}" - end - request = Net::HTTP::Get.new(req_uri, headers) - if (@cfg.user? && @cfg.pass?) - request.basic_auth(@cfg.user, @cfg.pass) - end - - response = http.request(request) - data = JSON.parse(response.body) + data = JSON.parse(get_http) data = string_navigate(data, @cfg.hosts_location) if @cfg.hosts_location? data.each do |node| next if node.empty? @@ -72,5 +51,28 @@ module Oxidized end object end + + def get_http + 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 + + req_uri = uri.request_uri + if node_want + req_uri = "#{req_uri}/#{node_want}" + end + request = Net::HTTP::Get.new(req_uri, headers) + if (@cfg.user? && @cfg.pass?) + request.basic_auth(@cfg.user, @cfg.pass) + end + http.request(request).body + end end end -- cgit v1.2.1 From 2f3cf7647742ce80b428d5e0aad75b45e0510bdc Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Fri, 22 Jun 2018 19:08:50 +0300 Subject: remove csv smell --- lib/oxidized/source/csv.rb | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'lib/oxidized/source') diff --git a/lib/oxidized/source/csv.rb b/lib/oxidized/source/csv.rb index 7b771f6..769b1f7 100644 --- a/lib/oxidized/source/csv.rb +++ b/lib/oxidized/source/csv.rb @@ -20,14 +20,7 @@ module Oxidized def load _node_want = nil nodes = [] - file = File.expand_path(@cfg.file) - file = if @cfg.gpg? - crypto = GPGME::Crypto.new password: @cfg.gpg_password - file = crypto.decrypt(File.open(file)).to_s - else - open(file) - end - file.each_line do |line| + get_file.each_line do |line| next if line.match(/^\s*#/) data = line.chomp.split(@cfg.delimiter, -1) next if data.empty? @@ -49,5 +42,17 @@ module Oxidized end nodes end + + private + + def get_file + file = File.expand_path(@cfg.file) + if @cfg.gpg? + crypto = GPGME::Crypto.new password: @cfg.gpg_password + crypto.decrypt(File.open(file)).to_s + else + open(file) + end + end end end -- cgit v1.2.1 From 04ffb2c68ac7f069bd9f33f39c77095c979aaec1 Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Fri, 22 Jun 2018 19:31:12 +0300 Subject: rubycop fixes --- lib/oxidized/source/csv.rb | 4 ++-- lib/oxidized/source/http.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/oxidized/source') diff --git a/lib/oxidized/source/csv.rb b/lib/oxidized/source/csv.rb index 769b1f7..5b43352 100644 --- a/lib/oxidized/source/csv.rb +++ b/lib/oxidized/source/csv.rb @@ -20,7 +20,7 @@ module Oxidized def load _node_want = nil nodes = [] - get_file.each_line do |line| + open_file.each_line do |line| next if line.match(/^\s*#/) data = line.chomp.split(@cfg.delimiter, -1) next if data.empty? @@ -45,7 +45,7 @@ module Oxidized private - def get_file + def open_file file = File.expand_path(@cfg.file) if @cfg.gpg? crypto = GPGME::Crypto.new password: @cfg.gpg_password diff --git a/lib/oxidized/source/http.rb b/lib/oxidized/source/http.rb index 36fa764..26c9e05 100644 --- a/lib/oxidized/source/http.rb +++ b/lib/oxidized/source/http.rb @@ -17,7 +17,7 @@ module Oxidized def load node_want = nil nodes = [] - data = JSON.parse(get_http) + data = JSON.parse(read_http(node_want)) data = string_navigate(data, @cfg.hosts_location) if @cfg.hosts_location? data.each do |node| next if node.empty? @@ -52,7 +52,7 @@ module Oxidized object end - def get_http + def read_http node_want uri = URI.parse(@cfg.url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.scheme == 'https' -- cgit v1.2.1