Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Ubiquity
- class ApiSerializer
- attr_accessor :data
- def initialize (data)
- @data ||= data
- end
- def license_for_api
- if data.try(:license).present?
- remap_license
- end
- end
- def rights_statements_for_api
- if data.try(:license).present?
- remap_rights_statement
- end
- end
- def member_of_collections_for_api
- if data.try(:member_of_collections) && data.class != FileSet
- data.member_of_collections.map do |collection_object|
- {uuid: collection_object.id, title: collection_object.title.first}
- end
- end
- end
- def alternate_identifier_for_api
- if data.try(:alternate_identifier) && data.class != FileSet
- remap_alternate_identifier
- end
- end
- def related_identifier_for_api
- if data.try(:related_identifier) && data.class != FileSet
- remap_related_identifier
- end
- end
- def data_filesets_summary_for_api
- if data.try(:file_sets)
- get_file_sets_totals
- end
- end
- def hash_map
- creator = JSON.parse(data.creator.try(:first)) if valid_json?(creator)
- editor = JSON.parse(data.try(:editor).try(:first) ) if valid_json?(editor)
- contributor = JSON.parse(data.contributor.try(:first) )if valid_json?(contributor)
- event = JSON.parse(data.try(:event).try(:first)) if valid_json?(event)
- {
- uuid: data.id,
- related_url: data.try(:related_url),
- type: 'work',
- data_type: data.try(:has_model).try(:first),
- title: data.title.try(:first),
- alternative_title: data.try(:alternative_title),
- resource_type: data.resource_type.first.try(:split).try(:first),
- creator: creator,
- contributor: contributor,
- editor: editor,
- abstract: data.try(:abstract),
- date_published: data.date_published,
- institution: data.institution,
- organisational_unit: data.org_unit,
- project_name: data.project_name,
- funder: data.funder,
- publisher: data.publisher,
- date_accepted: data.date_accepted,
- date_submitted: data.date_submitted,
- official_url: data.try(:official_url),
- language: data.language,
- license: data.license_for_api,
- rights_statement: data.rights_statements_for_api,
- rights_holder: data.rights_holder,
- doi: data.doi,
- alternate_identifier: data.alternate_identifier_for_api,
- peer_reviewed: data.try(:peer_reviewed),
- keywords: data.keyword,
- dewey: data.dewey,
- library_of_congress_classification: data.library_of_congress_classification,
- additional_info: data.try(:add_info),
- related_identifier: data.related_identifier_for_api,
- version: data.try(:version),
- duration: data.try(:duration),
- pagination: data.pagination,
- series_name: data.try(:series_name),
- issue: data.try(:issue),
- volume: data.try(:volume),
- material_media: data.try(:material_media),
- edition: data.try(:edition),
- event: event,
- journal_title: data.try(:journal_title),
- book_title: data.try(:book_title),
- article_number: data.try(:article_number),
- eissn: data.try(:eissn),
- issn: data.try(:issn),
- isbn: data.try(:isbn),
- current_he_institution: data.try(:current_he_institution),
- qualification: data.try(:qualification),
- alternative_journal_title: data.try(:alternative_journal_title),
- thumbnail_url: img_path,
- files: data.data_filesets_summary_for_api,
- collections: data.member_of_collections_for_api
- }
- end
- private
- #leave it as a public method because it used in other files
- # return false if == String
- def valid_?(data)
- !!JSON.parse(data) if data.class == String
- rescue ::ParserError
- false
- end
- def img_path
- if @data.thumbnail.present?
- img_path = CGI.escape(ActiveFedora::File.uri_to_id(data.thumbnail.original_file.versions.all.last.uri))
- img_path.presence?
- end
- end
- def remap_rights_statement
- rights_statement_hash = Hyrax::RightsStatementService.new.select_all_options.to_h
- data.rights_statement.map do |item|
- if rights_statement_hash.key(item)
- {
- name: rights_statement_hash.key(item),
- link: item
- }
- end
- end
- end
- def remap_license
- license_hash = Hyrax::LicenseService.new.select_all_options.to_h
- data.license.map do |item|
- if license_hash.key(item)
- {
- name: license_hash.key(item),
- link: item
- }
- end
- end
- end
- def remap_alternate_identifier
- if valid_json?(data.alternate_identifier.first)
- alternate_identifier_array = JSON.parse(data.alternate_identifier.first)
- alternate_identifier_array.map do |hash|
- {
- name: hash['alternate_identifier'],
- type: hash['alternate_identifier_type'],
- postion: hash["alternate_identifier_position"].to_i
- }
- end
- end
- end
- def remap_related_identifier
- if valid_json?(data.related_identifier.first)
- related_identifier_array = JSON.parse(data.related_identifier.first)
- related_identifier_array.map do |hash|
- {
- name: hash['related_identifier'],
- type: hash['related_identifier_type'],
- relationship: hash['relation_type'],
- postion: hash["related_identifier_position"].to_i
- }
- end
- end
- end
- def get_file_sets_totals
- file_count = data.file_sets.count
- #
- #returns hash that group files by their visibility eg {"open"=>2}
- total_summary = data.file_sets.group_by(&:visibility).transform_values(&:count)
- total_summary.merge({total: file_count})
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement