Class: HarvesterTools::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/harvester_utils.rb

Class Method Summary collapse

Class Method Details

.convertToURL(guid:) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/harvester_utils.rb', line 22

def self.convertToURL(guid:)
  FspHarvester::GUID_TYPES.each do |k, regex|
    if k == 'inchi' and regex.match(guid)
      return 'inchi', "https://pubchem.ncbi.nlm.nih.gov/rest/rdf/inchikey/#{guid}"
    elsif k == 'handle1' and regex.match(guid)
      return 'handle', "http://hdl.handle.net/#{guid}"
    elsif k == 'handle2' and regex.match(guid)
      return 'handle', "http://hdl.handle.net/#{guid}"
    elsif k == 'uri' and regex.match(guid)
      return 'uri', guid
    elsif k == 'doi' and regex.match(guid)
      return 'doi', "https://doi.org/#{guid}"
    elsif k == 'ark' and regex.match(guid)
      return 'ark', "https://n2t.net/#{guid}"
    end
  end
  [nil, nil]
end


68
69
70
71
72
73
74
75
76
# File 'lib/harvester_utils.rb', line 68

def self.process_link_headers(response:, metadata:)
  warn "\n\n parsing #{response.headers}\n\n"
  
  parser = LinkHeaders::Processor.new(default_anchor: .all_uris.last)
  parser.extract_and_parse(response: response)
  factory = parser.factory # LinkHeaders::LinkFactory
  FspHarvester::Utils.signpostingcheck(factory: factory, metadata: )
  factory.all_links
end

.resolve_guid(guid:, metadata: HarvesterTools::MetadataObject.new) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/harvester_utils.rb', line 7

def self.resolve_guid(guid:, metadata: HarvesterTools::MetadataObject.new)
  @meta = 
  @meta.all_uris = [guid]
  type, url = convertToURL(guid: guid)
  links = Array.new
  if type
    links = resolve_url(url: url, metadata: @meta)
    @meta.links = @meta.links.append(*links)
  else
    @meta.add_warning(['006', guid, ''])
    @meta.comments << "FATAL: GUID type not recognized.\n"
  end
  [links, @meta]
end

.resolve_url(url:, method: :get, nolinkheaders: false, metadata:, header: FspHarvester::ACCEPT_STAR_HEADER) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/harvester_utils.rb', line 48

def self.resolve_url(url:, method: :get, nolinkheaders: false, metadata:, header: FspHarvester::ACCEPT_STAR_HEADER)
  @meta = 
  @meta.guidtype = 'uri' if @meta.guidtype.nil?
  warn "\n\n FETCHING #{url} #{header}\n\n"
  response = HarvesterTools::WebUtils.fspfetch(url: url, headers: header, method: method, meta: @meta)
  warn "\n\n head #{response.headers.inspect}\n\n" if response

  unless response
    @meta.add_warning(['001', url, header])
    @meta.comments << "WARN: Unable to resolve #{url} using HTTP Accept header #{header}.\n"
    return []
  end

  @meta.comments << "INFO: following redirection using this header led to the following URL: #{@meta.all_uris.last}.  Using the output from this URL for the next few tests..."
  @meta.full_response << [url, response.body]

  links = process_link_headers(response: response, metadata: @meta) unless nolinkheaders
  links
end

.typeit(guid:) ⇒ Object



41
42
43
44
45
46
# File 'lib/harvester_utils.rb', line 41

def self.typeit(guid:)
  FspHarvester::GUID_TYPES.each do |type, regex|
    return type if regex.match(guid)
  end
  false
end