Class: OrcidClient::Work
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.
24
25
26
27
28
29
30
31
32
|
# File 'lib/orcid_client/work.rb', line 24
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)
@agency = options.fetch(:agency, nil)
@visibility = options.fetch(:visibility, 'public')
end
|
Instance Attribute Details
#agency ⇒ Object
Returns the value of attribute agency.
20
21
22
|
# File 'lib/orcid_client/work.rb', line 20
def agency
@agency
end
|
#doi ⇒ Object
Returns the value of attribute doi.
20
21
22
|
# File 'lib/orcid_client/work.rb', line 20
def doi
@doi
end
|
#name_detector ⇒ Object
recognize given name. Can be loaded once as ::NameDetector, e.g. in a Rails initializer
37
38
39
|
# File 'lib/orcid_client/work.rb', line 37
def name_detector
@name_detector
end
|
#orcid ⇒ Object
Returns the value of attribute orcid.
20
21
22
|
# File 'lib/orcid_client/work.rb', line 20
def orcid
@orcid
end
|
#orcid_token ⇒ Object
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_code ⇒ Object
Returns the value of attribute put_code.
20
21
22
|
# File 'lib/orcid_client/work.rb', line 20
def put_code
@put_code
end
|
#sandbox ⇒ Object
Returns the value of attribute sandbox.
20
21
22
|
# File 'lib/orcid_client/work.rb', line 20
def sandbox
@sandbox
end
|
#schema ⇒ Object
Returns the value of attribute schema.
20
21
22
|
# File 'lib/orcid_client/work.rb', line 20
def schema
@schema
end
|
#validation_errors ⇒ Object
Returns the value of attribute validation_errors.
20
21
22
|
# File 'lib/orcid_client/work.rb', line 20
def validation_errors
@validation_errors
end
|
#visibility ⇒ Object
Returns the value of attribute visibility.
20
21
22
|
# File 'lib/orcid_client/work.rb', line 20
def visibility
@visibility
end
|
Instance Method Details
#container_title ⇒ Object
user publisher name as fallback
61
62
63
|
# File 'lib/orcid_client/work.rb', line 61
def container_title
metadata.container_title || metadata.publisher.present? ? metadata.publisher["name"] : nil
end
|
#contributors ⇒ Object
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/orcid_client/work.rb', line 45
def contributors
Array.wrap(metadata.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
|
#data ⇒ Object
83
84
85
86
87
88
89
90
91
|
# File 'lib/orcid_client/work.rb', line 83
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
|
#description ⇒ Object
70
71
72
73
|
# File 'lib/orcid_client/work.rb', line 70
def description
ct = parse_attributes(metadata.descriptions, content: "description", first: true)
ct.squish if ct.present?
end
|
#has_required_elements? ⇒ Boolean
79
80
81
|
# File 'lib/orcid_client/work.rb', line 79
def has_required_elements?
doi && contributors && title && container_title && publication_date
end
|
#insert_contributor(xml, contributor) ⇒ Object
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/orcid_client/work.rb', line 158
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
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/orcid_client/work.rb', line 146
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
112
113
114
115
116
|
# File 'lib/orcid_client/work.rb', line 112
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
138
139
140
141
142
143
144
|
# File 'lib/orcid_client/work.rb', line 138
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
132
133
134
135
136
|
# File 'lib/orcid_client/work.rb', line 132
def insert_ids(xml)
xml.send(:'common:external-ids') do
insert_id(xml, 'doi', doi, 'self')
end
end
|
#insert_pub_date(xml) ⇒ Object
122
123
124
125
126
127
128
129
130
|
# File 'lib/orcid_client/work.rb', line 122
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
102
103
104
105
106
107
108
109
110
|
# File 'lib/orcid_client/work.rb', line 102
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
118
119
120
|
# File 'lib/orcid_client/work.rb', line 118
def insert_type(xml)
xml.send(:'work:type', type)
end
|
#insert_work(xml) ⇒ Object
93
94
95
96
97
98
99
100
|
# File 'lib/orcid_client/work.rb', line 93
def insert_work(xml)
insert_titles(xml)
insert_description(xml)
insert_type(xml)
insert_pub_date(xml)
insert_ids(xml)
insert_contributors(xml)
end
|
41
42
43
|
# File 'lib/orcid_client/work.rb', line 41
def metadata
@metadata ||= Bolognese::Metadata.new(input: doi, sandbox: sandbox, from: agency)
end
|
#publication_date ⇒ Object
65
66
67
68
|
# File 'lib/orcid_client/work.rb', line 65
def publication_date
pd = get_date(metadata.dates, "Issued") || metadata.publication_year
get_year_month_day(pd)
end
|
#root_attributes ⇒ Object
184
185
186
187
188
189
190
191
|
# File 'lib/orcid_client/work.rb', line 184
def root_attributes
{ :'put-code' => put_code,
:'visibility' => visibility,
:'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
|
#title ⇒ Object
56
57
58
|
# File 'lib/orcid_client/work.rb', line 56
def title
parse_attributes(metadata.titles, content: "title", first: true)
end
|
#type ⇒ Object
75
76
77
|
# File 'lib/orcid_client/work.rb', line 75
def type
orcid_work_type(metadata.types["resourceTypeGeneral"], metadata.types["resourceType"])
end
|
#without_control(s) ⇒ Object
174
175
176
177
178
179
180
181
182
|
# File 'lib/orcid_client/work.rb', line 174
def without_control(s)
r = ''
s.each_codepoint do |c|
if c >= 32
r << c
end
end
r
end
|