Module: Ldp::Response
- Defined in:
- lib/ldp/response.rb
Defined Under Namespace
Modules: Paging
Constant Summary collapse
- TYPE =
'type'.freeze
- RETURN =
'return'.freeze
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.
- #dup ⇒ Object
-
#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
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ldp/response.rb', line 29 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?
49 50 51 52 53 54 55 |
# File 'lib/ldp/response.rb', line 49 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
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ldp/response.rb', line 16 def self.links response h = {} Array(response.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 |
.rdf_source?(response) ⇒ Boolean
Is the response an LDP RDFSource?
ldp:Container is a subclass of ldp:RDFSource
60 61 62 |
# File 'lib/ldp/response.rb', line 60 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
97 98 99 |
# File 'lib/ldp/response.rb', line 97 def container? Ldp::Response.container?(self) end |
#dup ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ldp/response.rb', line 64 def dup super.tap do |new_resp| new_resp.send(:extend, Ldp::Response) unless new_resp.instance_variable_get(:@graph).nil? if ::RUBY_VERSION < '2.0' new_resp.send(:remove_instance_variable, :@graph) else new_resp.remove_instance_variable(:@graph) end end end end |
#etag ⇒ Object
Extract the ETag for the resource
143 144 145 |
# File 'lib/ldp/response.rb', line 143 def etag @etag ||= headers['ETag'.freeze] end |
#etag=(val) ⇒ Object
147 148 149 |
# File 'lib/ldp/response.rb', line 147 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)
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ldp/response.rb', line 124 def graph @graph ||= begin raise UnexpectedContentType, "The resource at #{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?
118 119 120 |
# File 'lib/ldp/response.rb', line 118 def has_page? rdf_source? && graph.has_statement?(RDF::Statement.new(page_subject, RDF.type, Ldp.page)) end |
#includes?(preference) ⇒ Boolean
169 170 171 172 173 |
# File 'lib/ldp/response.rb', line 169 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
153 154 155 |
# File 'lib/ldp/response.rb', line 153 def last_modified @last_modified ||= headers['Last-Modified'.freeze] end |
#last_modified=(val) ⇒ Object
157 158 159 |
# File 'lib/ldp/response.rb', line 157 def last_modified=(val) @last_modified = val end |
#links ⇒ Object
Link: headers from the HTTP response
79 80 81 |
# File 'lib/ldp/response.rb', line 79 def links @links ||= Ldp::Response.links(self) end |
#minimal? ⇒ Boolean
175 176 177 |
# File 'lib/ldp/response.rb', line 175 def minimal? preferences[RETURN][:value] == "minimal" end |
#page_subject ⇒ Object
Get the URI to the response
112 113 114 |
# File 'lib/ldp/response.rb', line 112 def page_subject @page_subject ||= RDF::URI.new env[:url] end |
#preferences ⇒ Object
101 102 103 |
# File 'lib/ldp/response.rb', line 101 def preferences Ldp::Resource.applied_preferences(headers['Preference-Applied'.freeze]) end |
#rdf_source? ⇒ Boolean
Is the response an LDP rdf source?
91 92 93 |
# File 'lib/ldp/response.rb', line 91 def rdf_source? Ldp::Response.rdf_source?(self) end |
#resource? ⇒ Boolean
Is the response an LDP resource?
85 86 87 |
# File 'lib/ldp/response.rb', line 85 def resource? Ldp::Response.resource?(self) end |
#subject ⇒ Object
Get the subject for the response
106 107 108 |
# File 'lib/ldp/response.rb', line 106 def subject page_subject end |
#types ⇒ Object
Extract the Link: rel=“type” headers for the resource
163 164 165 |
# File 'lib/ldp/response.rb', line 163 def types Array(links[TYPE]) end |