Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for LRDD to Rack::HostMeta

This update adds support for Link-Pattern elements as
described in the IETF draft:
"Link-based Resource Descriptor Discovery"

For more information, see:
  http://tools.ietf.org/html/draft-hammer-discovery
  • Loading branch information...
commit 5cfaaf8ca32edaff8c9286d489eb7fdc2f3aba48 1 parent 988cd66
Jon Crosby jcrosby authored
Showing with 28 additions and 15 deletions.
  1. +16 −10 lib/rack/contrib/host_meta.rb
  2. +12 −5 test/spec_rack_host_meta.rb
26 lib/rack/contrib/host_meta.rb
View
@@ -1,14 +1,19 @@
module Rack
# Rack middleware implementing the IETF draft: "Host Metadata for the Web"
+ # including support for Link-Pattern elements as described in the IETF draft:
+ # "Link-based Resource Descriptor Discovery."
#
# Usage:
# use Rack::HostMeta do
- # register :uri => '/robots.txt', :rel => 'robots'
- # register :uri => '/w3c/p3p.xml', :rel => 'privacy', :type => 'application/p3p.xml'
+ # link :uri => '/robots.txt', :rel => 'robots'
+ # link :uri => '/w3c/p3p.xml', :rel => 'privacy', :type => 'application/p3p.xml'
+ # link :pattern => '{uri};json_schema', :rel => 'describedby', :type => 'application/x-schema+json'
# end
#
- # See also: http://tools.ietf.org/html/draft-nottingham-site-meta
+ # See also:
+ # http://tools.ietf.org/html/draft-nottingham-site-meta
+ # http://tools.ietf.org/html/draft-hammer-discovery
#
# TODO:
# Accept POST operations allowing downstream services to register themselves
@@ -16,9 +21,9 @@ module Rack
class HostMeta
def initialize(app, &block)
@app = app
- @links = []
+ @lines = []
instance_eval(&block)
- @response = @links.join("\n")
+ @response = @lines.join("\n")
end
def call(env)
@@ -31,11 +36,12 @@ def call(env)
protected
- def register(config)
- link = "Link: <#{config[:uri]}>;"
- link += " rel=\"#{config[:rel]}\"" if config[:rel]
- link += " type=\"#{config[:type]}\"" if config[:type]
- @links << link
+ def link(config)
+ line = config[:uri] ? "Link: <#{config[:uri]}>;" : "Link-Pattern: <#{config[:pattern]}>;"
+ fragments = []
+ fragments << "rel=\"#{config[:rel]}\"" if config[:rel]
+ fragments << "type=\"#{config[:type]}\"" if config[:type]
+ @lines << "#{line} #{fragments.join("; ")}"
end
end
end
17 test/spec_rack_host_meta.rb
View
@@ -10,8 +10,9 @@
use Rack::Lint
use Rack::ContentLength
use Rack::HostMeta do
- register :uri => '/robots.txt', :rel => 'robots'
- register :uri => '/w3c/p3p.xml', :rel => 'privacy', :type => 'application/p3p.xml'
+ link :uri => '/robots.txt', :rel => 'robots'
+ link :uri => '/w3c/p3p.xml', :rel => 'privacy', :type => 'application/p3p.xml'
+ link :pattern => '{uri};json_schema', :rel => 'describedby', :type => 'application/x-schema+json'
end
run Rack::NotFound.new('test/404.html')
end
@@ -26,18 +27,24 @@
@response['Content-Type'].should.equal 'application/host-meta'
end
- specify "should include a Link entry for each item in the config block" do
+ specify "should include a Link entry for each Link item in the config block" do
@response.body.should.match(/Link:\s*<\/robots.txt>;.*\n/)
@response.body.should.match(/Link:\s*<\/w3c\/p3p.xml>;.*/)
end
- specify "should include a rel attribute for each Link entry where specified" do
+ specify "should include a Link-Pattern entry for each Link-Pattern item in the config" do
+ @response.body.should.match(/Link-Pattern:\s*<\{uri\};json_schema>;.*/)
+ end
+
+ specify "should include a rel attribute for each Link or Link-Pattern entry where specified" do
@response.body.should.match(/rel="robots"/)
@response.body.should.match(/rel="privacy"/)
+ @response.body.should.match(/rel="describedby"/)
end
- specify "should include a type attribute for each Link entry where specified" do
+ specify "should include a type attribute for each Link or Link-Pattern entry where specified" do
@response.body.should.match(/Link:\s*<\/w3c\/p3p.xml>;.*type.*application\/p3p.xml/)
+ @response.body.should.match(/Link-Pattern:\s*<\{uri\};json_schema>;.*type.*application\/x-schema\+json/)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.