Class: Orcid::ProfileRequest

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/orcid/profile_request.rb

Overview

Responsible for:

  • acknowledging that an ORCID Profile was requested

  • submitting a request for an ORCID Profile

  • handling the response for the ORCID Profile creation

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_user(user) ⇒ Object



7
8
9
# File 'app/models/orcid/profile_request.rb', line 7

def self.find_by_user(user)
  where(user: user).first
end

Instance Method Details

#default_profile_creation_serviceObject



37
38
39
40
41
42
43
44
45
# File 'app/models/orcid/profile_request.rb', line 37

def default_profile_creation_service
  @default_profile_creation_service ||= begin
    Orcid::Remote::ProfileCreationService.new do |on|
      on.success do |orcid_profile_id|
        handle_profile_creation_response(orcid_profile_id)
      end
    end
  end
end

#handle_profile_creation_response(orcid_profile_id) ⇒ Object



100
101
102
103
104
105
# File 'app/models/orcid/profile_request.rb', line 100

def handle_profile_creation_response(orcid_profile_id)
  self.class.transaction do
    update_column(:orcid_profile_id, orcid_profile_id)
    Orcid.connect_user_and_orcid_profile(user, orcid_profile_id)
  end
end

#run(options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/orcid/profile_request.rb', line 21

def run(options = {})
  # Why dependency injection? Because this is going to be a plugin, and
  # things can't possibly be simple. I also found it easier to test the
  # #run method with these injected dependencies
  validator = options.fetch(:validator) { method(:validate_before_run) }
  return false unless validator.call(self)

  payload_xml_builder = options.fetch(:payload_xml_builder) do
    method(:xml_payload)
  end
  profile_creation_service = options.fetch(:profile_creation_service) do
    default_profile_creation_service
  end
  profile_creation_service.call(payload_xml_builder.call(attributes))
end

#validate_before_run(context = self) ⇒ Object



47
48
49
50
# File 'app/models/orcid/profile_request.rb', line 47

def validate_before_run(context = self)
  validate_profile_id_is_unassigned(context) &&
    validate_user_does_not_have_profile(context)
end

#xml_payload(input = attributes) ⇒ Object

NOTE: This one lies ->

http://support.orcid.org/knowledgebase/articles/177522-create-an-id-technical-developer

NOTE: This one was true at 2014-02-06:14:55 ->

http://support.orcid.org/knowledgebase/articles/162412-tutorial-create-a-new-record-using-curl


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/orcid/profile_request.rb', line 76

def xml_payload(input = attributes)
  attrs = input.with_indifferent_access
  returning_value = <<-XML_TEMPLATE
  <?xml version="1.0" encoding="UTF-8"?>
  <orcid-message
  xmlns:xsi="http://www.orcid.org/ns/orcid https://raw.github.com/ORCID/ORCID-Source/master/orcid-model/src/main/resources/orcid-message-1.1.xsd"
  xmlns="http://www.orcid.org/ns/orcid">
  <message-version>1.1</message-version>
  <orcid-profile>
  <orcid-bio>
  <personal-details>
  <given-names>#{attrs.fetch('given_names')}</given-names>
  <family-name>#{attrs.fetch('family_name')}</family-name>
  </personal-details>
  <contact-details>
  <email primary="true">#{attrs.fetch('primary_email')}</email>
  </contact-details>
  </orcid-bio>
  </orcid-profile>
  </orcid-message>
  XML_TEMPLATE
  returning_value.strip
end