Class: Ldp::Response
- Inherits:
-
Object
- Object
- Ldp::Response
- Extended by:
- Forwardable
- Defined in:
- lib/ldp/response.rb
Constant Summary collapse
- TYPE =
'type'.freeze
- RETURN =
'return'.freeze
Instance Attribute Summary collapse
-
#etag ⇒ Object
Extract the ETag for the resource.
-
#last_modified ⇒ Object
Extract the last modified header for the resource.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #applied_preferences ⇒ Object
- #body ⇒ Object
-
#container? ⇒ Boolean
Is the response an LDP container?.
- #content_disposition_filename ⇒ Object
- #content_length ⇒ Object
- #content_type ⇒ Object
- #dup ⇒ Object
-
#each_statement(&block) ⇒ Object
deprecated
Deprecated.
use #graph instead
-
#first_page ⇒ Object
Get the URI to the first page.
-
#graph ⇒ Object
Get the graph for the resource (or a blank graph if there is no metadata for the resource).
-
#has_next? ⇒ Boolean
Is there a next page?.
-
#has_page? ⇒ Boolean
Is the response paginated?.
- #includes?(preference) ⇒ Boolean
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
-
#links ⇒ Object
Extract the Link: headers from the HTTP resource.
- #minimal? ⇒ Boolean
-
#next_page ⇒ Object
Get the URI for the next page.
-
#page ⇒ Object
Statements about the page.
-
#page_subject ⇒ Object
Get the URI to the response.
-
#rdf_source? ⇒ Boolean
Is the response an LDP RDFSource? ldp:Container is a subclass of ldp:RDFSource.
- #reader(&block) ⇒ Object
-
#resource? ⇒ Boolean
Is the response an LDP resource?.
-
#subject ⇒ Object
Get the subject for the response.
-
#types ⇒ Object
Extract the Link: rel=“type” headers for the resource.
Constructor Details
#initialize(response) ⇒ Response
Returns a new instance of Response.
14 15 16 |
# File 'lib/ldp/response.rb', line 14 def initialize(response) @response = response end |
Instance Attribute Details
#etag ⇒ Object
Extract the ETag for the resource
124 125 126 |
# File 'lib/ldp/response.rb', line 124 def etag @etag ||= headers['ETag'.freeze] end |
#last_modified ⇒ Object
Extract the last modified header for the resource
130 131 132 |
# File 'lib/ldp/response.rb', line 130 def last_modified @last_modified ||= headers['Last-Modified'.freeze] end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
9 10 11 |
# File 'lib/ldp/response.rb', line 9 def response @response end |
Instance Method Details
#applied_preferences ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# 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 ⇒ Object
100 101 102 |
# File 'lib/ldp/response.rb', line 100 def body response.body end |
#container? ⇒ Boolean
Is the response an LDP container?
55 56 57 58 59 60 61 |
# 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 ⇒ Object
192 193 194 195 |
# File 'lib/ldp/response.rb', line 192 def content_disposition_filename filename = content_disposition_attributes['filename'] CGI.unescape(filename) if filename end |
#content_length ⇒ Object
188 189 190 |
# File 'lib/ldp/response.rb', line 188 def content_length headers['Content-Length'].to_i end |
#content_type ⇒ Object
184 185 186 |
# File 'lib/ldp/response.rb', line 184 def content_type headers['Content-Type'] end |
#dup ⇒ Object
70 71 72 73 74 75 76 |
# 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) ⇒ Object
use #graph instead
116 117 118 119 120 |
# File 'lib/ldp/response.rb', line 116 def each_statement(&block) reader do |reader| reader.each_statement(&block) end end |
#first_page ⇒ Object
Get the URI to the first page
176 177 178 179 180 181 182 |
# 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 ⇒ Object
Get the graph for the resource (or a blank graph if there is no metadata for the resource)
106 107 108 |
# File 'lib/ldp/response.rb', line 106 def graph @graph ||= RDF::Graph.new << reader end |
#has_next? ⇒ Boolean
Is there a next page?
164 165 166 |
# File 'lib/ldp/response.rb', line 164 def has_next? next_page != nil end |
#has_page? ⇒ Boolean
Is the response paginated?
96 97 98 |
# 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) ⇒ Boolean
142 143 144 145 146 |
# 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 |
#links ⇒ Object
Extract the Link: headers from the HTTP resource
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ldp/response.rb', line 20 def links @links ||= begin h = {} Array(headers['Link'.freeze]).map { |x| x.split(','.freeze) }.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 end |
#minimal? ⇒ Boolean
148 149 150 |
# File 'lib/ldp/response.rb', line 148 def minimal? preferences[RETURN][:value] == "minimal" end |
#next_page ⇒ Object
Get the URI for the next page
170 171 172 |
# File 'lib/ldp/response.rb', line 170 def next_page graph.first_object [page_subject, RDF::Vocab::LDP.nextPage, nil] end |
#page ⇒ Object
Statements about the page
154 155 156 157 158 159 160 |
# 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 ⇒ Object
Get the URI to the response
90 91 92 |
# File 'lib/ldp/response.rb', line 90 def page_subject @page_subject ||= RDF::URI.new response.env[:url] end |
#rdf_source? ⇒ Boolean
Is the response an LDP RDFSource?
ldp:Container is a subclass of ldp:RDFSource
66 67 68 |
# File 'lib/ldp/response.rb', line 66 def rdf_source? container? || Array(links[TYPE]).include?(RDF::Vocab::LDP.RDFSource) end |
#reader(&block) ⇒ Object
110 111 112 |
# File 'lib/ldp/response.rb', line 110 def reader(&block) reader_for_content_type.new(body, base_uri: page_subject, &block) end |
#resource? ⇒ Boolean
Is the response an LDP resource?
49 50 51 |
# File 'lib/ldp/response.rb', line 49 def resource? Array(links[TYPE]).include? RDF::Vocab::LDP.Resource.to_s end |
#subject ⇒ Object
Get the subject for the response
80 81 82 83 84 85 86 |
# 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 ⇒ Object
Extract the Link: rel=“type” headers for the resource
136 137 138 |
# File 'lib/ldp/response.rb', line 136 def types Array(links[TYPE]) end |