Class: OrcidClient::Work

Inherits:
Object
  • Object
show all
Includes:
Bolognese::Utils, Api, Author, Base, Date, WorkType
Defined in:
lib/orcid_client/work.rb

Constant Summary collapse

SCHEMA =
File.expand_path("../../../resources/record_#{API_VERSION}/work-#{API_VERSION}.xsd", __FILE__)

Constants included from Api

Api::API_VERSION

Constants included from WorkType

OrcidClient::WorkType::TYPE_OF_WORK

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Api

#create_external_identifier, #create_notification, #create_work, #delete_external_identifier, #delete_notification, #delete_work, #get_notification, #get_notification_access_token, #get_works, #update_work

Methods included from WorkType

#orcid_work_type

Methods included from Date

#get_date_parts, #get_date_parts_from_parts, #get_iso8601_from_epoch, #get_iso8601_from_time, #get_parts_from_date_parts, #get_year_month, #get_year_month_day

Methods included from Author

#get_credit_name, #get_full_name

Constructor Details

#initialize(doi:, orcid:, orcid_token:, **options) ⇒ Work

Returns a new instance of Work.



22
23
24
25
26
27
28
# File 'lib/orcid_client/work.rb', line 22

def initialize(doi:, orcid:, orcid_token:, **options)
  @doi = doi
  @orcid = orcid
  @orcid_token = orcid_token
  @sandbox = options.fetch(:sandbox, nil) || ENV['API_URL'] == "https://api.stage.datacite.org"
  @put_code = options.fetch(:put_code, nil)
end

Instance Attribute Details

#doiObject (readonly)

Returns the value of attribute doi.



20
21
22
# File 'lib/orcid_client/work.rb', line 20

def doi
  @doi
end

#name_detectorObject (readonly)

recognize given name. Can be loaded once as ::NameDetector, e.g. in a Rails initializer



33
34
35
# File 'lib/orcid_client/work.rb', line 33

def name_detector
  @name_detector
end

#orcidObject (readonly)

Returns the value of attribute orcid.



20
21
22
# File 'lib/orcid_client/work.rb', line 20

def orcid
  @orcid
end

#orcid_tokenObject (readonly)

Returns the value of attribute orcid_token.



20
21
22
# File 'lib/orcid_client/work.rb', line 20

def orcid_token
  @orcid_token
end

#put_codeObject (readonly)

Returns the value of attribute put_code.



20
21
22
# File 'lib/orcid_client/work.rb', line 20

def put_code
  @put_code
end

#sandboxObject (readonly)

Returns the value of attribute sandbox.



20
21
22
# File 'lib/orcid_client/work.rb', line 20

def sandbox
  @sandbox
end

#schemaObject (readonly)

Returns the value of attribute schema.



20
21
22
# File 'lib/orcid_client/work.rb', line 20

def schema
  @schema
end

#validation_errorsObject (readonly)

Returns the value of attribute validation_errors.



20
21
22
# File 'lib/orcid_client/work.rb', line 20

def validation_errors
  @validation_errors
end

Instance Method Details

#container_titleObject

user publisher name as fallback



57
58
59
# File 'lib/orcid_client/work.rb', line 57

def container_title
  .container_title || .publisher
end

#contributorsObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/orcid_client/work.rb', line 41

def contributors
  Array.wrap(.creators).map do |contributor|
    orcid = Array.wrap(contributor["nameIdentifiers"]).find { |c| c["nameIdentifierScheme"] == "ORCID" }.to_h.fetch("nameIdentifier", nil)
    credit_name = contributor["familyName"].present? ? [contributor["givenName"], contributor["familyName"]].join(" ") : contributor["name"]
    
    { orcid: orcid,
      credit_name: credit_name,
      role: nil }.compact
  end
end

#dataObject



79
80
81
82
83
84
85
86
87
# File 'lib/orcid_client/work.rb', line 79

def data
  return nil unless has_required_elements?

  Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
    xml.send(:'work:work', root_attributes) do
      insert_work(xml)
    end
  end.to_xml
end

#descriptionObject



66
67
68
69
# File 'lib/orcid_client/work.rb', line 66

def description
  ct = parse_attributes(.descriptions, content: "description", first: true)
  ct.squish if ct.present?
end

#has_required_elements?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/orcid_client/work.rb', line 75

def has_required_elements?
  doi && contributors && title && container_title && publication_date
end

#insert_contributor(xml, contributor) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/orcid_client/work.rb', line 154

def insert_contributor(xml, contributor)
  if contributor[:orcid].present?
    xml.send(:'common:contributor-orcid') do
      xml.send(:'common:uri', contributor[:orcid])
      xml.send(:'common:path', validate_orcid(contributor[:orcid]))
      xml.send(:'common:host', 'orcid.org')
    end
  end
  xml.send(:'credit-name', contributor[:credit_name])
  if contributor[:role]
    xml.send(:'contributor-attributes') do
      xml.send(:'contributor-role', contributor[:role])
    end
  end
end

#insert_contributors(xml) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/orcid_client/work.rb', line 142

def insert_contributors(xml)
  return nil unless contributors.present?

  xml.send(:'work:contributors') do
    contributors.each do |contributor|
      xml.contributor do
        insert_contributor(xml, contributor)
      end
    end
  end
end

#insert_description(xml) ⇒ Object



108
109
110
111
112
# File 'lib/orcid_client/work.rb', line 108

def insert_description(xml)
  return nil unless description.present?

  xml.send(:'work:short-description', description.truncate(2500, separator: ' '))
end

#insert_id(xml, id_type, value, relationship) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/orcid_client/work.rb', line 134

def insert_id(xml, id_type, value, relationship)
  xml.send(:'common:external-id') do
    xml.send(:'common:external-id-type', id_type)
    xml.send(:'common:external-id-value', value)
    xml.send(:'common:external-id-relationship', relationship)
  end
end

#insert_ids(xml) ⇒ Object



128
129
130
131
132
# File 'lib/orcid_client/work.rb', line 128

def insert_ids(xml)
  xml.send(:'common:external-ids') do
    insert_id(xml, 'doi', doi, 'self')
  end
end

#insert_pub_date(xml) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/orcid_client/work.rb', line 118

def insert_pub_date(xml)
  if publication_date['year']
    xml.send(:'common:publication-date') do
      xml.year(publication_date.fetch('year'))
      xml.month(publication_date.fetch('month', nil)) if publication_date['month']
      xml.day(publication_date.fetch('day', nil)) if publication_date['month'] && publication_date['day']
    end
  end
end

#insert_titles(xml) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/orcid_client/work.rb', line 98

def insert_titles(xml)
  if title
    xml.send(:'work:title') do
      xml.send(:'common:title', title.truncate(1000, separator: ' '))
    end
  end

  xml.send(:'work:journal-title', container_title) if container_title
end

#insert_type(xml) ⇒ Object



114
115
116
# File 'lib/orcid_client/work.rb', line 114

def insert_type(xml)
  xml.send(:'work:type', type)
end

#insert_work(xml) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/orcid_client/work.rb', line 89

def insert_work(xml)
  insert_titles(xml)
  insert_description(xml)
  insert_type(xml)
  insert_pub_date(xml)
  insert_ids(xml)
  insert_contributors(xml)
end

#metadataObject



37
38
39
# File 'lib/orcid_client/work.rb', line 37

def 
  @metadata ||= Bolognese::Metadata.new(input: doi, sandbox: sandbox)
end

#publication_dateObject



61
62
63
64
# File 'lib/orcid_client/work.rb', line 61

def publication_date
  pd = get_date(.dates, "Issued") || .publication_year
  get_year_month_day(pd)
end

#root_attributesObject



180
181
182
183
184
185
186
187
# File 'lib/orcid_client/work.rb', line 180

def root_attributes
  { :'put-code' => put_code,
    :'visibility' => 'public',
    :'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
    :'xsi:schemaLocation' => 'http://www.orcid.org/ns/work ../work-3.0.xsd',
    :'xmlns:common' => 'http://www.orcid.org/ns/common',
    :'xmlns:work' => 'http://www.orcid.org/ns/work' }.compact
end

#titleObject



52
53
54
# File 'lib/orcid_client/work.rb', line 52

def title
  parse_attributes(.titles, content: "title", first: true)
end

#typeObject



71
72
73
# File 'lib/orcid_client/work.rb', line 71

def type
  orcid_work_type(.types["resourceTypeGeneral"], .types["resourceType"])
end

#without_control(s) ⇒ Object



170
171
172
173
174
175
176
177
178
# File 'lib/orcid_client/work.rb', line 170

def without_control(s)
  r = ''
  s.each_codepoint do |c|
    if c >= 32
      r << c
    end
  end
  r
end