<commit_before>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]
            $password = auth_hash[:sn_password]
            "SN::Success: Configuration successful"
        end

        def self.get_resource(query_hash = {}, displayvalue = false, table)
            # to be filled in
            RestClient::Resource.new(URI.escape($root_url + "/#{table}.do?JSON&sysparm_action=getRecords&sysparm_query=#{hash_to_query(query_hash)}&displayvalue=#{displayvalue}"), $username, $password)
        end

        def self.post_resource(table)
            RestClient::Resource.new(URI.escape($root_url + "/#{table}.do?JSON&sysparm_action=insert"), $username, $password)
        end

        def self.update_resource(incident_number, table)
           RestClient::Resource.new(URI.escape($root_url + "/#{table}.do?JSON&sysparm_query=number=#{incident_number}&sysparm_action=update"), $username, $password) 
        end

        private
            def self.hash_to_query(query_hash = {})
                if query_hash.empty?
                    return ""
                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
                end
                query_string.join('^')
            end
    end
end<commit_msg>More flexible way of querying against ServiceNow<commit_after>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]
            $password = auth_hash[:sn_password]
            "SN::Success: Configuration successful"
        end

        def self.get_resource(query_hash = {}, displayvalue = false, table)
            # to be filled in
            RestClient::Resource.new(URI.escape($root_url + "/#{table}.do?JSON&sysparm_action=getRecords&sysparm_query=#{hash_to_query(query_hash)}&displayvalue=#{displayvalue}"), $username, $password)
        end

        def self.post_resource(table)
            RestClient::Resource.new(URI.escape($root_url + "/#{table}.do?JSON&sysparm_action=insert"), $username, $password)
        end

        def self.update_resource(incident_number, table)
           RestClient::Resource.new(URI.escape($root_url + "/#{table}.do?JSON&sysparm_query=number=#{incident_number}&sysparm_action=update"), $username, $password) 
        end

        private
            def self.hash_to_query(query_hash = {})
                if query_hash.empty?
                  # 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 = []
                
                # 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
end
{'fraction_non_alphanumeric': 0.10840471092077088, 'fraction_numerical': 0.0, 'mean_word_length': 1.91953125, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5368020', 'n_tokens_mistral': 1036, 'n_tokens_neox': 1002, 'n_words': 209}