Module: Ldp::Response
- Defined in:
- lib/ldp/response.rb
Defined Under Namespace
Modules: Paging
Class Method Summary collapse
- .applied_preferences(headers) ⇒ Object
-
.container?(response) ⇒ Boolean
Is the response an LDP container?.
-
.links(response) ⇒ Object
Extract the Link: headers from the HTTP resource.
-
.rdf_source?(response) ⇒ Boolean
Is the response an LDP RDFSource? ldp:Container is a subclass of ldp:RDFSource.
-
.resource?(response) ⇒ Boolean
Is the response an LDP resource?.
-
.wrap(client, raw_resp) ⇒ Object
Wrap the raw Faraday respone with our LDP extensions.
Instance Method Summary collapse
-
#container? ⇒ Boolean
Is the response an LDP container.
-
#etag ⇒ Object
Extract the ETag for the resource.
- #etag=(val) ⇒ Object
-
#graph ⇒ Object
Get the graph for the resource (or a blank graph if there is no metadata for the resource).
-
#has_page? ⇒ Boolean
Is the response paginated?.
- #includes?(preference) ⇒ Boolean
-
#last_modified ⇒ Object
Extract the last modified header for the resource.
- #last_modified=(val) ⇒ Object
-
#links ⇒ Object
Link: headers from the HTTP response.
- #minimal? ⇒ Boolean
-
#page_subject ⇒ Object
Get the URI to the response.
- #preferences ⇒ Object
-
#rdf_source? ⇒ Boolean
Is the response an LDP rdf source?.
-
#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.
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?
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 |
.links(response) ⇒ Object
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
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 |
Instance Method Details
#container? ⇒ Boolean
Is the response an LDP container
83 84 85 |
# File 'lib/ldp/response.rb', line 83 def container? Ldp::Response.container?(self) end |
#etag ⇒ Object
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 |
#graph ⇒ Object
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?
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
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_modified ⇒ Object
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 |
#links ⇒ Object
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
159 160 161 |
# File 'lib/ldp/response.rb', line 159 def minimal? preferences["return"][:value] == "minimal" end |
#page_subject ⇒ Object
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 |
#preferences ⇒ Object
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?
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?
71 72 73 |
# File 'lib/ldp/response.rb', line 71 def resource? Ldp::Response.resource?(self) end |
#subject ⇒ Object
Get the subject for the response
92 93 94 |
# File 'lib/ldp/response.rb', line 92 def subject page_subject end |
#types ⇒ Object
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 |