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, 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



200
201
202
203
204
205
# File 'lib/rdf/util/file.rb', line 200

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



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

def conn= conn
  @conn = conn
end

.open_url(base_uri, options) ⇒ RemoteDocument, Object

Returns A RemoteDocument.

Parameters:

  • base_uri (String)

    to open

  • options (Hash{Symbol => Object})

Returns:

See Also:

Since:

  • 1.2



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/rdf/util/file.rb', line 212

def self.open_url(base_uri, options)
  response = conn.get do |req|
    req.url base_uri
    headers(options).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
    }

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