Module: Krikri::LDP::Resource

Extended by:
ActiveSupport::Concern
Included in:
RdfSource, OriginalRecord
Defined in:
lib/krikri/ldp/resource.rb

Overview

Note:

Ideally, this is a general purpose LDP resource. However some HTTP PUT creation behavior may be specific to Marmotta. This avoids the need to interact directly with the owning container, but is a less generalized implementation.

Implements basic LDP CRUD operations. Requires an implementation of ‘#rdf_subject` returning an `RDF::URI`. The resource idenitified by the URI must conform to LDP Resource’s interaction patterns.

Examples:

implementing a resource

class MyResource
  include Krikri::LDP::Resource

  def rdf_subject
    @rdf_subject ||= RDF::URI('http://example.com/ldp/a/resource/path')
  end
end

See Also:

Instance Method Summary collapse

Instance Method Details

#delete!(headers = {}) ⇒ Object

Sends DELETE request to the resource’s #rdf_subject via #ldp_connection. Headers can be passed in. Default HTTP headers are:

If-Match: "#{etag}" (uses idempotent put if an Entity Tag is cached)


132
133
134
135
136
137
138
# File 'lib/krikri/ldp/resource.rb', line 132

def delete!(headers = {})
  raise "Cannot delete #{rdf_subject}, does not exist." unless exist?
  headers['If-Match'] ||= etag
  response = make_request(:delete, nil, headers)
  @http_headers = nil
  response
end

#etagString

Returns the current cached HTTP ETag for the resource.

Returns:

  • (String)

    the current cached HTTP ETag for the resource



50
51
52
# File 'lib/krikri/ldp/resource.rb', line 50

def etag
  http_head['etag'] if exists?
end

#exists?Boolean Also known as: exist?

Returns true if the LDP server already knows about the resource, otherwise false.

Returns:

  • (Boolean)

    true if the LDP server already knows about the resource, otherwise false.



94
95
96
97
98
99
100
101
102
# File 'lib/krikri/ldp/resource.rb', line 94

def exists?
  return true if http_head
  false
rescue Faraday::ResourceNotFound
  false
rescue Faraday::ClientError => e
  return false if !e.response.nil? && e.response[:status] == 410
  raise e
end

#get(headers = {}, force = false) ⇒ Faraday::Response

Sends a GET request to #rdf_subject and caches the headers and body. Executes lazily unless ‘force` parameter is `true`, using cached values if present.

Parameters:

  • headers (Hash<String, String>) (defaults to: {})

    a hash of HTTP headers; e.g. => ‘text/plain’.

  • force (Boolean) (defaults to: false)

    force request if true

Returns:

  • (Faraday::Response)

    the server’s response

Raises:

  • (Faraday::ClientError)

    if the server responds with an error status. Faraday::ClientError#response contains the full response.



84
85
86
87
88
89
# File 'lib/krikri/ldp/resource.rb', line 84

def get(headers = {}, force = false)
  return @http_cache if @http_cache && !force
  response = make_request(:get, nil, headers)
  @http_headers = response.env['response_headers']
  @http_cache = response
end

#http_head(force = false) ⇒ Hash<String, String>

Sends a HEAD request to #rdf_subject and caches the headers. Executes lazily unless ‘force` parameter is `true`, using cached values if present.

Parameters:

  • force (Boolean) (defaults to: false)

    force request if true

Returns:

  • (Hash<String, String>)

    a hash of HTTP headers

Raises:

  • (Faraday::ClientError)

    if the server responds with an error status. Faraday::ClientError#response contains the full response.



68
69
70
71
# File 'lib/krikri/ldp/resource.rb', line 68

def http_head(force = false)
  return @http_headers if @http_headers && !force
  @http_headers = make_request(:head).env['response_headers']
end

#ldp_connectionFaraday::Connection

Returns a connection to the configured LDP endpoint.

Returns:

  • (Faraday::Connection)

    a connection to the configured LDP endpoint



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/krikri/ldp/resource.rb', line 33

def ldp_connection
  @ldp_conn ||= Faraday.new(ldp_ns) do |conn|
    conn.request :retry, max: 4, interval: 0.025,
                 interval_randomness: 0.5, backoff_factor: 2,
                 exceptions: [Faraday::ConnectionFailed,
                              'Errno::ETIMEDOUT',
                              'Timeout::Error',
                              'Error::TimeoutError',
                              Faraday::TimeoutError]
    conn.use Faraday::Response::RaiseError
    conn.use FaradayMiddleware::FollowRedirects, limit: 3
    conn.adapter Faraday.default_adapter
  end
end

#modified_dateString

Returns the current cached Last-Modified date for the resource.

Returns:

  • (String)

    the current cached Last-Modified date for the resource



56
57
58
# File 'lib/krikri/ldp/resource.rb', line 56

def modified_date
  http_head['last-modified'] if exists?
end

#save(body = nil, headers = {}) ⇒ Faraday::Response

Sends PUT request to the resource’s #rdf_subject via #ldp_connection. A body and headers can be passed in. Default HTTP headers are:

Content-Type: 'text/turtle' (i.e. creates an LDP-RS)
If-Match: "#{etag}" (uses idempotent put if an Entity Tag is cached)

Parameters:

  • body (#to_s) (defaults to: nil)

    the request body.

  • headers (Hash<String, String>) (defaults to: {})

    a hash of HTTP headers; e.g. => ‘text/plain’.

Returns:

  • (Faraday::Response)

    the server’s response

Raises:

  • (Faraday::ClientError)

    if the server responds with an error status. Faraday::ClientError#response contains the full response.



118
119
120
121
122
123
124
# File 'lib/krikri/ldp/resource.rb', line 118

def save(body = nil, headers = {})
  headers['Content-Type'] ||= default_content_type
  headers['If-Match'] ||= etag if exists?
  response = make_request(:put, body, headers)
  @http_headers = response.env['response_headers']
  response
end