Sha256: 76ab9bb5fefab71e9bb4c9199df97cd70eb1d8bc0375e0004246f016c48a195f

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

module ClockWindow
  class Filters
    def initialize(**kwargs)
      @title_range = kwargs.fetch(:title_range)     { 0..60 }
      @lazy_matchers = kwargs.fetch(:lazy_matchers) { true  }
      @no_notify = kwargs.fetch(:no_notify)         { false }

      # These are destructive matchers.  String will conform to match data.
      @matches = Array(kwargs.fetch(:matches)           { [] }) # [/(thing)/, /(in)/] # parenthesis takes priority
      @substitutions = Array(kwargs.fetch(:substitutions) { [] }) # [[//,'']]
    end

    def call(source)
      apply_filters(source)
    end

    private
    def apply_filters(target)
      target = matchers(target)
      @substitutions.each do |a,b|
        target = target.gsub(a,b)
      end
      target = no_notify(target)
      target[@title_range]
    end

    def matchers(target)
      @matches.each do |fltr|
        if @lazy_matchers
          tmp = target.match( Regexp.new(fltr) )
          tmp = tmp[1] || tmp[0] if tmp.is_a? MatchData
          target = tmp || target 
        else
          target = target[Regexp.new(fltr)].to_s
        end
      end
      target
    end

    def no_notify(target)
      # Remove Twitter notifications (must be last)
      # Other sites may follow same notification pattern
      target = target.gsub(Regexp.new(/\A ?\(\d+\) ?/), '') if @no_notify
      target
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
clock_window-0.0.6 lib/clock_window/filters.rb
clock_window-0.0.5 lib/clock_window/filters.rb