diff --git a/lib/ontologies_api_client.rb b/lib/ontologies_api_client.rb index 8cce87b..739639a 100644 --- a/lib/ontologies_api_client.rb +++ b/lib/ontologies_api_client.rb @@ -2,7 +2,6 @@ require 'multi_json' require 'spawnling' -require_relative 'ontologies_api_client/utils' require_relative 'ontologies_api_client/config' require_relative 'ontologies_api_client/http' require_relative 'ontologies_api_client/link_explorer' diff --git a/lib/ontologies_api_client/base.rb b/lib/ontologies_api_client/base.rb index 8e2cf8d..aedd782 100644 --- a/lib/ontologies_api_client/base.rb +++ b/lib/ontologies_api_client/base.rb @@ -50,17 +50,12 @@ def self.map_classes end def initialize(options = {}) - begin - values = options[:values] - if values.is_a?(Hash) && !values.empty? - create_attributes(values.keys) - populate_attributes(values) - end - create_attributes(self.class.attrs_always_present || []) - rescue => e - LOGGER.debug("\n\n ECCEZIONE! RUBY API - base.rb - initialize(options): #{e.message}\n#{e.backtrace.join("\n")}") - raise e + values = options[:values] + if values.is_a?(Hash) && !values.empty? + create_attributes(values.keys) + populate_attributes(values) end + create_attributes(self.class.attrs_always_present || []) end def id @@ -157,4 +152,4 @@ def populate_attributes(hash) end end -end +end \ No newline at end of file diff --git a/lib/ontologies_api_client/models/creator.rb b/lib/ontologies_api_client/models/creator.rb deleted file mode 100644 index 4a9569a..0000000 --- a/lib/ontologies_api_client/models/creator.rb +++ /dev/null @@ -1,12 +0,0 @@ -require_relative "../base" - -module LinkedData - module Client - module Models - class Creator < LinkedData::Client::Base - include LinkedData::Client::ReadWrite - @media_type = "http://data.ecoportal.org/metadata/Creator" - end - end - end -end diff --git a/lib/ontologies_api_client/models/creator_identifier.rb b/lib/ontologies_api_client/models/creator_identifier.rb deleted file mode 100644 index c7da7c8..0000000 --- a/lib/ontologies_api_client/models/creator_identifier.rb +++ /dev/null @@ -1,12 +0,0 @@ -require_relative "../base" - -module LinkedData - module Client - module Models - class CreatorIdentifier < LinkedData::Client::Base - include LinkedData::Client::ReadWrite - @media_type = "http://data.ecoportal.org/metadata/CreatorIdentifier" - end - end - end -end diff --git a/lib/ontologies_api_client/read_write.rb b/lib/ontologies_api_client/read_write.rb index 5cc6d98..366f8ba 100644 --- a/lib/ontologies_api_client/read_write.rb +++ b/lib/ontologies_api_client/read_write.rb @@ -1,5 +1,4 @@ require 'active_support/core_ext/hash' -require_relative 'utils' module LinkedData module Client @@ -63,75 +62,29 @@ def cache_refresh(options = {}) end def equivalent?(current_value, new_value) - # LOGGER.debug("\n\n----------------------\nONTOLOGIES_API_RUBY_CLIENT: LinkedData::Client::ReadWrite -> equivalent:\n\n > current_value=#{current_value.inspect}\n\n > new_value=#{new_value.inspect}") - begin - # If we're comparing an existing embedded object - # then use the id for comparison - if current_value.is_a?(LinkedData::Client::Base) - #LOGGER.debug(" > current_value is a LinkedData::Client::Base \n > current_value= #{current_value}\n > new_value=#{new_value}") - result_equivalent = current_value.id.eql?(new_value) - #LOGGER.debug(" > > equivalent RESULT =#{result_equivalent}") - return result_equivalent - end - - # Otherwise, do some complex comparing - case new_value - when String - #LOGGER.debug(" > new_value is a String\n > current_value= #{current_value}\n > new_value=#{new_value}") - result_equivalent = current_value.to_s.eql?(new_value) - #LOGGER.debug(" > > equivalent RESULT =#{result_equivalent}") - return result_equivalent - when Array, Hash - new_value = nil if new_value.is_a?(Array) && new_value.empty? && current_value.nil? - if new_value.is_a?(Hash) || (new_value.is_a?(Array) && (new_value.first.is_a?(Hash) || new_value.first.is_a?(OpenStruct))) - #LOGGER.debug(" > new_value is an Array && new_value.first is an Hash or OpenStruct") - - ordered_current = LinkedData::Client::Utils.order_inner_array_elements(current_value, true, [:id, :type, :links, :context, :created]) - # LOGGER.debug(" > > ordered_current=#{ordered_current}") + # If we're comparing an existing embedded object + # then use the id for comparison + if current_value.is_a?(LinkedData::Client::Base) + return current_value.id.eql?(new_value) + end - clean_current = LinkedData::Client::Utils.recursive_symbolize_keys(ordered_current, true, [:id, :type, :links, :context, :created]) - # LOGGER.debug(" > > clean_current=#{clean_current}") - - # LOGGER.debug(" > > BEFORE elaboration PROVE: new_value=#{new_value}") - # LOGGER.debug(" > > BEFORE elaboration PROVE: new_value.inspect= #{new_value.inspect}") - # LOGGER.debug(" > > BEFORE elaboration PROVE: new_value_symbolized: #{LinkedData::Client::Utils.recursive_symbolize_keys(new_value)}") - #new_value.map{|e| LOGGER.debug("new_value.map: \n e = #{e}\n e.to_h = #{e.to_h}")} - - #new_value[0].each{ |k,v| LOGGER.debug("new_value[0].each [k,v] = [#{k},#{v}] => #{new_value[0]}")} - - clean_new_to_order = LinkedData::Client::Utils.recursive_symbolize_keys(new_value, true, [:id, :type, :links, :context, :created]) - # LOGGER.debug(" > > clean_new_to_order=#{clean_new_to_order}") - - clean_new = LinkedData::Client::Utils.order_inner_array_elements(clean_new_to_order, true, [:id, :type, :links, :context, :created]) - #LOGGER.debug(" > > clean_new=#{clean_new}") - - result_equivalent = clean_current.eql?(clean_new) rescue clean_current == clean_new - # LOGGER.debug(" > > equivalent RESULT =#{result_equivalent}") - return result_equivalent - else - #LOGGER.debug(" > new_value is an Array OR Hash:\n > current_value= #{current_value}\n > new_value=#{new_value}") - result_equivalent = current_value.sort.eql?(new_value.sort) rescue current_value == new_value - #LOGGER.debug(" > > equivalent RESULT =#{result_equivalent}") - return result_equivalent - end + # Otherwise, do some complex comparing + case new_value + when String + return current_value.to_s.eql?(new_value) + when Array, Hash + new_value = nil if new_value.is_a?(Array) && new_value.empty? && current_value.nil? + if new_value.is_a?(Array) && new_value.first.is_a?(Hash) + clean_current = current_value.map {|e| e.to_h.symbolize_keys.delete_if {|k,v| v.nil?}}.sort_by {|e| e.is_a?(Hash) ? e.values.sort : e} + clean_new = new_value.map {|e| e.symbolize_keys.delete_if {|k,v| v.nil?}}.sort_by {|e| e.is_a?(Hash) ? e.values.sort : e} + return clean_current.eql?(clean_new) rescue clean_current == clean_new + else + return current_value.sort.eql?(new_value.sort) rescue current_value == new_value end - #LOGGER.debug(" > OTHERWISE CASE > current_value= #{current_value}\n > new_value=#{new_value}") - result_equivalent = current_value.sort.eql?(new_value.sort) rescue current_value == new_value - #LOGGER.debug(" > > equivalent RESULT =#{result_equivalent}") - return result_equivalent - rescue => e - LOGGER.debug("\n\n ECCEZIONE! ONTOLOGIES_API_RUBY_CLIENT: LinkedData::Client::equivalent: #{e.message}\n#{e.backtrace.join("\n")}") - raise e end + return current_value.eql?(new_value) rescue current_value == new_value end - # NEW method Ecoportal - # clean an annidate hash (hash with array, hash or array of hash) - def clean_annidate_hash(obj) - - end - - def invalidate_cache(cache_refresh_all = true) self.class.all(invalidate_cache: true) if cache_refresh_all HTTP.get(self.id, invalidate_cache: true) if self.id @@ -151,4 +104,4 @@ def refresh_cache end end -end +end \ No newline at end of file diff --git a/lib/ontologies_api_client/utils.rb b/lib/ontologies_api_client/utils.rb deleted file mode 100644 index 9bc2b53..0000000 --- a/lib/ontologies_api_client/utils.rb +++ /dev/null @@ -1,101 +0,0 @@ -# --- NEW ECOPORTAL class----- -# Utility class -module LinkedData - module Client - class Utils - - #Symbolize the keys recursively for the object passed as parameter - # Es. {"a" => "aaa", "b" => {"b1" => "bbb1", "b2" => "bbb2"}} - # becomes {:a => "aaa", :b => {:b1 => "bbb1", :b2 => "bbb2"}} - def self.recursive_symbolize_keys(obj, exclude_nil_value = false, excluded_keys = []) - # LOGGER.debug("ONTOLOGIES_API_RUBY_CLIENT: LinkedData::Client::Utils -> recursive_symbolize_keys CALL:") - if obj.is_a?Array - ## LOGGER.debug(" OBJ is an Array") - obj.map do |e| - val = (e.to_h == {}) ? e : e.to_h - recursive_symbolize_keys(val, exclude_nil_value, excluded_keys) - end - else - {}.tap do |h| - obj.each do |key, value| - # LOGGER.debug("ONTOLOGIES_API_RUBY_CLIENT: LinkedData::Client::Utils -> recursive_symbolize_keys [key,value] = [#{key}, #{value}] - key.to_sym= #{key.to_sym}") - # next if excluded_keys.include?(key) - # next if (exclude_nil_value && value.nil?) - h[key.to_sym] = map_value(value, exclude_nil_value, excluded_keys) - end - end - end - end - - def self.map_value(thing, exclude_nil_value = false, excluded_keys = []) - # LOGGER.debug("ONTOLOGIES_API_RUBY_CLIENT: LinkedData::Client::Utils -> map_value: \n > thing = #{thing}") - case thing - when Hash - # LOGGER.debug(" > thing is HASH") - recursive_symbolize_keys(thing, exclude_nil_value, excluded_keys) - when Array - # LOGGER.debug(" > thing is Array") - thing.map { |v| map_value(v, exclude_nil_value, excluded_keys) } - when OpenStruct - # LOGGER.debug(" > thing is OpenStruct") - recursive_symbolize_keys(thing.to_h, exclude_nil_value, excluded_keys) - # when Base - # recursive_symbolize_keys(thing.to_hash, exclude_nil_value) - else - # LOGGER.debug(" > thing type is not recognized") - thing - end - end - - # UTILITY: be an object comparables with another one, indipendently the order of its fields or the order of - # its array elements - # For each array in the object (Hash or OpenStruct) or nested into subobject, this method order the elements. - # That permits to compare two objects (Hash or OpenStruct). - # Es. - # {:a => [3,1,2], :b => {:b1 => [30,10,50], :b2 => [300,100,500]}} - # becomes {:a => [1,2,3], :b => {:b1 => [10,30,50], :b2 => [100,300,500]}} - # - # In case the elements of array are hashs, then it orders them by their hash value - def self.order_inner_array_elements(obj, exclude_nil_value = false, excluded_keys = []) - if obj.is_a?Array - obj.map do |e| - val = (e.to_h == {}) ? e : e.to_h - order_inner_array_elements(val, exclude_nil_value, excluded_keys) - end - #obj.map{|e| order_inner_array_elements(e.to_h, exclude_nil_value, excluded_keys)} - else - {}.tap do |h| - obj.each do |key, value| - next if excluded_keys.include?(key) - next if (exclude_nil_value && value.nil?) - v = map_ordered_value(value, exclude_nil_value, excluded_keys) - h[key.to_sym] = v - end - end - end - end - - def self.map_ordered_value(thing, exclude_nil_value = false, excluded_keys = []) - case thing - when Hash - result = order_inner_array_elements(thing, exclude_nil_value, excluded_keys) - when Array - result = thing.map do |v| - case v - when Hash, Array, OpenStruct - map_ordered_value(v, exclude_nil_value, excluded_keys) - else - v - end - end - result = result.sort_by{|e| e.is_a?(Hash) ? e.hash : e} - when OpenStruct - result = order_inner_array_elements(thing.to_h, exclude_nil_value, excluded_keys) - else - result = thing - end - result - end - end - end -end \ No newline at end of file