class Irc::Bot::Journal::Query

Describes a query on journal entries, it is used both to describe a subscription aswell as to query persisted messages. There two ways to declare a Query instance, using the DSL like this:

Query.define do
  id 'foo'
  id 'bar'
  topic 'log.irc.*'
  topic 'log.core'
  timestamp from: Time.now, to: Time.now + 60 * 10
  payload 'action': :privmsg
  payload 'channel': '#rbot'
  payload 'foo.bar': 'baz'
end

or using a hash: (NOTE: avoid using symbols in payload)

Query.define({
  id: ['foo', 'bar'],
  topic: ['log.irc.*', 'log.core'],
  timestamp: {
    from: Time.now
    to: Time.now + 60 * 10
  },
  payload: {
    'action' => 'privmsg'
    'channel' => '#rbot',
    'foo.bar' => 'baz'
  }
})

Attributes

id[R]

array of ids to match (OR)

payload[R]

hash of key values to match

timestamp[R]

hash with from: timestamp and to: timestamp

topic[R]

array of topics to match with wildcard support (OR)

Public Class Methods

define(query=nil, &block) click to toggle source
# File lib/rbot/journal.rb, line 267
def self.define(query=nil, &block)
  factory = Factory.new
  if block_given?
    factory.instance_eval(&block)
    query = factory.query
  end
  Query.new query
end
new(query) click to toggle source
# File lib/rbot/journal.rb, line 176
def initialize(query)
  @id = query[:id] || []
  @id = [@id] if @id.is_a? String
  @topic = query[:topic] || []
  @topic = [@topic] if @topic.is_a? String
  @timestamp = {
    from: nil, to: nil
  }
  if query[:timestamp] and query[:timestamp][:from]
    @timestamp[:from] = query[:timestamp][:from]
  end
  if query[:timestamp] and query[:timestamp][:to]
    @timestamp[:to] = query[:timestamp][:to]
  end
  @payload = query[:payload] || {}
end

Public Instance Methods

matches?(message) click to toggle source

returns true if the given message matches the query

# File lib/rbot/journal.rb, line 194
def matches?(message)
  return false if not @id.empty? and not @id.include? message.id
  return false if not @topic.empty? and not topic_matches? message.topic
  if @timestamp[:from]
    return false unless message.timestamp >= @timestamp[:from]
  end
  if @timestamp[:to]
    return false unless message.timestamp <= @timestamp[:to]
  end
  found = false
  @payload.each_pair do |key, value|
    begin
      message.get(key.to_s)
    rescue ArgumentError
    end
    found = true
  end
  return false if not found and not @payload.empty?
  true
end
topic_matches?(_topic) click to toggle source
# File lib/rbot/journal.rb, line 215
def topic_matches?(_topic)
  @topic.each do |topic|
    if topic.include? '*'
      match = true
      topic.split('.').zip(_topic.split('.')).each do |a, b|
        if a == '*'
          if not b or b.empty?
            match = false
          end
        else
          match = false unless a == b
        end
      end
      return true if match
    else
      return true if topic == _topic
    end
  end
  return false
end