Class: Krikri::OriginalRecord

Inherits:
Object
  • Object
show all
Includes:
LDP::Resource
Defined in:
app/models/krikri/original_record.rb

Overview

Handles records as harvested, prior to mapping

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LDP::Resource

#delete!, #etag, #exists?, #get, #http_head, #ldp_connection, #modified_date

Constructor Details

#initialize(identifier) ⇒ OriginalRecord

Note:

calling OriginalRecord.new will instantiate an empty object. Use .load and .build to populate the object on instantiation.

Instantiate an OriginalRecord object with a #local_name matching the argument.

Raises:

  • (ArgumentError)

19
20
21
22
23
# File 'app/models/krikri/original_record.rb', line 19

def initialize(identifier)
  raise ArgumentError, "#{identifier} is an invalid local name" if
    identifier.include?('/')
  @local_name = identifier
end

Instance Attribute Details

#contentObject

Returns the value of attribute content


7
8
9
# File 'app/models/krikri/original_record.rb', line 7

def content
  @content
end

#content_typeObject


120
121
122
# File 'app/models/krikri/original_record.rb', line 120

def content_type
  @content_type || 'text/xml'
end

#local_nameObject

Returns the value of attribute local_name


7
8
9
# File 'app/models/krikri/original_record.rb', line 7

def local_name
  @local_name
end

#rdf_subjectObject

Returns the value of attribute rdf_subject


7
8
9
# File 'app/models/krikri/original_record.rb', line 7

def rdf_subject
  @rdf_subject
end

Class Method Details

.base_uriObject


83
84
85
# File 'app/models/krikri/original_record.rb', line 83

def base_uri
  Krikri::Settings['marmotta']['record_container']
end

.build(identifier, content, content_type = nil) ⇒ OriginalRecord

Note:

Marmotta interprets some content types universally as LDP-RDFSources. Take care when passing new content types through to Marmotta; you may get unexpected errors from the server.

Instantiate and populate an OriginalRecord Resource (new or existing) with the specified content and content type.

Raises:

  • when no matching record is found in the LDP datastore


71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/krikri/original_record.rb', line 71

def build(identifier, content, content_type = nil)
  raise(ArgumentError,
        '`content` must be a readable IO object or String.'\
        "Got a #{content.class}") unless
    content.is_a?(String) || content.respond_to?(:read)
  record = new(identifier)
  record.reload if record.exists?
  record.content = content
  record.content_type = content_type
  record
end

.build_uri(local_name) ⇒ Object


87
88
89
# File 'app/models/krikri/original_record.rb', line 87

def build_uri(local_name)
  RDF::URI(base_uri) / local_name
end

.load(identifier) ⇒ OriginalRecord

Instantiate and populate an existing OriginalRecord Resource.

Raises:

  • when no matching record is found in the LDP datastore


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/krikri/original_record.rb', line 34

def load(identifier)
  identifier = identifier.to_s.split('/').last if
    identifier.start_with? base_uri

  if identifier.include?('.')
    record = new(identifier.split('.').first)
  else
    record = new(identifier)
  end

  raise "No #{self} found with id: #{identifier}" unless record.exists?

  if identifier.include?('.')
    record.rdf_subject = "#{base_uri}/#{identifier}"
  else
    record.rdf_subject = nr_uri_from_headers(record.http_head)
  end

  record.reload
end

.nr_uri_from_headers(headers) ⇒ Object

TODO:

figure out how to handle situations where more than one NR is described by the same RDFSource (second file PUT to same URI)

Gets the URI for the ldp:NonRDFSource from the Headers returned by the containing ldp:RDFSource.


98
99
100
101
102
103
104
# File 'app/models/krikri/original_record.rb', line 98

def nr_uri_from_headers(headers)
  links = headers['link'].split(',').select! do |link|
    link.include? 'rel="content"'
  end

  links.first[/.*<(.*)>/, 1]
end

Instance Method Details

#==(other) ⇒ Object


107
108
109
110
111
112
113
114
# File 'app/models/krikri/original_record.rb', line 107

def ==(other)
  return false unless other.is_a? OriginalRecord
  return false unless local_name == other.local_name
  return false unless content == other.content
  return false unless content_type == other.content_type
  return false unless etag == other.etag
  true
end

#rdf_sourceRDF::URI

Returns the URI for the managing RDFSource created for the record


128
129
130
131
# File 'app/models/krikri/original_record.rb', line 128

def rdf_source
  @rdf_source ||=
    .new(self.class.build_uri(local_name))
end

#reloadOriginalRecord

Reloads the record from its LDP URI, updates #content to the response body

Raises:

  • (Faraday::ClientError)

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


156
157
158
159
160
161
162
# File 'app/models/krikri/original_record.rb', line 156

def reload
  @rdf_subject ||= self.class.nr_uri_from_headers(http_head)
  response = get(nil, true)
  self.content_type = response.env.response_headers['content-type']
  self.content = response.env.body
  self
end

#save(activity_uri = nil) ⇒ Boolean

Saves over LDP, passing #content and #headers to the request.

Raises:

  • (Faraday::ClientError)

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

See Also:


141
142
143
144
145
146
147
148
149
# File 'app/models/krikri/original_record.rb', line 141

def save(activity_uri = nil)
  response = super(@content, headers)
  @rdf_subject ||= response.env.response_headers['location']
  http_head(true)
  return response unless activity_uri
  rdf_source.wasGeneratedBy = activity_uri
  rdf_source.save
  response
end

#to_sObject


116
117
118
# File 'app/models/krikri/original_record.rb', line 116

def to_s
  content
end