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

Instance Method Summary collapse

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?

Returns:

  • (Boolean)


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

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

Returns:

  • (Boolean)


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

.resource?(response) ⇒ Boolean

Is the response an LDP resource?

Returns:

  • (Boolean)


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

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



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

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)


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

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

#dupObject



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

#etagObject

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

#graphObject

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?

Returns:

  • (Boolean)


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

Returns:

  • (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_modifiedObject

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

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

Returns:

  • (Boolean)


175
176
177
# File 'lib/ldp/response.rb', line 175

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

#page_subjectObject

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

#preferencesObject



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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


85
86
87
# File 'lib/ldp/response.rb', line 85

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

#subjectObject

Get the subject for the response



106
107
108
# File 'lib/ldp/response.rb', line 106

def subject
  page_subject
end

#typesObject

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