Class: Orcid::Remote::ProfileQueryService::ResponseParser

Inherits:
Object
  • Object
show all
Defined in:
app/services/orcid/remote/profile_query_service/response_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collaborators = {}) ⇒ ResponseParser

Returns a new instance of ResponseParser.



14
15
16
17
18
19
20
21
# File 'app/services/orcid/remote/profile_query_service/response_parser.rb', line 14

def initialize(collaborators = {})
  @response_builder = collaborators.fetch(:response_builder) do
    SearchResponse
  end
  @logger = collaborators.fetch(:logger) do
    Rails.logger
  end
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



12
13
14
# File 'app/services/orcid/remote/profile_query_service/response_parser.rb', line 12

def logger
  @logger
end

#response_builderObject (readonly)

Returns the value of attribute response_builder.



12
13
14
# File 'app/services/orcid/remote/profile_query_service/response_parser.rb', line 12

def response_builder
  @response_builder
end

Class Method Details

.call(document, collaborators = {}) ⇒ Object

A convenience method to expose entry into the ResponseParser function



8
9
10
# File 'app/services/orcid/remote/profile_query_service/response_parser.rb', line 8

def self.call(document, collaborators = {})
  new(collaborators).call(document)
end

Instance Method Details

#call(document) ⇒ Object



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
# File 'app/services/orcid/remote/profile_query_service/response_parser.rb', line 23

def call(document)
  json = JSON.parse(document)
  json.fetch('orcid-search-results').fetch('orcid-search-result')
  .each_with_object([]) do |result, returning_value|
    profile = result.fetch('orcid-profile')
    begin
      identifier = profile.fetch('orcid-identifier').fetch('path')
      orcid_bio = profile.fetch('orcid-bio')
      given_names = orcid_bio.fetch('personal-details').fetch('given-names').fetch('value')
      family_name = orcid_bio.fetch('personal-details').fetch('family-name').fetch('value')
      emails = []
      contact_details = orcid_bio['contact-details']
      if contact_details
        emails = (contact_details['email'] || []).map {|email| email.fetch('value') }
      end
      label = "#{given_names} #{family_name}"
      label << ' (' << emails.join(', ') << ')' if emails.any?
      label << " [ORCID: #{identifier}]"
      biography = ''
      biography = orcid_bio['biography']['value'] if orcid_bio['biography']
      returning_value << response_builder.new('id' => identifier, 'label' => label, 'biography' => biography)
    rescue KeyError => e
      logger.warn("Unexpected ORCID JSON Response, part of the response has been ignored.\tException Encountered:#{e.class}\t#{e}")
    end
    returning_value
  end
end