Class: RDF::Util::File::RestClientAdapter

Inherits:
HttpAdapter show all
Defined in:
vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/util/file.rb

Overview

If the Rest Client gem is included, it will be used for retrieving resources allowing for sophisticated HTTP caching using REST Client Components allowing the use of Rack::Cache to avoid network access.

Since:

  • 1.2

Class Method Summary collapse

Methods inherited from HttpAdapter

default_accept_header, headers

Class Method Details

.open_url(base_uri, proxy: nil, headers: {}, verify_none: false, **options) ⇒ RemoteDocument, Object

This method is abstract.

Returns A RDF::Util::File::RemoteDocument. If a block is given, the result of evaluating the block is returned.

Parameters:

  • base_uri (String)

    to open

  • proxy (String)

    HTTP Proxy to use for requests.

  • headers (Array, String)

    ({}) HTTP Request headers

  • verify_none (Boolean)

    (false) Don't verify SSL certificates

  • options (Hash{Symbol => Object})

    options are ignored in this implementation. Applications are encouraged to override this implementation to provide more control over HTTP headers and redirect following.

Returns:

Raises:

  • (IOError)

    if not found

Since:

  • 1.2



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/util/file.rb', line 73

def self.open_url(base_uri, proxy: nil, headers: {}, verify_none: false, **options)
  ssl_verify = verify_none ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER

  # If RestClient is loaded, prefer it
  RestClient.proxy = proxy.to_s if proxy
  client = RestClient::Resource.new(base_uri, verify_ssl: ssl_verify)
  client.get(headers(headers: headers)) do |response, request, res, &blk|
    case response.code
    when 200..299
      # found object

      # If a Location is returned, it defines the base resource for this file, not it's actual ending location
      document_options = {
        base_uri:     RDF::URI(response.headers.fetch(:location, base_uri)),
        code:         response.code.to_i,
        headers:      response.headers
      }

      remote_document = RemoteDocument.new(response.body, document_options)
    when 300..399
      # Document base is redirected location
      # Location may be relative
      base_uri = ::URI.join(base_uri, response.headers[:location].to_s).to_s
      response.follow_redirection(&blk)
    else
      raise IOError, "<#{base_uri}>: #{response.code}"
    end
  end
end