Class: Ldp::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ldp/response.rb

Constant Summary collapse

TYPE =
'type'.freeze
RETURN =
'return'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



13
14
15
# File 'lib/ldp/response.rb', line 13

def initialize(response)
  @response = response
end

Instance Attribute Details

#etagObject

Extract the ETag for the resource



125
126
127
# File 'lib/ldp/response.rb', line 125

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

#last_modifiedObject

Extract the last modified header for the resource



131
132
133
# File 'lib/ldp/response.rb', line 131

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

#responseObject (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_preferencesObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/ldp/response.rb', line 34

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

#bodyObject



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

def body
  response.body
end

#container?Boolean

Is the response an LDP container?

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/ldp/response.rb', line 54

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_filenameObject



201
202
203
204
# File 'lib/ldp/response.rb', line 201

def content_disposition_filename
  filename = content_disposition_attributes['filename']
  URI.decode(filename) if filename
end

#content_lengthObject



197
198
199
# File 'lib/ldp/response.rb', line 197

def content_length
  headers['Content-Length'].to_i
end

#content_typeObject



193
194
195
# File 'lib/ldp/response.rb', line 193

def content_type
  headers['Content-Type']
end

#dupObject



69
70
71
72
73
74
75
# File 'lib/ldp/response.rb', line 69

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



117
118
119
120
121
# File 'lib/ldp/response.rb', line 117

def each_statement(&block)
  reader do |reader|
    reader.each_statement(&block)
  end
end

#first_pageObject

Get the URI to the first page



185
186
187
188
189
190
191
# File 'lib/ldp/response.rb', line 185

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

#graphObject

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



105
106
107
108
109
110
111
# File 'lib/ldp/response.rb', line 105

def graph
  @graph ||= begin
    graph = RDF::Graph.new
    each_statement { |s| graph << s }
    graph
  end
end

#has_next?Boolean

Is there a next page?

Returns:

  • (Boolean)


173
174
175
# File 'lib/ldp/response.rb', line 173

def has_next?
  next_page != nil
end

#has_page?Boolean

Is the response paginated?

Returns:

  • (Boolean)


95
96
97
# File 'lib/ldp/response.rb', line 95

def has_page?
  rdf_source? && graph.has_statement?(RDF::Statement.new(page_subject, RDF.type, RDF::Vocab::LDP.Page))
end

#includes?(preference) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Extract the Link: headers from the HTTP resource



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ldp/response.rb', line 19

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

Returns:

  • (Boolean)


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

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

#next_pageObject

Get the URI for the next page



179
180
181
# File 'lib/ldp/response.rb', line 179

def next_page
  graph.first_object [page_subject, RDF::Vocab::LDP.nextPage, nil]
end

#pageObject

Statements about the page



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ldp/response.rb', line 155

def page
  @page_graph ||= begin
    g = RDF::Graph.new

    if resource?
      res = graph.query RDF::Statement.new(page_subject, nil, nil)

      res.each_statement do |s|
        g << s
      end
    end

    g
  end
end

#page_subjectObject

Get the URI to the response



89
90
91
# File 'lib/ldp/response.rb', line 89

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

Returns:

  • (Boolean)


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

def rdf_source?
  container? || Array(links[TYPE]).include?(RDF::Vocab::LDP.RDFSource)
end

#reader(&block) ⇒ Object



113
114
115
# File 'lib/ldp/response.rb', line 113

def reader(&block)
  reader_for_content_type.new(body, base_uri: page_subject, &block)
end

#resource?Boolean

Is the response an LDP resource?

Returns:

  • (Boolean)


48
49
50
# File 'lib/ldp/response.rb', line 48

def resource?
  Array(links[TYPE]).include? RDF::Vocab::LDP.Resource.to_s
end

#subjectObject

Get the subject for the response



79
80
81
82
83
84
85
# File 'lib/ldp/response.rb', line 79

def subject
  @subject ||= if has_page?
    graph.first_object [page_subject, RDF::Vocab::LDP.pageOf, nil]
  else
    page_subject
  end
end

#typesObject

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



137
138
139
# File 'lib/ldp/response.rb', line 137

def types
  Array(links[TYPE])
end