Module: Krikri::LDP::Resource

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

Overview

Implements basic LDP CRUD operations

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)

111
112
113
114
115
116
117
# File 'lib/krikri/ldp/resource.rb', line 111

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


29
30
31
# File 'lib/krikri/ldp/resource.rb', line 29

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.


73
74
75
76
77
78
79
80
81
# File 'lib/krikri/ldp/resource.rb', line 73

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.


63
64
65
66
67
68
# File 'lib/krikri/ldp/resource.rb', line 63

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.


47
48
49
50
# File 'lib/krikri/ldp/resource.rb', line 47

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


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/krikri/ldp/resource.rb', line 12

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


35
36
37
# File 'lib/krikri/ldp/resource.rb', line 35

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.


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

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