Module: Ldp::Response

Defined in:
lib/ldp/response.rb

Defined Under Namespace

Modules: Paging

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.applied_preferences(headers) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/ldp/response.rb', line 28

def self.applied_preferences headers
  h = {}
  
  Array(headers).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

.container?(response) ⇒ Boolean

Is the response an LDP container?

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/ldp/response.rb', line 48

def self.container? response
  [
    Ldp.basic_container,
    Ldp.direct_container,
    Ldp.indirect_container
  ].any? { |x| Array(links(response)["type"]).include? x.to_s }
end

Extract the Link: headers from the HTTP resource



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ldp/response.rb', line 15

def self.links response
  h = {}
  Array(response.headers["Link"]).map { |x| x.split(", ") }.flatten.inject(h) do |memo, header|
    m = header.match(/<(?<link>.*)>;\s?rel="(?<rel>[^"]+)"/)
    if m
      memo[m[:rel]] ||= []
      memo[m[:rel]] << m[:link]
    end

    memo
  end
end

.rdf_source?(response) ⇒ Boolean

Is the response an LDP RDFSource?

ldp:Container is a subclass of ldp:RDFSource

Returns:

  • (Boolean)


59
60
61
# File 'lib/ldp/response.rb', line 59

def self.rdf_source? response
  container?(response) || Array(links(response)["type"]).include?(Ldp.rdf_source)
end

.resource?(response) ⇒ Boolean

Is the response an LDP resource?

Returns:

  • (Boolean)


42
43
44
# File 'lib/ldp/response.rb', line 42

def self.resource? response
  Array(links(response)["type"]).include? Ldp.resource.to_s
end

.wrap(client, raw_resp) ⇒ Object

Wrap the raw Faraday respone with our LDP extensions



7
8
9
10
11
# File 'lib/ldp/response.rb', line 7

def self.wrap client, raw_resp
  raw_resp.send(:extend, Ldp::Response)
  raw_resp.send(:extend, Ldp::Response::Paging) if raw_resp.has_page?
  raw_resp
end

Instance Method Details

#container?Boolean

Is the response an LDP container

Returns:

  • (Boolean)


83
84
85
# File 'lib/ldp/response.rb', line 83

def container?
  Ldp::Response.container?(self)
end

#etagObject

Extract the ETag for the resource



129
130
131
# File 'lib/ldp/response.rb', line 129

def etag
  @etag ||= headers['ETag']
end

#etag=(val) ⇒ Object



133
134
135
# File 'lib/ldp/response.rb', line 133

def etag=(val)
  @etag = val
end

#graphObject

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ldp/response.rb', line 110

def graph
  @graph ||= begin
    raise UnexpectedContentType, "The resource #{page_subject} is not an RDFSource" unless rdf_source?
    graph = RDF::Graph.new

    if resource?
      RDF::Reader.for(:ttl).new(StringIO.new(body), :base_uri => page_subject) do |reader|
        reader.each_statement do |s|
          graph << s
        end
      end
    end

    graph
  end
end

#has_page?Boolean

Is the response paginated?

Returns:

  • (Boolean)


104
105
106
# File 'lib/ldp/response.rb', line 104

def has_page?
  rdf_source? && graph.has_statement?(RDF::Statement.new(page_subject, RDF.type, Ldp.page))
end

#includes?(preference) ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
156
157
# File 'lib/ldp/response.rb', line 153

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_modifiedObject

Extract the last modified header for the resource



139
140
141
# File 'lib/ldp/response.rb', line 139

def last_modified
  @last_modified ||= headers['Last-Modified']
end

#last_modified=(val) ⇒ Object



143
144
145
# File 'lib/ldp/response.rb', line 143

def last_modified=(val)
  @last_modified = val
end

Link: headers from the HTTP response



65
66
67
# File 'lib/ldp/response.rb', line 65

def links
  @links ||= Ldp::Response.links(self)
end

#minimal?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/ldp/response.rb', line 159

def minimal?
  preferences["return"][:value] == "minimal"
end

#page_subjectObject

Get the URI to the response



98
99
100
# File 'lib/ldp/response.rb', line 98

def page_subject
  @page_subject ||= RDF::URI.new env[:url]
end

#preferencesObject



87
88
89
# File 'lib/ldp/response.rb', line 87

def preferences
  Ldp::Resource.applied_preferences(headers["Preference-Applied"])
end

#rdf_source?Boolean

Is the response an LDP rdf source?

Returns:

  • (Boolean)


77
78
79
# File 'lib/ldp/response.rb', line 77

def rdf_source?
  Ldp::Response.rdf_source?(self)
end

#resource?Boolean

Is the response an LDP resource?

Returns:

  • (Boolean)


71
72
73
# File 'lib/ldp/response.rb', line 71

def resource?
  Ldp::Response.resource?(self)
end

#subjectObject

Get the subject for the response



92
93
94
# File 'lib/ldp/response.rb', line 92

def subject
  page_subject
end

#typesObject

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



149
150
151
# File 'lib/ldp/response.rb', line 149

def types
  Array(links["type"])
end