Created
July 9, 2017 18:31
-
-
Save majormoses/2e57e74ea68c2e6c8ba10b555102c802 to your computer and use it in GitHub Desktop.
Converting xml to json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
require 'sensu-plugin/check/cli' | |
require 'active_support/all' | |
require 'json' | |
dir_to_check = Dir.glob('/tmp/testdir/*') | |
# Dir always returns an empty list even if the dir does not even exist | |
if dir_to_check.empty? | |
p 'this is an empty dir, does not exist, or have permissions to view' | |
else | |
files = dir_to_check.sort_by { |file_name| File.mtime(file_name)} | |
end | |
p files | |
result = Hash.from_xml(File.read(files[-1])).to_json | |
puts result | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
verification:
$ /tmp/xml_json.rb "this is an empty dir" nil