Class: RDF::Util::File::FaradayAdapter

Inherits:
HttpAdapter show all
Defined in:
lib/rdf/util/file.rb

Overview

Use Faraday for retrieving resources

Since:

  • 1.2

Class Method Summary collapse

Methods inherited from HttpAdapter

default_accept_header, default_user_agent, headers

Class Method Details

.connObject

Get the Faraday::Connection to use for retrieving RDF resources, or a default connect that follows redirects.

Since:

  • 1.2



190
191
192
193
194
195
# File 'lib/rdf/util/file.rb', line 190

def conn
  @conn ||= Faraday.new do |conn|
    conn.use FaradayMiddleware::FollowRedirects
    conn.adapter Faraday.default_adapter
  end
end

.conn=(conn) ⇒ Object

Set the Faraday::Connection to use for retrieving RDF resources

Since:

  • 1.2



183
184
185
# File 'lib/rdf/util/file.rb', line 183

def conn= conn
  @conn = conn
end

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

This method is abstract.

Returns A RemoteDocument. If a block is given, the result of evaluating the block is returned.

Parameters:

  • base_uri (String)

    to open

  • proxy (String) (defaults to: nil)

    HTTP Proxy to use for requests.

  • headers (Array, String) (defaults to: {})

    ({}) HTTP Request headers

    Defaults Accept header based on available reader content types to allow for content negotiation based on available readers.

    Defaults User-Agent header, unless one is specified.

  • verify_none (Boolean) (defaults to: false)

    (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



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/rdf/util/file.rb', line 199

def self.open_url(base_uri, proxy: nil, headers: {}, verify_none: false, **options)
  response = conn.get do |req|
    req.url base_uri
    headers(headers: headers).each do |k,v|
      req.headers[k] = v
    end
  end

  case response.status
  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, response.env.url)),
      code:         response.status,
      headers:      response.headers
    }

    RemoteDocument.new(response.body, document_options)
  else
    raise IOError, "<#{base_uri}>: #{response.status}"
  end
end