module ADIWG::Mdtranslator::Writers::SbJson
Constants
- VERSION
Public Class Methods
build(intObj, responseObj)
click to toggle source
# File lib/adiwg/mdtranslator/writers/sbJson/sections/sbJson_sbJson.rb, line 34 def self.build(intObj, responseObj) metadataInfo = intObj[:metadata][:metadataInfo] resourceInfo = intObj[:metadata][:resourceInfo] distributorInfo = intObj[:metadata][:distributorInfo] hCitation = resourceInfo[:citation] @Namespace = ADIWG::Mdtranslator::Writers::SbJson Jbuilder.new do |json| resourceId = Id.build(intObj) json.id resourceId json.title hCitation[:title] unless hCitation.empty? json.alternateTitles hCitation[:alternateTitles] unless hCitation[:alternateTitles].empty? json.body Abstract.build(resourceInfo[:abstract]) json.citation Citation.build(hCitation) unless hCitation.empty? # gather all identifiers # include the metadataIdentifier if it is NOT in the 'gov.sciencebase.catalog' namespace # otherwise it would be the resourceId above aIdentifiers = [] unless metadataInfo[:metadataIdentifier].empty? unless metadataInfo[:metadataIdentifier][:namespace] == 'gov.sciencebase.catalog' aIdentifiers << metadataInfo[:metadataIdentifier] end end # do not duplicate the identifier which is the primary resource unless hCitation.empty? hCitation[:identifiers].each do |hIdentifier| unless hIdentifier[:identifier] == resourceId aIdentifiers << hIdentifier end end end # eliminate duplicate identifiers # duplicate must match on both ID and schema (namespace) aUniqIds = [] aIdentifiers.each do |hIdentifier| foundDup = false aUniqIds.each do |hUniqId| if hIdentifier[:identifier] == hUniqId[:identifier] if hIdentifier[:namespace] == hUniqId[:namespace] foundDup = true end end end unless foundDup aUniqIds << hIdentifier end end json.identifiers @Namespace.json_map(aUniqIds, Identifier) unless aIdentifiers.empty? json.purpose resourceInfo[:purpose] haveRights = false haveRights = true unless resourceInfo[:constraints].empty? distributorInfo.each do |hDistribution| unless hDistribution[:liabilityStatement].nil? haveRights = true end end if haveRights json.rights Rights.build(resourceInfo[:constraints], distributorInfo) end json.provenance Provenance.build json.materialRequestInstructions MaterialRequest.build(distributorInfo) unless distributorInfo.empty? json.parentId ParentId.build(metadataInfo[:parentMetadata]) unless metadataInfo[:parentMetadata].empty? aContactList = Contact.get_contact_list(intObj) json.contacts @Namespace.json_map(aContactList, Contact) unless aContactList.empty? json.webLinks WebLink.build(intObj[:metadata]) json.browseCategories BrowseCategory.build(resourceInfo[:resourceTypes]) json.tags Tag.build(intObj) json.dates Date.build(resourceInfo) unless resourceInfo.empty? json.spatial Spatial.build(resourceInfo[:extents]) unless resourceInfo[:extents].empty? json.facets Facet.build(intObj[:metadata]) json.geographicExtents GeographicExtent.build(resourceInfo[:extents]) unless resourceInfo[:extents].empty? end end
get_contact_by_id(contactId)
click to toggle source
find contact in contact array and return the contact hash
# File lib/adiwg/mdtranslator/writers/sbJson/sbJson_writer.rb, line 47 def self.get_contact_by_id(contactId) @contacts.each do |hContact| if hContact[:contactId] == contactId return hContact end end {} end
get_contact_by_index(contactIndex)
click to toggle source
find contact in contact array and return the contact hash
# File lib/adiwg/mdtranslator/writers/sbJson/sbJson_writer.rb, line 39 def self.get_contact_by_index(contactIndex) if @contacts[contactIndex] return @contacts[contactIndex] end {} end
get_contact_index_by_id(contactId)
click to toggle source
find contact in contact array and return the contact index
# File lib/adiwg/mdtranslator/writers/sbJson/sbJson_writer.rb, line 57 def self.get_contact_index_by_id(contactId) @contacts.each_with_index do |hContact, index| if hContact[:contactId] == contactId return index end end {} end
json_map(collection = [], _class)
click to toggle source
ignore jBuilder object mapping when array is empty
# File lib/adiwg/mdtranslator/writers/sbJson/sbJson_writer.rb, line 67 def self.json_map(collection = [], _class) if collection.nil? || collection.empty? return nil else collection.map { |item| _class.build(item).attributes! } end end
nested_objs_by_element(obj, ele, excludeList = [])
click to toggle source
find all nested objects in 'obj' that contain the element 'ele'
# File lib/adiwg/mdtranslator/writers/sbJson/sbJson_writer.rb, line 76 def self.nested_objs_by_element(obj, ele, excludeList = []) aCollected = [] obj.each do |key, value| skipThisOne = false excludeList.each do |exclude| if key == exclude.to_sym skipThisOne = true end end next if skipThisOne if key == ele.to_sym aCollected << obj elsif obj.is_a?(Array) if key.respond_to?(:each) aReturn = nested_objs_by_element(key, ele, excludeList) aCollected = aCollected.concat(aReturn) unless aReturn.empty? end elsif obj[key].respond_to?(:each) aReturn = nested_objs_by_element(value, ele, excludeList) aCollected = aCollected.concat(aReturn) unless aReturn.empty? end end aCollected end
startWriter(intObj, responseObj)
click to toggle source
# File lib/adiwg/mdtranslator/writers/sbJson/sbJson_writer.rb, line 17 def self.startWriter(intObj, responseObj) @contacts = intObj[:contacts] # set output flag for null properties Jbuilder.ignore_nil(!responseObj[:writerShowTags]) # set the format of the output file based on the writer specified responseObj[:writerOutputFormat] = 'json' responseObj[:writerVersion] = ADIWG::Mdtranslator::Writers::SbJson::VERSION # write the sbJson metadata record metadata = SbJson.build(intObj, responseObj) # set writer pass to true if no messages # false or warning state will be set by writer code responseObj[:writerPass] = true if responseObj[:writerMessages].empty? # encode the metadata target as JSON metadata.target! end