Class: OrcidClient::Notification

Inherits:
Object
  • Object
show all
Includes:
Api, Author, Base, Date
Defined in:
lib/orcid_client/notification.rb

Constant Summary collapse

SCHEMA =
File.expand_path("../../../resources/notification_#{API_VERSION}/notification-permission-#{API_VERSION}.xsd", __FILE__)

Constants included from Api

Api::API_VERSION

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 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:, notification_access_token:, **options) ⇒ Notification

Returns a new instance of Notification.



18
19
20
21
22
23
24
25
26
# File 'lib/orcid_client/notification.rb', line 18

def initialize(doi:, orcid:, notification_access_token:, **options)
  @doi = doi
  @orcid = orcid
  @notification_access_token = notification_access_token
  @put_code = options.fetch(:put_code, nil)
  @subject = options.fetch(:subject, nil)
  @intro = options.fetch(:intro, nil)
  @notification_host = options[:sandbox] ? 'sandbox.orcid.org' : 'orcid.org'
end

Instance Attribute Details

#doiObject (readonly)

Returns the value of attribute doi.



16
17
18
# File 'lib/orcid_client/notification.rb', line 16

def doi
  @doi
end

#introObject (readonly)

Returns the value of attribute intro.



16
17
18
# File 'lib/orcid_client/notification.rb', line 16

def intro
  @intro
end

#notification_access_tokenObject (readonly)

Returns the value of attribute notification_access_token.



16
17
18
# File 'lib/orcid_client/notification.rb', line 16

def notification_access_token
  @notification_access_token
end

#notification_hostObject (readonly)

Returns the value of attribute notification_host.



16
17
18
# File 'lib/orcid_client/notification.rb', line 16

def notification_host
  @notification_host
end

#orcidObject (readonly)

Returns the value of attribute orcid.



16
17
18
# File 'lib/orcid_client/notification.rb', line 16

def orcid
  @orcid
end

#put_codeObject (readonly)

Returns the value of attribute put_code.



16
17
18
# File 'lib/orcid_client/notification.rb', line 16

def put_code
  @put_code
end

#schemaObject (readonly)

Returns the value of attribute schema.



16
17
18
# File 'lib/orcid_client/notification.rb', line 16

def schema
  @schema
end

#subjectObject (readonly)

Returns the value of attribute subject.



16
17
18
# File 'lib/orcid_client/notification.rb', line 16

def subject
  @subject
end

#validation_errorsObject (readonly)

Returns the value of attribute validation_errors.



16
17
18
# File 'lib/orcid_client/notification.rb', line 16

def validation_errors
  @validation_errors
end

Instance Method Details

#dataObject



46
47
48
49
50
51
52
53
54
# File 'lib/orcid_client/notification.rb', line 46

def data
  return nil unless has_required_elements?

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

#has_required_elements?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/orcid_client/notification.rb', line 42

def has_required_elements?
  doi && item_name
end

#insert_authorization_url(xml) ⇒ Object



68
69
70
71
72
73
# File 'lib/orcid_client/notification.rb', line 68

def insert_authorization_url(xml)
  xml.send(:'notification:authorization-url') do
    xml.send(:'notification:path', work_notification_path)
    xml.send(:'notification:host', notification_host)
  end
end

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



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

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_items(xml) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/orcid_client/notification.rb', line 87

def insert_items(xml)
  return nil unless has_required_elements?

  xml.send(:'notification:items') do
    xml.send(:'notification:item') do
      xml.send(:'notification:item-type', item_type)
      xml.send(:'notification:item-name', item_name)

      insert_id(xml, "doi", doi, "self")
    end
  end
end

#insert_notification(xml) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/orcid_client/notification.rb', line 56

def insert_notification(xml)
  insert_notification_type(xml)
  insert_authorization_url(xml)
  insert_notification_subject(xml)
  insert_notification_intro(xml)
  insert_items(xml)
end

#insert_notification_intro(xml) ⇒ Object



83
84
85
# File 'lib/orcid_client/notification.rb', line 83

def insert_notification_intro(xml)
  xml.send(:'notification:notification-intro', intro)
end

#insert_notification_subject(xml) ⇒ Object



79
80
81
# File 'lib/orcid_client/notification.rb', line 79

def insert_notification_subject(xml)
  xml.send(:'notification:notification-subject', subject)
end

#insert_notification_type(xml) ⇒ Object



64
65
66
# File 'lib/orcid_client/notification.rb', line 64

def insert_notification_type(xml)
  xml.send(:'notification:notification-type', "permission")
end

#item_nameObject



34
35
36
# File 'lib/orcid_client/notification.rb', line 34

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

#item_typeObject



38
39
40
# File 'lib/orcid_client/notification.rb', line 38

def item_type
  "work"
end

#metadataObject



30
31
32
# File 'lib/orcid_client/notification.rb', line 30

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

#root_attributesObject



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

def root_attributes
  { :'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
    :'xsi:schemaLocation' => 'http://www.orcid.org/ns/notification ../notification-permission-2.1.xsd',
    :'xmlns:common' => 'http://www.orcid.org/ns/common',
    :'xmlns:notification' => 'http://www.orcid.org/ns/notification' }
end

#work_notification_pathObject



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

def work_notification_path
  "/oauth/authorize?client_id=#{ENV['ORCID_CLIENT_ID']}&response_type=code&scope=/read-limited%20/activities/update%20/person/update&redirect_uri=#{ENV['REDIRECT_URI']}"
end