class Ldp::Response

Constants

RETURN
TYPE

Attributes

etag[W]
last_modified[W]
response[R]

Public Class Methods

new(response) click to toggle source

@param response [Faraday::Response]

# File lib/ldp/response.rb, line 14
def initialize(response)
  @response = response
end

Public Instance Methods

applied_preferences() click to toggle source
# File lib/ldp/response.rb, line 35
def applied_preferences
  h = {}

  Array(headers['Preference-Applied'.freeze]).map { |x| x.split(",") }.flatten.inject(h) do |memo, header|
    m = header.match(/(?<key>[^=;]*)(=(?<value>[^;,]*))?(;\s*(?<params>[^,]*))?/)
    includes = (m[:params].match(/include="(?<include>[^"]+)"/)[:include] || "").split(" ")
    omits = (m[:params].match(/omit="(?<omit>[^"]+)"/)[:omit] || "").split(" ")
    memo[m[:key]] = { value: m[:value], includes: includes, omits: omits }
  end
end
body() click to toggle source
# File lib/ldp/response.rb, line 100
def body
  response.body
end
container?() click to toggle source

Is the response an LDP container?

# File lib/ldp/response.rb, line 55
def container?
  [
    RDF::Vocab::LDP.BasicContainer,
    RDF::Vocab::LDP.DirectContainer,
    RDF::Vocab::LDP.IndirectContainer
  ].any? { |x| Array(links[TYPE]).include? x.to_s }
end
content_disposition_filename() click to toggle source
# File lib/ldp/response.rb, line 192
def content_disposition_filename
  filename = content_disposition_attributes['filename']
  CGI.unescape(filename) if filename
end
content_length() click to toggle source
# File lib/ldp/response.rb, line 188
def content_length
  headers['Content-Length'].to_i
end
content_type() click to toggle source
# File lib/ldp/response.rb, line 184
def content_type
  headers['Content-Type']
end
dup() click to toggle source
Calls superclass method
# File lib/ldp/response.rb, line 70
def dup
  super.tap do |new_resp|
    unless new_resp.instance_variable_get(:@graph).nil?
      new_resp.remove_instance_variable(:@graph)
    end
  end
end
each_statement(&block) click to toggle source

@deprecated use {#graph} instead

# File lib/ldp/response.rb, line 116
def each_statement(&block)
  reader do |reader|
    reader.each_statement(&block)
  end
end
etag() click to toggle source

Extract the ETag for the resource

# File lib/ldp/response.rb, line 124
def etag
  @etag ||= headers['ETag'.freeze]
end
first_page() click to toggle source

Get the URI to the first page

# File lib/ldp/response.rb, line 176
def first_page
  if links['first']
    RDF::URI.new links['first']
  elsif graph.has_statement? RDf::Statement.new(page_subject, RDF::Vocab::LDP.nextPage, nil)
    subject
  end
end
graph() click to toggle source

Get the graph for the resource (or a blank graph if there is no metadata for the resource)

# File lib/ldp/response.rb, line 106
def graph
  @graph ||= RDF::Graph.new << reader
end
has_next?() click to toggle source

Is there a next page?

# File lib/ldp/response.rb, line 164
def has_next?
  next_page != nil
end
has_page?() click to toggle source

Is the response paginated?

# File lib/ldp/response.rb, line 96
def has_page?
  rdf_source? && graph.has_statement?(RDF::Statement.new(page_subject, RDF.type, RDF::Vocab::LDP.Page))
end
includes?(preference) click to toggle source
# File lib/ldp/response.rb, line 142
def includes? preference
  key = Ldp.send("prefer_#{preference}") if Ldp.respond_to("prefer_#{preference}")
  key ||= preference
  preferences[RETURN][:includes].include?(key) || !preferences["return"][:omits].include?(key)
end
last_modified() click to toggle source

Extract the last modified header for the resource

# File lib/ldp/response.rb, line 130
def last_modified
  @last_modified ||= headers['Last-Modified'.freeze]
end
minimal?() click to toggle source
# File lib/ldp/response.rb, line 148
def minimal?
  preferences[RETURN][:value] == "minimal"
end
next_page() click to toggle source

Get the URI for the next page

# File lib/ldp/response.rb, line 170
def next_page
  graph.first_object [page_subject, RDF::Vocab::LDP.nextPage, nil]
end
page() click to toggle source

Statements about the page

# File lib/ldp/response.rb, line 154
def page
  @page_graph ||= begin
    page_graph = RDF::Graph.new
    page_graph << graph.query([page_subject, nil, nil]) if resource?
    page_graph
  end
end
page_subject() click to toggle source

Get the URI to the response

# File lib/ldp/response.rb, line 90
def page_subject
  @page_subject ||= RDF::URI.new response.env[:url]
end
rdf_source?() click to toggle source

Is the response an LDP RDFSource?

ldp:Container is a subclass of ldp:RDFSource
# File lib/ldp/response.rb, line 66
def rdf_source?
  container? || Array(links[TYPE]).include?(RDF::Vocab::LDP.RDFSource)
end
reader(&block) click to toggle source
# File lib/ldp/response.rb, line 110
def reader(&block)
  reader_for_content_type.new(body, base_uri: page_subject, &block)
end
resource?() click to toggle source

Is the response an LDP resource?

# File lib/ldp/response.rb, line 49
def resource?
  Array(links[TYPE]).include? RDF::Vocab::LDP.Resource.to_s
end
subject() click to toggle source

Get the subject for the response

# File lib/ldp/response.rb, line 80
def subject
  @subject ||= if has_page?
                 graph.first_object [page_subject, RDF::Vocab::LDP.pageOf, nil]
               else
                 page_subject
               end
end
types() click to toggle source

Extract the Link: rel=“type” headers for the resource

# File lib/ldp/response.rb, line 136
def types
  Array(links[TYPE])
end

Private Instance Methods

content_disposition_attributes() click to toggle source
# File lib/ldp/response.rb, line 199
def content_disposition_attributes
  parts = headers['Content-Disposition'].split(/;\s*/).collect { |entry| entry.split(/\s*=\s*/) }
  entries = parts.collect do |part|
    value = part[1].respond_to?(:sub) ? part[1].sub(%r{^"(.*)"$}, '\1') : part[1]
    [part[0], value]
  end
  Hash[entries]
end
headers() click to toggle source
# File lib/ldp/response.rb, line 208
def headers
  response.headers
end
reader_for_content_type() click to toggle source
# File lib/ldp/response.rb, line 212
def reader_for_content_type
  content_type = content_type || 'text/turtle'
  content_type = Array(content_type).first
  RDF::Reader.for(content_type: content_type)
end