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

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

Overview

Responsible for parsing a response document

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collaborators = {}) ⇒ ResponseParser

Returns a new instance of ResponseParser.



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

def initialize(collaborators = {})
  @response_builder = collaborators.fetch(:response_builder) { default_response_builder }
  @logger = collaborators.fetch(:logger) { default_logger }
end

Class Method Details

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

A convenience method to expose entry into the ResponseParser function



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

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

Instance Method Details

#call(document) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/orcid/remote/profile_query_service/response_parser.rb', line 20

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 = extract_identifier(profile)
      label = extract_label(identifier, profile)
      biography = extract_biography(profile)
      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