Sha256: 594da25ee05fd294526b7b1df0d00c05a738216842c35765d6edffa25a5f50f2

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 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
    #
    # @example
    #   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
    #
    # @example
    #   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

2 entries across 2 versions & 1 rubygems

Version Path
backburner-0.0.3 lib/backburner/connection.rb
backburner-0.0.2 lib/backburner/connection.rb