Sha256: 10838ea799f663fd47842f1774b4707636d6944fab2169994f6f249866ac7781

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

# Since the gem can be used in many ways (sucker daemon, sinatra app, instrument, ad-hoc, don't include anything)
require 'mouth/version'

module Mouth
  class << self
    attr_accessor :logger
    attr_accessor :mongo_host

    # Returns a mongo connection (NOT an em-mongo connection)
    def mongo
      @mongo ||= begin
        require 'mongo'   # require mongo here, as opposed to the top, because we don't want mongo included in the reactor (use em-mongo for that)
        Mongo::Connection.new(self.mongo_host || "localhost").db("mouth")
      end
    end
    
    def collection(collection_name)
      @collections ||= {}
      @collections[collection_name] ||= begin
        c = mongo.collection(collection_name)
        c.ensure_index([["t", 1]], {:background => true, :unique => true})
        c
      end
    end
    
    def mongo_collection_name(namespace)
      "mouth_#{namespace}"
    end
    
    def sanitize_namespace(key)
      key.gsub(/\s+/, '_').gsub(/\//, '-').gsub(/[^a-zA-Z_\-0-9]/, '')
    end
    
    def sanitize_metric(key)
      key.gsub(/\s+/, '_').gsub(/[^a-zA-Z0-9\-_\/]/, '')
    end
    
    # Parses a key into two parts: namespace, and metric.  Also sanitizes each field
    # Returns an array of values: [namespace, metric]
    # eg,
    # parse_key("Ticket.process_new_ticket") # => ["Ticket", "process_new_ticket"]
    # parse_key("Forum List.other! stuff.ok") # => ["Forum_List", "other_stuff-ok"]
    # parse_key("no_namespace") # => ["default", "no_namespace"]
    def parse_key(key)
      parts = key.split(".")
      namespace = nil
      metric = nil
      if parts.length > 1
        namespace = parts.shift
        metric = parts.join("-")
      else
        namespace = "default"
        metric = parts.shift
      end
      
      [sanitize_namespace(namespace), sanitize_metric(metric)]
    end 
    

  end  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mouth-0.8.0 lib/mouth.rb