Class: HarvesterTools::WebUtils

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

Class Method Summary collapse

Class Method Details

.fspfetch(url:, headers: ACCEPT_STAR_HEADER, method: :get, meta: HarvesterTools::MetadataObject.new) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
# File 'lib/web_utils.rb', line 4

def self.fspfetch(url:, headers: ACCEPT_STAR_HEADER, method: :get, meta: HarvesterTools::MetadataObject.new)
  warn 'In fetch routine now.  '

  begin
    warn "executing call over the Web to #{url}"
    response = RestClient::Request.execute({
                                            method: method,
                                            url: url.to_s,
                                            # user: user,
                                            # password: pass,
                                            headers: headers
                                          })
    meta.all_uris |= [response.request.url]  # it's possible to call this method without affecting the metadata object being created by the harvester
    warn "starting URL #{url}"
    warn "final URL #{response.request.url}"
    warn "Response code #{response.code}"
    if response.code == 203 
      meta.add_warning(["002", url, headers])
      meta.comments << "WARN: Response is non-authoritative (HTTP response code: #{response.code}).  Headers may have been manipulated encountered when trying to resolve #{url}\n"
    end
    response
  rescue RestClient::ExceptionWithResponse => e
    warn "EXCEPTION WITH RESPONSE! #{e.response.code} with response #{e.response}\nfailed response headers: #{e.response.headers}"
    meta.add_warning(["003", url, headers])
    meta.comments << "WARN: HTTP error #{e} encountered when trying to resolve #{url}\n"
    if (e.response.code == 500 or e.response.code == 404)
      return false
    else
      e.response
    end
    # now we are returning the headers and body that were returned
  rescue RestClient::Exception => e
    warn "EXCEPTION WITH NO RESPONSE! #{e}"
    meta.add_warning(["003", url, headers])
    meta.comments << "WARN: HTTP error #{e} encountered when trying to resolve #{url}\n"
    false
    # now we are returning 'False', and we will check that with an \"if\" statement in our main code
  rescue Exception => e
    warn "EXCEPTION UNKNOWN! #{e}"
    meta.add_warning(["003", url, headers])
    meta.comments << "WARN: HTTP error #{e} encountered when trying to resolve #{url}\n"
    false
    # now we are returning 'False', and we will check that with an \"if\" statement in our main code
  end
end