Sha256: fc9881832316275716a0ab1c8166e0bde6abadd3b54326402e3a8ca36630cd30

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

require 'rack/response'

module Rack
  module LinkHeaders
    # Helper for easy adding link headers to rack responses.
    module Helper
      def self.included(base)
        base.send :include, InstanceMethods
      end

      module InstanceMethods
        # Add a new Link header to response headers. Requires
        # a URL and a params hash. Does not escape or
        # sanitize anything. Manual added Link headers will be
        # overridden.
        def link(url, params = {})
          links << {:url => url.to_s, :params => params}

          self["Link"] = links.to_a.map do |link|
            "<#{link[:url]}>" + link[:params].keys.sort.map do |k|
              "; #{k}=\"#{link[:params][k]}\""
            end.join
          end.join(', ')
        end

        def links
          @__links ||= []
        end
      end
    end
  end
end

Rack::Response.send :include, Rack::LinkHeaders::Helper
Rack::Response::Helpers.send :include, Rack::LinkHeaders::Helper

ActionDispatch::Response.send :include, Rack::LinkHeaders::Helper if defined?(ActionDispatch::Response)
ActionController::TestResponse.send :include, Rack::LinkHeaders::Helper if defined?(ActionController::TestResponse)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rack-link_headers-2.2.2 lib/rack/link_headers/helper.rb
rack-link_headers-2.2.1 lib/rack/link_headers/helper.rb