module Schlep

Constants

VERSION

Attributes

redis_url[W]

Public Instance Methods

app() click to toggle source
# File lib/schlep.rb, line 12
def app
  @app ||= ""
end
app=(string) click to toggle source
# File lib/schlep.rb, line 16
def app=(string)
  @app = sanitize string
end
configure() { |self| ... } click to toggle source
# File lib/schlep.rb, line 20
def configure
  yield self
end
envelope(type, message, options = {}) click to toggle source
# File lib/schlep.rb, line 24
def envelope(type, message, options = {})
  {
    :timestamp => timestamp,
    :app =>       options[:app]  || app,
    :host =>      options[:host] || host,
    :type =>      type,
    :message =>   serialize_message(message)
  }
end
event(type, message, options = {}) click to toggle source
# File lib/schlep.rb, line 34
def event(type, message, options = {})
  events type, [message], options
end
events(type, messages, options = {}) click to toggle source
# File lib/schlep.rb, line 38
def events(type, messages, options = {})
  options.keys.each { |k| options[k] = sanitize(options[k]) }

  messages.map! { |message| envelope(type, message, options).to_json }

  suppress_redis_errors do
    redis.pipelined do
      while messages.any?
        redis.rpush key, messages.shift
      end
    end
  end
end
host() click to toggle source
# File lib/schlep.rb, line 52
def host
  @host ||= sanitize `hostname`
end
host=(string) click to toggle source
# File lib/schlep.rb, line 56
def host=(string)
  @host = sanitize string
end
key() click to toggle source
# File lib/schlep.rb, line 60
def key
  @key ||= :schlep
end
redis() click to toggle source
# File lib/schlep.rb, line 64
def redis
  @redis ||= Redis.new redis_options
end
redis_options() click to toggle source
# File lib/schlep.rb, line 68
def redis_options
  return {} unless redis_url

  parsed_url = URI::parse(redis_url)

  {
    :host     => parsed_url.host,
    :port     => parsed_url.port,
    :password => parsed_url.password
  }
end
redis_url() click to toggle source
# File lib/schlep.rb, line 80
def redis_url
  @redis_url ||= ENV["REDIS_URL"] or ENV["REDISTOGO_URL"]
end
reset() click to toggle source
# File lib/schlep.rb, line 84
def reset
  %w[app host redis redis_url].each do |ivar|
    instance_variable_set "@#{ivar}", nil
  end
end
serialize_message(message) click to toggle source
# File lib/schlep.rb, line 90
def serialize_message(message)
  return message unless
    message.is_a? String and
    message.match /\{.+\}/

  begin
    JSON.parse message
  rescue JSON::ParserError
    message
  end
end
timestamp() click to toggle source
# File lib/schlep.rb, line 102
def timestamp
  Time.now.to_f
end

Private Instance Methods

sanitize(string) click to toggle source
# File lib/schlep.rb, line 108
def sanitize(string)
  string.gsub! /\s/, ""
  string.gsub! /^[^\w]+|[^\w]+$/, ""
  string.gsub! /[^\w\.\-]+/, ":"

  string
end
suppress_redis_errors() { || ... } click to toggle source
# File lib/schlep.rb, line 116
def suppress_redis_errors
  begin
    yield
  rescue Errno::ECONNREFUSED => e
    puts e.inspect unless ENV['RUBY_ENV'] == 'test'
  end
end