middleman/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb

84 lines
2.2 KiB
Ruby
Raw Normal View History

require 'middleman-core/sitemap/resource'
module Middleman
module Sitemap
module Extensions
class RequestEndpoints < Extension
# Manages the list of proxy configurations and manipulates the sitemap
# to include new resources based on those configurations
def initialize(app, config={}, &block)
super
2014-07-16 03:01:45 +02:00
@app.add_to_config_context(:endpoint, &method(:create_endpoint))
2014-01-01 03:21:30 +01:00
@endpoints = {}
end
2014-01-01 03:21:30 +01:00
# Setup a proxy from a path to a target
# @param [String] path
# @param [Hash] opts The :path value gives a request path if it
# differs from the output path
2014-07-03 04:04:34 +02:00
Contract String, Or[({ path: String }), Proc] => Any
2014-01-01 03:21:30 +01:00
def create_endpoint(path, opts={}, &block)
endpoint = {
2014-04-29 19:50:21 +02:00
request_path: path
2014-01-01 03:21:30 +01:00
}
if block_given?
endpoint[:output] = block
else
2014-04-29 19:50:21 +02:00
endpoint[:request_path] = opts[:path] if opts.key?(:path)
2014-01-01 03:21:30 +01:00
end
2014-01-01 03:21:30 +01:00
@endpoints[path] = endpoint
2014-01-01 03:21:30 +01:00
@app.sitemap.rebuild_resource_list!(:added_endpoint)
end
2014-01-01 03:21:30 +01:00
# Update the main sitemap resource list
2014-07-03 04:04:34 +02:00
# @return Array<Middleman::Sitemap::Resource>
Contract ResourceList => ResourceList
2014-01-01 03:21:30 +01:00
def manipulate_resource_list(resources)
resources + @endpoints.map do |path, config|
r = EndpointResource.new(
@app.sitemap,
path,
config[:request_path]
)
2014-04-29 19:50:21 +02:00
r.output = config[:output] if config.key?(:output)
2014-01-01 03:21:30 +01:00
r
end
end
2014-01-01 03:21:30 +01:00
end
2014-01-01 03:21:30 +01:00
class EndpointResource < ::Middleman::Sitemap::Resource
2014-07-03 04:04:34 +02:00
Contract None => Maybe[Proc]
2014-01-01 03:21:30 +01:00
attr_accessor :output
2014-05-31 07:46:15 +02:00
def initialize(store, path, request_path)
2014-01-01 03:21:30 +01:00
super(store, path)
2014-05-31 07:46:15 +02:00
@request_path = ::Middleman::Util.normalize_path(request_path)
2014-01-01 03:21:30 +01:00
end
2013-11-25 09:12:39 +01:00
2014-07-03 04:04:34 +02:00
Contract None => String
2014-05-31 07:46:15 +02:00
attr_reader :request_path
2014-07-03 04:04:34 +02:00
Contract None => Bool
2014-01-01 03:21:30 +01:00
def template?
true
end
2014-07-03 04:04:34 +02:00
Contract Args[Any] => String
2014-04-29 20:43:05 +02:00
def render(*)
2014-04-29 19:50:21 +02:00
return output.call if output
2014-01-01 03:21:30 +01:00
end
2014-07-03 04:04:34 +02:00
Contract None => Bool
2014-01-01 03:21:30 +01:00
def ignored?
false
end
end
end
end
end