Project

lapine

0.01
No commit activity in last 3 years
No release in over 3 years
Talk to rabbits
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.1
~> 13.0
~> 3.1

Runtime

>= 0
>= 0
>= 0
>= 0.2.2
 Project Readme

Lapine

Gem Version

Speak to RabbitMQ. This gem serves as a wrapper for publishing messages to RabbitMQ via the Bunny gem, and for consuming messages using the AMQP gem.

Configuration

Initialization can be done inline in a daemon, or if used in Rails an initializer should be made at config/initializers/lapine.rb

Register a connection. This connection should be given a name, and a hash of connection options that will be passed through to Bunny.

Lapine.add_connection 'my-connection', {
  host: 'my-rabbitmq.mine.com',
  port: 5672,
  user: 'rabbit',
  password: 'meow'
}

Then register an exchange.

Lapine.add_exchange 'efrafa', 
  durable: true, 
  connection: 'my-connection',  # required
  type: 'topic'              # required

Publisher Usage

Define a class that configures which exchange is used. This class must define #to_hash

require 'lapine'

class Worker
  include Lapine::Publisher
  
  exchange 'efrafa'
  
  def initialize(action)
    @action = action
  end
  
  def to_hash
    {
      'action' => @action
    }
  end
end

This class can be used to publish messages onto its exchange:

Worker.new('dig').publish

Publishing can take a routing key for topic exchanges:

Worker.new('dig').publish('rabbits.drones')

Note that the #initialize method and the contents of #to_hash are arbitrary.

Consumer Usage

Please see the Lapine wiki for documentation on defining and configuring consumers.

But... WHY

  • This should be dead simple, but everything else was either too complex or assumed very specific configurations different from what we want.

Testing

Lapine comes with helpers to stub out calls to RabbitMQ. This allows you to write tests using Lapine, without having to actually run RabbitMQ in your test suite.

require 'lapine/test/rspec_helper'

RSpec.configure do |config|
  config.include Lapine::Test::RSpecHelper, fake_rabbit: true

  config.before :each, :fake_rabbit do |example|
    Lapine::Test::RSpecHelper.setup(example)
  end

  config.after :each, :fake_rabbit do
    Lapine::Test::RSpecHelper.teardown
  end
end

An example test would look something like this:

RSpec.describe MyPublisher, fake_rabbit: true do
  let(:exchange) { Lapine.find_exchange('my.topic') }
  let(:queue) { exchange.channel.queue.bind(exchange) }

  describe 'publishing' do
    it 'adds a message to a queue' do
      MyPublisher.new.publish('my.things')
      expect(queue.message_count).to eq(1)
    end
  end
end

Contributing

  1. Fork it ( https://github.com/[my-github-username]/lapine/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request