From e7314e094a7c8717badec4f1f951b4cbb98ff5e1 Mon Sep 17 00:00:00 2001 From: Dan Franko Date: Fri, 21 Nov 2014 11:55:26 -0500 Subject: [PATCH] More flexible way of querying against ServiceNow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The “old” way basically forced you to use a specific operator. This will allow for the user to specify what operator should be used. --- lib/classes/configuration.rb | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/classes/configuration.rb b/lib/classes/configuration.rb index 9f97e68..398d6a8 100644 --- a/lib/classes/configuration.rb +++ b/lib/classes/configuration.rb @@ -1,6 +1,5 @@ module ServiceNow class Configuration - def self.configure(auth_hash = {}) $root_url = auth_hash[:sn_url].sub(/(\/)+$/, '') #remove trailing slash if there are any $username = auth_hash[:sn_username] @@ -24,20 +23,19 @@ def self.update_resource(incident_number, table) private def self.hash_to_query(query_hash = {}) if query_hash.empty? - return "" + # You're gonna have a bad time if you don't pass a query + raise "SN::ERROR: You must provide a query!" end + query_string = [] - query_hash.each do |k, v| - key_str = k.to_s - value_str = v.to_s - # if we are querying based on short_description or description - # we use a partial match - if key_str == "short_description" || key_str == "description" - query_string << key_str + "LIKE" + value_str - else - query_string << key_str + "=" + value_str - end + + # Make sure we're always dealing with an array + query_hash = [query_hash] if ! query_hash.is_a?(Array) + + query_hash.each do |f| + query_string.push f[:field] + f[:operator] + f[:query] end + query_string.join('^') end end