Class: Commonmeta::Metadata

Inherits:
Object
  • Object
show all
Includes:
MetadataUtils
Defined in:
lib/commonmeta/metadata.rb

Constant Summary

Constants included from Readers::SchemaOrgReader

Readers::SchemaOrgReader::SO_TO_DC_RELATION_TYPES, Readers::SchemaOrgReader::SO_TO_DC_REVERSE_RELATION_TYPES

Constants included from Utils

Utils::BIB_TO_CM_TRANSLATIONS, Utils::CM_TO_BIB_TRANSLATIONS, Utils::CM_TO_CR_TRANSLATIONS, Utils::CM_TO_CSL_TRANSLATIONS, Utils::CM_TO_DC_TRANSLATIONS, Utils::CM_TO_JATS_TRANSLATIONS, Utils::CM_TO_RIS_TRANSLATIONS, Utils::CM_TO_SO_TRANSLATIONS, Utils::CR_TO_CM_TRANSLATIONS, Utils::CSL_TO_CM_TRANSLATIONS, Utils::DC_TO_CM_TRANSLATIONS, Utils::NORMALIZED_LICENSES, Utils::RIS_TO_CM_TRANSLATIONS, Utils::SO_TO_CM_TRANSLATIONS, Utils::UNKNOWN_INFORMATION

Constants included from SchemaUtils

SchemaUtils::COMMONMETA

Instance Attribute Summary collapse

Attributes included from MetadataUtils

#name_detector, #reverse

Instance Method Summary collapse

Methods included from MetadataUtils

#container_title, #csl_hsh, #graph, #locale, #raw, #should_passthru, #style

Methods included from Writers::TurtleWriter

#turtle

Methods included from Writers::SchemaOrgWriter

#schema_hsh, #schema_org

Methods included from Writers::RisWriter

#ris

Methods included from Writers::RdfXmlWriter

#rdf_xml

Methods included from Writers::JatsWriter

#insert_authors, #insert_citation, #insert_citation_title, #insert_contributor, #insert_editors, #insert_fpage, #insert_issue, #insert_lpage, #insert_pub_id, #insert_publication_date, #insert_publisher_name, #insert_source, #insert_version, #insert_volume, #is_article?, #is_chapter?, #is_data?, #jats, #publication_type

Methods included from Writers::DataciteWriter

#datacite, #datacite_contributor, #datacite_reference

Methods included from Writers::CsvWriter

#csv

Methods included from Writers::CslWriter

#csl

Methods included from Writers::CrossrefXmlWriter

#crossref_xml

Methods included from Writers::CodemetaWriter

#codemeta

Methods included from Writers::CffWriter

#cff, #write_cff_creators, #write_cff_reference

Methods included from Writers::CitationWriter

#citation

Methods included from Writers::BibtexWriter

#bibtex

Methods included from Readers::SchemaOrgReader

#get_schema_org, #read_schema_org, #schema_org_has_part, #schema_org_is_identical_to, #schema_org_is_new_version_of, #schema_org_is_part_of, #schema_org_is_previous_version_of, #schema_org_is_referenced_by, #schema_org_is_supplement_to, #schema_org_is_supplemented_by, #schema_org_reference, #schema_org_references

Methods included from Readers::RisReader

#read_ris, #ris_meta

Methods included from Readers::NpmReader

#get_npm, #read_npm

Methods included from Readers::DataciteReader

#format_contributor, #get_datacite, #get_datacite_reference, #read_datacite

Methods included from Readers::CslReader

#read_csl

Methods included from Readers::CrossrefXmlReader

#crossref_alternate_identifiers, #crossref_date_published, #crossref_description, #crossref_funding_reference, #crossref_license, #crossref_people, #crossref_references, #get_crossref_xml, #read_crossref_xml

Methods included from Readers::CrossrefReader

#get_crossref, #get_reference, #read_crossref

Methods included from Readers::CodemetaReader

#get_codemeta, #read_codemeta

Methods included from Readers::CffReader

#cff_creators, #get_cff, #get_cff_reference, #read_cff

Methods included from Readers::BibtexReader

#read_bibtex

Methods included from Utils

#decode_container_id, #decode_doi, #encode_container_id, #encode_doi, #find_from_format, #find_from_format_by_ext, #find_from_format_by_filename, #find_from_format_by_id, #find_from_format_by_string, #from_csl, #from_datacite, #from_schema_org, #get_contributor, #get_date, #get_date_from_date_parts, #get_date_from_parts, #get_date_parts, #get_date_parts_from_parts, #get_dates_from_date, #get_datetime_from_iso8601, #get_datetime_from_time, #get_identifier, #get_identifier_type, #get_iso8601_date, #get_series_information, #get_year_month, #get_year_month_day, #github_as_cff_url, #github_as_codemeta_url, #github_as_owner_url, #github_as_release_url, #github_as_repo_url, #github_from_url, #github_owner_from_url, #github_release_from_url, #github_repo_from_url, #hsh_to_fos, #hsh_to_spdx, #jsonlint, #map_hash_keys, #name_to_fos, #name_to_spdx, #normalize_cc_url, #normalize_id, #normalize_issn, #normalize_licenses, #normalize_orcid, #normalize_url, #orcid_as_url, #orcid_from_url, #parse_attributes, #sanitize, #spdx_to_hsh, #strip_milliseconds, #to_csl, #to_datacite, #to_identifier, #to_ris, #to_schema_org, #to_schema_org_citation, #to_schema_org_container, #to_schema_org_funder, #to_schema_org_identifiers, #to_schema_org_relation, #to_schema_org_spatial_coverage, #validate_orcid, #validate_orcid_scheme, #validate_url

Methods included from SchemaUtils

#json_schema_errors

Methods included from DoiUtils

#crossref_api_url, #datacite_api_url, #doi_as_url, #doi_from_url, #doi_resolver, #get_crossref_member, #get_doi_ra, #normalize_doi, #validate_doi, #validate_funder_doi, #validate_prefix

Methods included from CrossrefUtils

#crossref_errors, #crossref_root_attributes, #insert_citation_list, #insert_crossref_abstract, #insert_crossref_access_indicators, #insert_crossref_alternate_identifiers, #insert_crossref_creators, #insert_crossref_issn, #insert_crossref_language, #insert_crossref_license, #insert_crossref_person, #insert_crossref_publication_date, #insert_crossref_subjects, #insert_crossref_titles, #insert_crossref_work, #insert_doi_data, #insert_group_title, #insert_institution, #insert_journal, #insert_posted_content, #insert_posted_date, #write_crossref_xml

Methods included from AuthorUtils

#author_name_identifiers, #authors_as_string, #cleanup_author, #get_affiliations, #get_authors, #get_one_author, #is_personal_name?, #name_exists?

Constructor Details

#initialize(options = {}) ⇒ Metadata

Returns a new instance of Metadata.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/commonmeta/metadata.rb', line 15

def initialize(options = {})
  options.symbolize_keys!

  id = normalize_id(options[:input], options)
  ra = nil

  if id.present?
    @from = options[:from] || find_from_format(id: id)

    # mEDRA, KISTI, JaLC and OP DOIs are found in the Crossref index
    case @from
    when 'medra'
      ra = 'mEDRA'
    when 'kisti'
      ra = 'KISTI'
    when 'jalc'
      ra = 'JaLC'
    when 'op'
      ra = 'OP'
    end

    # generate name for method to call dynamically
    hsh = @from.present? ? send("get_#{@from}", id: id, **options) : {}
    string = hsh.fetch('string', nil)
  elsif options[:input].present? && File.exist?(options[:input])
    filename = File.basename(options[:input])
    ext = File.extname(options[:input])
    if %w[.bib .ris .xml .json .cff].include?(ext)
      hsh = {
        'url' => options[:url],
        'state' => options[:state],
        'provider_id' => options[:provider_id],
        'client_id' => options[:client_id],
        'depositor' => options[:depositor],
        'email' => options[:email],
        'registrant' => options[:registrant],
        'content_url' => options[:content_url]
      }
      string = File.read(options[:input])
      @from = options[:from] || find_from_format(string: string, ext: ext)
    else
      warn "File type #{ext} not supported"
      exit 1
    end
  else
    hsh = {
      'url' => options[:url],
      'state' => options[:state],
      'provider_id' => options[:provider_id],
      'client_id' => options[:client_id],
      'depositor' => options[:depositor],
      'email' => options[:email],
      'registrant' => options[:registrant],
      'content_url' => options[:content_url],
      'creators' => options[:creators],
      'contributors' => options[:contributors],
      'titles' => options[:titles],
      'publisher' => options[:publisher]
    }
    string = options[:input]
    @from = options[:from] || find_from_format(string: string)
  end

  # make sure input is encoded as utf8
  if string.present? && string.is_a?(String)
    dup_string = string.dup.force_encoding('UTF-8').encode!
  end
  @string = dup_string

  # input options for citation formatting
  @style = options[:style]
  @locale = options[:locale]

  @sandbox = options[:sandbox]

  # options that come from the datacite database
  @url = hsh.to_h['url'].presence || options[:url].presence
  @state = hsh.to_h['state'].presence
  @provider_id = hsh.to_h['provider_id'].presence
  @client_id = hsh.to_h['client_id'].presence
  @content_url = hsh.to_h['content_url'].presence

  # options that come from the submission, needed
  # for crossref doi registration
  @depositor = hsh.to_h['depositor'].presence
  @email = hsh.to_h['email'].presence
  @registrant = hsh.to_h['registrant'].presence

  # set attributes directly
  read_options = options.slice(
    :creators,
    :contributors,
    :titles,
    :type,
    :additional_type,
    :container,
    :publisher,
    :funding_references,
    :date,
    :descriptions,
    :rights_list,
    :version,
    :subjects,
    :language,
    :geo_locations,
    :references,
    :alternate_identifiers,
    :related_identifiers,
    :related_items,
    :formats,
    :sizes
  ).compact

  @regenerate = options[:regenerate] || read_options.present?
  # generate name for method to call dynamically
  opts = { string: @string, sandbox: options[:sandbox], doi: options[:doi], id: id,
           ra: ra }.merge(read_options)
  @meta = @from.present? ? send("read_#{@from}", **opts) : {}
end

Instance Attribute Details

#additional_type ⇒ Object



253
254
255
# File 'lib/commonmeta/metadata.rb', line 253

def additional_type
  @additional_type ||= meta.fetch('additional_type', nil)
end

#alternate_identifiers ⇒ Object



233
234
235
# File 'lib/commonmeta/metadata.rb', line 233

def alternate_identifiers
  @alternate_identifiers ||= meta.fetch('alternate_identifiers', nil)
end

#client_id ⇒ Object



147
148
149
# File 'lib/commonmeta/metadata.rb', line 147

def client_id
  @client_id ||= meta.fetch('client_id', nil)
end

#container ⇒ Object



217
218
219
# File 'lib/commonmeta/metadata.rb', line 217

def container
  @container ||= meta.fetch('container', nil)
end

#content_url ⇒ Object



237
238
239
# File 'lib/commonmeta/metadata.rb', line 237

def content_url
  @content_url ||= meta.fetch('content_url', nil)
end

#contributor=(value) ⇒ Object (writeonly)

Sets the attribute contributor

Parameters:

  • value —

    the value to set the attribute contributor to.



12
13
14
# File 'lib/commonmeta/metadata.rb', line 12

def contributor=(value)
  @contributor = value
end

#contributors ⇒ Object



265
266
267
# File 'lib/commonmeta/metadata.rb', line 265

def contributors
  @contributors ||= meta.fetch('contributors', nil)
end

#creators ⇒ Object



261
262
263
# File 'lib/commonmeta/metadata.rb', line 261

def creators
  @creators ||= meta.fetch('creators', nil)
end

#date ⇒ Object



225
226
227
# File 'lib/commonmeta/metadata.rb', line 225

def date
  @date ||= meta.fetch('date', nil)
end

#depositor ⇒ Object

Returns the value of attribute depositor.



9
10
11
# File 'lib/commonmeta/metadata.rb', line 9

def depositor
  @depositor
end

#descriptions ⇒ Object



165
166
167
# File 'lib/commonmeta/metadata.rb', line 165

def descriptions
  @descriptions ||= meta.fetch('descriptions', nil)
end

#doc ⇒ Object (readonly)

Returns the value of attribute doc.



11
12
13
# File 'lib/commonmeta/metadata.rb', line 11

def doc
  @doc
end

#doi ⇒ Object



139
140
141
# File 'lib/commonmeta/metadata.rb', line 139

def doi
  @doi ||= meta.fetch('doi', nil)
end

#email ⇒ Object

Returns the value of attribute email.



9
10
11
# File 'lib/commonmeta/metadata.rb', line 9

def email
  @email
end

#format=(value) ⇒ Object (writeonly)

Sets the attribute format

Parameters:

  • value —

    the value to set the attribute format to.



12
13
14
# File 'lib/commonmeta/metadata.rb', line 12

def format=(value)
  @format = value
end

#formats ⇒ Object



185
186
187
# File 'lib/commonmeta/metadata.rb', line 185

def formats
  @formats ||= meta.fetch('formats', nil)
end

#from ⇒ Object

Returns the value of attribute from.



9
10
11
# File 'lib/commonmeta/metadata.rb', line 9

def from
  @from
end

#funding_references ⇒ Object



193
194
195
# File 'lib/commonmeta/metadata.rb', line 193

def funding_references
  @funding_references ||= meta.fetch('funding_references', nil)
end

#geo_locations ⇒ Object



221
222
223
# File 'lib/commonmeta/metadata.rb', line 221

def geo_locations
  @geo_locations ||= meta.fetch('geo_locations', nil)
end

#id ⇒ Object



135
136
137
# File 'lib/commonmeta/metadata.rb', line 135

def id
  @id ||= meta.fetch('id', nil)
end

#issue ⇒ Object

Returns the value of attribute issue.



9
10
11
# File 'lib/commonmeta/metadata.rb', line 9

def issue
  @issue
end

#language ⇒ Object



177
178
179
# File 'lib/commonmeta/metadata.rb', line 177

def language
  @language ||= meta.fetch('language', nil)
end

#license ⇒ Object



169
170
171
# File 'lib/commonmeta/metadata.rb', line 169

def license
  @license ||= meta.fetch('license', nil)
end

#locale=(value) ⇒ Object (writeonly)

Sets the attribute locale

Parameters:

  • value —

    the value to set the attribute locale to.



12
13
14
# File 'lib/commonmeta/metadata.rb', line 12

def locale=(value)
  @locale = value
end

#meta ⇒ Object

Returns the value of attribute meta.



9
10
11
# File 'lib/commonmeta/metadata.rb', line 9

def meta
  @meta
end

#page_end ⇒ Object (readonly)

Returns the value of attribute page_end.



11
12
13
# File 'lib/commonmeta/metadata.rb', line 11

def page_end
  @page_end
end

#page_start ⇒ Object (readonly)

Returns the value of attribute page_start.



11
12
13
# File 'lib/commonmeta/metadata.rb', line 11

def page_start
  @page_start
end

#provider ⇒ Object



241
242
243
# File 'lib/commonmeta/metadata.rb', line 241

def provider
  @provider ||= meta.fetch('provider', nil)
end

#provider_id ⇒ Object



143
144
145
# File 'lib/commonmeta/metadata.rb', line 143

def provider_id
  @provider_id ||= meta.fetch('provider_id', nil)
end

#publisher ⇒ Object



229
230
231
# File 'lib/commonmeta/metadata.rb', line 229

def publisher
  @publisher ||= meta.fetch('publisher', nil)
end

#references ⇒ Object



197
198
199
# File 'lib/commonmeta/metadata.rb', line 197

def references
  @references ||= meta.fetch('references', nil)
end

#regenerate ⇒ Object

Returns the value of attribute regenerate.



9
10
11
# File 'lib/commonmeta/metadata.rb', line 9

def regenerate
  @regenerate
end

#registrant ⇒ Object

Returns the value of attribute registrant.



9
10
11
# File 'lib/commonmeta/metadata.rb', line 9

def registrant
  @registrant
end


201
202
203
# File 'lib/commonmeta/metadata.rb', line 201

def related_identifiers
  @related_identifiers ||= meta.fetch('related_identifiers', nil)
end


205
206
207
# File 'lib/commonmeta/metadata.rb', line 205

def related_items
  @related_items ||= meta.fetch('related_items', nil)
end

#sandbox ⇒ Object

Returns the value of attribute sandbox.



9
10
11
# File 'lib/commonmeta/metadata.rb', line 9

def sandbox
  @sandbox
end

#schema_version ⇒ Object



189
190
191
# File 'lib/commonmeta/metadata.rb', line 189

def schema_version
  @schema_version ||= meta.fetch('schema_version', nil)
end

#show_errors ⇒ Object

Returns the value of attribute show_errors.



9
10
11
# File 'lib/commonmeta/metadata.rb', line 9

def show_errors
  @show_errors
end

#sizes ⇒ Object



181
182
183
# File 'lib/commonmeta/metadata.rb', line 181

def sizes
  @sizes ||= meta.fetch('sizes', nil)
end

#state ⇒ Object



245
246
247
# File 'lib/commonmeta/metadata.rb', line 245

def state
  @state ||= meta.fetch('state', nil)
end

#string ⇒ Object

Returns the value of attribute string.



9
10
11
# File 'lib/commonmeta/metadata.rb', line 9

def string
  @string
end

#style=(value) ⇒ Object (writeonly)

Sets the attribute style

Parameters:

  • value —

    the value to set the attribute style to.



12
13
14
# File 'lib/commonmeta/metadata.rb', line 12

def style=(value)
  @style = value
end

#subjects ⇒ Object



173
174
175
# File 'lib/commonmeta/metadata.rb', line 173

def subjects
  @subjects ||= meta.fetch('subjects', nil)
end

#titles ⇒ Object



257
258
259
# File 'lib/commonmeta/metadata.rb', line 257

def titles
  @titles ||= meta.fetch('titles', nil)
end

#type ⇒ Object



249
250
251
# File 'lib/commonmeta/metadata.rb', line 249

def type
  @type ||= meta.fetch('type', nil)
end

#url ⇒ Object



209
210
211
# File 'lib/commonmeta/metadata.rb', line 209

def url
  @url ||= meta.fetch('url', nil)
end

#version ⇒ Object



213
214
215
# File 'lib/commonmeta/metadata.rb', line 213

def version
  @version ||= meta.fetch('version', nil)
end

#volume=(value) ⇒ Object (writeonly)

Sets the attribute volume

Parameters:

  • value —

    the value to set the attribute volume to.



12
13
14
# File 'lib/commonmeta/metadata.rb', line 12

def volume=(value)
  @volume = value
end

Instance Method Details

#errors ⇒ Object

Catch errors in the reader Then validate against JSON schema for Commonmeta



161
162
163
# File 'lib/commonmeta/metadata.rb', line 161

def errors
  meta.fetch('errors', nil) || json_schema_errors
end

#exists? ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/commonmeta/metadata.rb', line 151

def exists?
  (@state || meta.fetch('state', nil)) != 'not_found'
end

#valid? ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/commonmeta/metadata.rb', line 155

def valid?
  exists? && errors.nil?
end