lib/bulkrax.rb in bulkrax-4.4.2 vs lib/bulkrax.rb in bulkrax-5.0.0

- old
+ new

@@ -2,12 +2,17 @@ require "bulkrax/version" require "bulkrax/engine" require 'active_support/all' +# rubocop:disable Metrics/ModuleLength module Bulkrax class << self + # @todo Move from module attribute methods to a configuration class. With module attributes, + # when we make a change we are polluting the global space. This means that our tests that + # modify these config values are modifying global state. Which is not desirous, as it can + # introduce unexpected flakey tests. mattr_accessor :api_definition, :default_field_mapping, :default_work_type, :export_path, :field_mappings, @@ -166,6 +171,48 @@ # this function maps the vars from your app into your engine def self.setup yield self end + + # Responsible for stripping hidden characters from the given string. + # + # @param value [#to_s] + # @return [String] with hidden characters removed + # + # @see https://github.com/samvera-labs/bulkrax/issues/688 + def self.normalize_string(value) + # Removing [Byte Order Mark (BOM)](https://en.wikipedia.org/wiki/Byte_order_mark) + value.to_s.delete("\xEF\xBB\xBF") + end + + # This class confirms to the Active::Support.serialze interface. It's job is to ensure that we + # don't have keys with the tricksy Byte Order Mark character. + # + # @see https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Serialization/ClassMethods.html#method-i-serialize + class NormalizedJson + def self.normalize_keys(hash) + return hash unless hash.respond_to?(:each_pair) + returning_value = {} + hash.each_pair do |key, value| + returning_value[Bulkrax.normalize_string(key)] = value + end + returning_value + end + + # When we write the serialized data to the database, we "dump" the value into that database + # column. + def self.dump(value) + JSON.dump(normalize_keys(value)) + end + + # When we load the serialized data from the database, we pass the database's value into "load" + # function. + # + # rubocop:disable Security/JSONLoad + def self.load(string) + normalize_keys(JSON.load(string)) + end + # rubocop:enable Security/JSONLoad + end end +# rubocop:disable Metrics/ModuleLength