lib/html/pipeline.rb in html-pipeline-0.0.11 vs lib/html/pipeline.rb in html-pipeline-0.0.12

- old
+ new

@@ -23,11 +23,10 @@ # result_class - The default Class of the result object for individual # calls. Default: Hash. Protip: Pass in a Struct to get # some semblance of type safety. class Pipeline autoload :VERSION, 'html/pipeline/version' - autoload :Pipeline, 'html/pipeline/pipeline' autoload :Filter, 'html/pipeline/filter' autoload :AbsoluteSourceFilter, 'html/pipeline/absolute_source_filter' autoload :BodyContent, 'html/pipeline/body_content' autoload :AutolinkFilter, 'html/pipeline/autolink_filter' autoload :CamoFilter, 'html/pipeline/camo_filter' @@ -63,10 +62,16 @@ # Public: Instrumentation service for the pipeline. # Set an ActiveSupport::Notifications compatible object to enable. attr_accessor :instrumentation_service + # Public: String name for this Pipeline. Defaults to Class name. + attr_writer :instrumentation_name + def instrumentation_name + @instrumentation_name || self.class.name + end + class << self # Public: Default instrumentation service for new pipeline objects. attr_accessor :default_instrumentation_service end @@ -92,11 +97,13 @@ # output of the last filter in the pipeline. def call(html, context = {}, result = nil) context = @default_context.merge(context) context = context.freeze result ||= @result_class.new - instrument "call_pipeline.html_pipeline", :filters => @filters.map(&:name) do + payload = default_payload :filters => @filters.map(&:name), + :context => context, :result => result + instrument "call_pipeline.html_pipeline", payload do result[:output] = @filters.inject(html) do |doc, filter| perform_filter(filter, doc, context, result) end end @@ -107,26 +114,17 @@ # # The filter is instrumented. # # Returns the result of the filter. def perform_filter(filter, doc, context, result) - instrument "call_filter.html_pipeline", :filter => filter.name do + payload = default_payload :filter => filter.name, + :context => context, :result => result + instrument "call_filter.html_pipeline", payload do filter.call(doc, context, result) end end - # Internal: if the `instrumentation_service` object is set, instruments the - # block, otherwise the block is ran without instrumentation. - # - # Returns the result of the provided block. - def instrument(event, payload = nil) - return yield unless instrumentation_service - instrumentation_service.instrument event, payload do - yield - end - end - # Like call but guarantee the value returned is a DocumentFragment. # Pipelines may return a DocumentFragment or a String. Callers that need a # DocumentFragment should use this method. def to_document(input, context = {}, result = nil) result = call(input, context, result) @@ -140,9 +138,39 @@ if output.respond_to?(:to_html) output.to_html else output.to_s end + end + + # Public: setup instrumentation for this pipeline. + # + # Returns nothing. + def setup_instrumentation(name = nil, service = nil) + self.instrumentation_name = name + self.instrumentation_service = + service || self.class.default_instrumentation_service + end + + # Internal: if the `instrumentation_service` object is set, instruments the + # block, otherwise the block is ran without instrumentation. + # + # Returns the result of the provided block. + def instrument(event, payload = nil) + payload ||= default_payload + return yield(payload) unless instrumentation_service + instrumentation_service.instrument event, payload do |payload| + yield payload + end + end + + # Internal: Default payload for instrumentation. + # + # Accepts a Hash of additional payload data to be merged. + # + # Returns a Hash. + def default_payload(payload = {}) + {:pipeline => instrumentation_name}.merge(payload) end end end # XXX nokogiri monkey patches for 1.8