Sha256: dad49b3f090d9cb6aeb2bd27f8c74b2ab96054aa7d722cc455e04b314ec5503f

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require 'delegate'

module Backburner
  class Connection < SimpleDelegator
    class BadURL < RuntimeError; end

    attr_accessor :url, :beanstalk

    def initialize(url)
      @url = url
      connect!
    end

    # Sets the delegator object to the underlying beanstalk connection
    # self.put(...)
    def __getobj__
      __setobj__(@beanstalk)
      super
    end

    protected

    # Connects to a beanstalk queue
    def connect!
      @beanstalk ||= Beanstalk::Pool.new(beanstalk_addresses)
    end

    # Returns the beanstalk queue addresses
    # beanstalk_addresses => ["localhost:11300"]
    def beanstalk_addresses
      uris = self.url.split(/[\s,]+/)
      uris.map {|uri| beanstalk_host_and_port(uri)}
    end

    # Returns a host and port based on the uri_string given
    # beanstalk_host_and_port("beanstalk://localhost") => "localhost:11300"
    def beanstalk_host_and_port(uri_string)
      uri = URI.parse(uri_string)
      raise(BadURL, uri_string) if uri.scheme != 'beanstalk'
      "#{uri.host}:#{uri.port || 11300}"
    end
  end # Connection
end # Backburner

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
backburner-0.0.1 lib/backburner/connection.rb