waffle 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'rake'
4
+ gem 'waffle', :git => 'git://github.com/peanut/waffle.git'
data/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # Waffle
2
+
3
+ An abstract flow publisher and subscriber.
4
+
5
+ [![Build Status](https://secure.travis-ci.org/peanut/waffle.png?branch=master)](http://travis-ci.org/peanut/waffle)
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
data/lib/waffle.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'waffle/version'
2
+ require 'waffle/utils'
3
+ require 'waffle/base'
4
+ require 'waffle/configuration'
5
+ require 'waffle/event'
6
+
7
+ module Waffle
8
+ module Strategies
9
+
10
+ autoload :Rabbitmq, 'waffle/strategies/rabbitmq'
11
+
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ module Waffle
2
+ class Base
3
+
4
+ def initialize(strategy = nil)
5
+ @strategy = strategy
6
+ end
7
+
8
+ def publish(flow = 'events', message = '')
9
+ @strategy.publish flow, message
10
+ end
11
+
12
+ def subscribe(flow = '', &block)
13
+ @strategy.subscribe flow, &block
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Waffle
2
+ class Configuration
3
+
4
+ def initialize(custom_params = {})
5
+ @configuration = {'strategy' => 'rabbitmq', 'url' => nil}.merge custom_params
6
+ end
7
+
8
+ def method_missing(m, *args, &block)
9
+ if @configuration.has_key? m.to_s
10
+ @configuration[m.to_s]
11
+ else
12
+ super
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,33 @@
1
+ require 'singleton'
2
+
3
+ module Waffle
4
+ class Event
5
+ include Singleton
6
+
7
+ class << self
8
+ # Syntactic sugar ^_^
9
+ def occured(*args)
10
+ self.instance.occured(*args)
11
+ end
12
+ end
13
+
14
+ def config
15
+ @config ||= Waffle::Configuration.new
16
+ end
17
+
18
+ def transport
19
+ @transport ||= Waffle::Base.new eval("Waffle::Strategies::#{config.strategy.capitalize}").new(config)
20
+ end
21
+
22
+ def occured(event_name = 'event', event_data = nil)
23
+ unless event_data.is_a? Hash
24
+ event_data = {'body' => event_data.to_s}
25
+ end
26
+
27
+ event_data.merge!({'occured_at' => Time.now})
28
+
29
+ transport.publish event_name, Waffle::Utils.encode(event_data)
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,30 @@
1
+ require 'bunny'
2
+
3
+ module Waffle
4
+ module Strategies
5
+ class Rabbitmq
6
+
7
+ def initialize(configuration = nil)
8
+ raise ArgumentError unless configuration && configuration.is_a?(Waffle::Configuration)
9
+ @bunny = Bunny.new configuration.url
10
+ @bunny.start
11
+ end
12
+
13
+ def publish(flow = 'events', message = '')
14
+ @exchange = @bunny.exchange flow
15
+ @exchange.publish message
16
+ end
17
+
18
+ def subscribe(flow = 'events')
19
+ @exchange = @bunny.exchange flow
20
+ @queue = @bunny.queue '', :durable => true, :auto_delete => true
21
+ @queue.bind @exchange
22
+
23
+ @queue.subscribe do |message|
24
+ yield Waffle::Utils.decode(message[:payload])
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ require 'yajl'
2
+
3
+ module Waffle
4
+ class Utils
5
+
6
+ class << self
7
+
8
+ def encode(message = nil)
9
+ Yajl::Encoder.encode message
10
+ end
11
+
12
+ def decode(message = '')
13
+ Yajl::Parser.parse message
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ module Waffle
2
+
3
+ VERSION = '0.1.0'
4
+
5
+ end
@@ -0,0 +1,23 @@
1
+ require 'test/unit'
2
+ require 'waffle/configuration'
3
+ require 'waffle/strategies/rabbitmq'
4
+
5
+ class RabbitmqTest < Test::Unit::TestCase
6
+
7
+ def test_bad_initialization
8
+ assert_raise ArgumentError do
9
+ Waffle::Strategies::Rabbitmq.new
10
+ end
11
+
12
+ assert_raise ArgumentError do
13
+ Waffle::Strategies::Rabbitmq.new 'crap'
14
+ end
15
+ end
16
+
17
+ def test_good_initialization
18
+ assert_nothing_raised do
19
+ Waffle::Strategies::Rabbitmq.new Waffle::Configuration.new
20
+ end
21
+ end
22
+
23
+ end
data/waffle.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ require "waffle/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'waffle'
7
+ s.version = Waffle::VERSION
8
+
9
+ s.homepage = 'http://github.com/peanut/waffle'
10
+ s.authors = ['Alexander Lomakin']
11
+ s.email = 'alexander.lomakin@gmail.com'
12
+
13
+ s.homepage = ''
14
+
15
+ s.summary = 'Abstract flow publisher and subscriber'
16
+ s.description = 'Client part of Patty statistics server'
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+
20
+ s.add_runtime_dependency 'bunny'
21
+ s.add_runtime_dependency 'yajl-ruby'
22
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: waffle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexander Lomakin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-19 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bunny
16
+ requirement: &73646510 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *73646510
25
+ - !ruby/object:Gem::Dependency
26
+ name: yajl-ruby
27
+ requirement: &73645680 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *73645680
36
+ description: Client part of Patty statistics server
37
+ email: alexander.lomakin@gmail.com
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files: []
41
+ files:
42
+ - .travis.yml
43
+ - Gemfile
44
+ - README.md
45
+ - Rakefile
46
+ - lib/waffle.rb
47
+ - lib/waffle/base.rb
48
+ - lib/waffle/configuration.rb
49
+ - lib/waffle/event.rb
50
+ - lib/waffle/strategies/rabbitmq.rb
51
+ - lib/waffle/utils.rb
52
+ - lib/waffle/version.rb
53
+ - test/test_rabbitmq.rb
54
+ - waffle.gemspec
55
+ homepage: ''
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.10
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Abstract flow publisher and subscriber
79
+ test_files: []