cultivated_rabbit 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a934cf00cc95b61abcac90314bc1d8fb1b0423b85939eb4d9c341571a7fa7afe
4
+ data.tar.gz: 67d88fa2ff4e43f446c66a7df00d8778599d33bbf3744f8b05e68eb64fcfd907
5
+ SHA512:
6
+ metadata.gz: 35a5b77890bc844a71bcfda0a519f836ccf35fcad17dd686d9986dc2ecc9967587c29cdda5fd89d0a6a5453de13f174e2d1ced71cad4785c4804a93836d3c249
7
+ data.tar.gz: 3cfce06cf8e0f50286aa32c9ae35e0da26f6ac17dd1aaa65322f1fb614e34224289781ccacfdbffaaa8fd48ddc98f6ec11091a2b81c1a6e5fff6995f12bf4278
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.16.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at damien@cultivatedcode.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in cultivated_rabbit.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cultivated_rabbit (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ amq-protocol (2.3.0)
10
+ bunny (2.13.0)
11
+ amq-protocol (~> 2.3, >= 2.3.0)
12
+ diff-lcs (1.3)
13
+ rake (10.5.0)
14
+ rspec (3.8.0)
15
+ rspec-core (~> 3.8.0)
16
+ rspec-expectations (~> 3.8.0)
17
+ rspec-mocks (~> 3.8.0)
18
+ rspec-core (3.8.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-expectations (3.8.2)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-mocks (3.8.0)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.8.0)
26
+ rspec-support (3.8.0)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ bundler (~> 1.16)
33
+ bunny (~> 2.13)
34
+ cultivated_rabbit!
35
+ rake (~> 10.0)
36
+ rspec (~> 3.0)
37
+
38
+ BUNDLED WITH
39
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Cultivated Code
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ # CultivatedRabbit
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'cultivated_rabbit'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cultivated_rabbit
18
+
19
+ ## Usage
20
+
21
+ Used as a wrapper for rabbit, config based.
22
+
23
+ The config is based on a yml file.
24
+
25
+ Imagine you have a config file located at: `some_project/config/rabbit_config.yml`
26
+
27
+ ```yml
28
+ ---
29
+ rabbit_mq:
30
+ connection_string: amqp://rabbit:5672
31
+ queue_names: # Queue names object can have any valid bunny params. http://rubybunny.info/articles/queues.html
32
+ - publish: foo
33
+ subscribe: bar
34
+ durable: true
35
+ auto_delete: false
36
+ exchange_name: foobar
37
+ exclusive: false
38
+ - publish: hello
39
+
40
+ ```
41
+
42
+ You can use this config like so:
43
+
44
+ ```ruby
45
+ require 'cultivated_rabbit'
46
+
47
+ callback = Proc.new do |body, rabbit| # body is the rabbit message body, rabbit is the rabbit wrapper in case you would like to publish another message. rabbi.publish(queue_name -> optional will read publish from config)
48
+ puts body
49
+ puts rabbit
50
+ rabbit.publish(message: 'hello world')
51
+ rabbit.publish(queue_name: 'some_queue', message: 'bye world')
52
+ end
53
+
54
+ CultivatedRabbit.load_from_config(
55
+ config_path: 'some_project/config/rabbit_config.yml',
56
+ sub_proc: callback
57
+ )
58
+
59
+ loop { sleep 5 }
60
+
61
+ ```
62
+
63
+ It can also be used without a config by caling `CultivatedRabbit.rabbit_wrapper` this will return the rabbit_wrapper class passed to the proc.
64
+
65
+ ```ruby
66
+ require 'cultivated_rabbit'
67
+
68
+ rabbit_wrapper = CultivatedRabbit.rabbit_wrapper.new(connection_string: 'amqp://localhost:1567', exchange_name: 'default', config: nil) # You can also pass in a config here
69
+ rabbit_wrapper.publish('some_queue_name') do |body|
70
+ puts body
71
+ rabbit_wrapper.publish(queue_name: 'some_queue', message: 'hello world')
72
+ end
73
+
74
+ loop { sleep 5 }
75
+
76
+ ```
77
+
78
+ ## Development
79
+
80
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
81
+
82
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
83
+
84
+ ## Contributing
85
+
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cultivated_rabbit. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
87
+
88
+ ## License
89
+
90
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
91
+
92
+ ## Code of Conduct
93
+
94
+ Everyone interacting in the CultivatedRabbit project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/cultivated_rabbit/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cultivated_rabbit"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,40 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "cultivated_rabbit/version"
5
+
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "cultivated_rabbit"
9
+ spec.version = CultivatedRabbit::VERSION
10
+ spec.authors = [ "Cultivated Code", "Damien Touchette"]
11
+ spec.email = ["damien@cultivatedcode.com", "cj@cultivatedcode.com"]
12
+
13
+ spec.summary = %q{Cultivated Code's rabbit wrapper}
14
+ spec.description = %q{ Used to more easily interact with rabbit mq. }
15
+ spec.homepage = "https://cultivatedcode.com"
16
+ spec.license = "MIT"
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
22
+ else
23
+ raise "RubyGems 2.0 or newer is required to protect against " \
24
+ "public gem pushes."
25
+ end
26
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_development_dependency "bundler", "~> 1.16"
37
+ spec.add_development_dependency "rake", "~> 10.0"
38
+ spec.add_development_dependency "rspec", "~> 3.0"
39
+ spec.add_development_dependency "bunny", "~> 2.13"
40
+ end
@@ -0,0 +1,5 @@
1
+ require './cultivated_rabbit'
2
+ require_relative './services/config/config.rb'
3
+
4
+ CultivatedRabbit.load_from_config(config_path: Services::Config::DEFAULT_CONFIG_PATH, sub_proc: nil)
5
+ loop { sleep 5 }
@@ -0,0 +1,3 @@
1
+ module CultivatedRabbit
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,12 @@
1
+ require_relative './cultivated_rabbit/version.rb'
2
+ require_relative './services/public_api/public_api.rb'
3
+
4
+ module CultivatedRabbit
5
+ def self.rabbit_wrapper
6
+ Services::PublicApi.rabbit_wrapper
7
+ end
8
+
9
+ def self.load_from_config(config_path: nil, sub_proc: nil)
10
+ Services::PublicApi.load_from_config(config_path: config_path, sub_proc: sub_proc)
11
+ end
12
+ end
@@ -0,0 +1,44 @@
1
+ require_relative './config.rb'
2
+
3
+ module Services
4
+ module Config
5
+ module Base
6
+ module ConfigStrucBase
7
+ QUEUE_NAME_KEYS = [
8
+ 'publish',
9
+ 'subscribe',
10
+ 'durable',
11
+ 'auto_delete',
12
+ 'exchange_name',
13
+ 'exclusive'
14
+ ]
15
+ ROOT_KEY = 'rabbit_mq'.freeze
16
+ QUEUE_NAME_KEY = 'queue_names'.freeze
17
+ CONNECTION_STRING_KEY = 'connection_string'.freeze
18
+ INIT_ERR_MSG = 'ConfigStruct#init -> #setup_vars'.freeze
19
+ BASIC_KEY = 'is_basic'.freeze
20
+
21
+
22
+ def sub_queue_name
23
+ queue_names[0].subscribe
24
+ end
25
+
26
+ def pub_queue_name
27
+ queue_names[0].publish
28
+ end
29
+
30
+ def durable
31
+ queue_names[0].durable
32
+ end
33
+
34
+ def durable
35
+ queue_names[0].durable
36
+ end
37
+
38
+ def exchange_name
39
+ queue_names[0].exchange_name
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,9 @@
1
+ require_relative '../services.rb'
2
+
3
+ module Services
4
+ module Config
5
+ CONFIG_DIR = File.dirname(__FILE__)
6
+ CONFIG_FILE_NAME = 'default_settings.yml'.freeze
7
+ DEFAULT_CONFIG_PATH = "#{CONFIG_DIR}/#{CONFIG_FILE_NAME}".freeze
8
+ end
9
+ end
@@ -0,0 +1,36 @@
1
+ require_relative './config.rb'
2
+ require_relative './errors/parser.rb'
3
+ require_relative './base.rb'
4
+
5
+ Struct.new('Config', :rabbit_mq)
6
+ Struct.new('RabbitMq', :connection_string, :queue_names)
7
+ Struct.new('QueueName', :publish, :subscribe, :durable, :auto_delete, :exchange_name, :exclusive)
8
+
9
+ module Services
10
+ module Config
11
+ class ConfigStruct
12
+ include Services::Config::Base::ConfigStrucBase
13
+ attr_reader :config, :queue_names, :rabbit_mq
14
+
15
+ def initialize(config_hash = nil)
16
+ setup_vars(config_hash)
17
+ end
18
+
19
+ def setup_vars(config_hash)
20
+ if !config_hash.nil?
21
+ @is_basic = config_hash.fetch(BASIC_KEY, false)
22
+ @config_hash = config_hash
23
+ @config = Struct::Config.new(config_hash[ROOT_KEY])
24
+ @queue_names = config_hash[ROOT_KEY][QUEUE_NAME_KEY].map do |val|
25
+ params = QUEUE_NAME_KEYS.map { |key| val.fetch(key, nil) }
26
+ Struct::QueueName.new(*params)
27
+ end
28
+ @rabbit_mq = Struct::RabbitMq.new(config_hash[ROOT_KEY][CONNECTION_STRING_KEY], @queue_names)
29
+ else
30
+ throw Services::Config::Errors::InvalidConfig.new(e.message, INIT_ERR_MSG)
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,11 @@
1
+ ---
2
+ rabbit_mq:
3
+ connection_string: amqp://rabbit:5672
4
+ queue_names: # Queue names object can have any valid bunny params. http://rubybunny.info/articles/queues.html
5
+ - publish: foo
6
+ subscribe: bar
7
+ durable: true
8
+ auto_delete: false
9
+ exchange_name: foobar
10
+ exclusive: false
11
+ - publish: hello
@@ -0,0 +1,9 @@
1
+ require_relative '../config.rb'
2
+
3
+ module Services
4
+ module Config
5
+ module Errors
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,24 @@
1
+ require_relative './errors.rb'
2
+
3
+ module Services
4
+ module Config
5
+ module Errors
6
+ class Parser < StandardError
7
+ attr_reader :last_called_method
8
+
9
+ def initialize(msg = "Something went wrong", last_called_method = '#init')
10
+ @last_called_method = last_called_method
11
+ super(msg)
12
+ end
13
+ end
14
+
15
+ class InvalidConfig < Parser
16
+ attr_reader :last_called_method
17
+
18
+ def initialize(msg, last_called_method)
19
+ super(msg, last_called_method)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ require_relative './config.rb'
2
+ require_relative './errors/parser.rb'
3
+ require 'yaml'
4
+
5
+ module Services
6
+ module Config
7
+ class Parser
8
+ attr_reader :config_path, :config_structs
9
+
10
+ def initialize(config_path = Services::Config::DEFAULT_CONFIG_PATH)
11
+ @config_path = config_path
12
+ @config_hash = YAML.load_file(@config_path)
13
+ @config_structs = nil
14
+ end
15
+
16
+ def load_config
17
+ begin
18
+ @config_structs = @config_structs.nil? ? Services::Config::ConfigStruct.new(@config_hash) : @config_structs
19
+ rescue StandardError => e
20
+ throw Services::Config::Errors::InvalidConfig.new(e.message, '#load_config')
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,36 @@
1
+ require_relative '../services.rb'
2
+ require_relative '../rabbit_wrapper/base.rb'
3
+ require_relative '../rabbit_wrapper/from_config.rb'
4
+ require_relative '../config/parser.rb'
5
+
6
+ module Services
7
+ module PublicApi
8
+ def self.default_proc(body, rabbit_wrapper)
9
+ puts "
10
+ No proc given but received this msg: #{body} \n
11
+ With this instance of rabbit: #{rabbit_wrapper}
12
+
13
+ Pass a proc to CultivatedRabbit.load_from_config(sub_proc: Proc.new { |body, rabbit_wrapper| your logic })
14
+ "
15
+ rabbit_wrapper.publish('Hello World')
16
+ end
17
+
18
+ # returns a rabbit class from config file
19
+ def self.load_from_config(config_path: '', sub_proc: nil)
20
+ config = Services::Config::Parser.new(config_path)
21
+ proc_default = proc { |body, rabbit| default_proc(body, rabbit) }
22
+
23
+ Services::RabbitWrapper::FromConfig.run(config: config, sub_proc: sub_proc.nil? ? proc_default : sub_proc)
24
+ end
25
+
26
+ # returns a rabbit_wrapper class reference with no config
27
+ def self.rabbit_wrapper
28
+ Services::RabbitWrapper::Base
29
+ end
30
+
31
+ # internal logs, to be implemented
32
+ def self.logs
33
+ raise NotImplementedError
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,74 @@
1
+ require 'bunny'
2
+ require_relative './base_helpers.rb'
3
+ require_relative './rabbit_wrapper.rb'
4
+ require_relative '../config/config_struct.rb'
5
+
6
+ module Services
7
+ module RabbitWrapper
8
+ class Base
9
+ include Services::RabbitWrapper::BaseHelpers
10
+
11
+ def initialize(connection_string: nil, exchange_name: nil, config: nil)
12
+ @conn = Bunny.new(connection_string, log_level: :info)
13
+ @conn.start
14
+ # @exchange_name = exchange_name
15
+ @config = setup_config(connection_string, exchange_name, config)
16
+ end
17
+
18
+ def subscribe(queue_name = nil)
19
+ ch = @conn.create_channel
20
+ q_name = sub_queue_name(queue_name)
21
+
22
+ exchange = ch.direct(
23
+ q_name,
24
+ :durable => @config.durable
25
+ )
26
+
27
+ q = ch.queue(q_name, :durable => @config.durable)
28
+ q.bind(exchange, :routing_key => q_name)
29
+ ch.prefetch(1)
30
+
31
+ puts " [*] Waiting for messages (#{queue_name}). To exit press CTRL+C"
32
+ begin
33
+ q.subscribe(manual_ack: true) do |delivery_info, properties, body|
34
+ puts " [x] Received (redelivery: #{redelivery?(delivery_info)})"
35
+
36
+ yield(body)
37
+
38
+ ch.ack(delivery_info.delivery_tag)
39
+ puts " - Message acknowledged"
40
+ puts " [x] Done"
41
+ end
42
+ rescue Interrupt => _
43
+ exit(0)
44
+ end
45
+ end
46
+
47
+ def publish(queue_name: nil, message: nil)
48
+ ch = @conn.create_channel
49
+ q_name = pub_queue_name(queue_name)
50
+ exchange = ch.direct(q_name, :durable => @config.durable)
51
+
52
+ puts "Sending message: queue=#{q_name} - message=#{message}"
53
+ q = ch.queue(q_name, :durable => @config.durable)
54
+ q.bind(exchange, :routing_key => q_name)
55
+ exchange.publish(message, :routing_key => q_name)
56
+ end
57
+
58
+ def close
59
+ @conn.close
60
+ end
61
+
62
+ private
63
+
64
+ def redelivery?(delivery_info)
65
+ begin
66
+ delivery_info.redelivered
67
+ rescue => e
68
+ puts "Redelivered method does not exist"
69
+ return nil
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,50 @@
1
+ require_relative './rabbit_wrapper.rb'
2
+
3
+ module Services
4
+ module RabbitWrapper
5
+ module BaseHelpers
6
+
7
+ def basic_config_params(connection_string, exchange_name)
8
+ {
9
+ 'rabbit_mq' => {
10
+ 'connection_string' => connection_string,
11
+ 'queue_names' => [
12
+ {
13
+ 'exchange_name' => exchange_name,
14
+ 'durable' => true
15
+ }
16
+ ],
17
+ 'is_basic' => true
18
+ }
19
+ }
20
+ end
21
+
22
+ def setup_config(connection_string, exchange_name, config)
23
+ if connection_string && exchange_name
24
+ @is_basic = true
25
+ @config = Services::Config::ConfigStruct.new(basic_config_params(connection_string, exchange_name))
26
+ else
27
+ @is_basic = false
28
+ config.load_config
29
+ @config = config.config_structs
30
+ end
31
+ end
32
+
33
+ def sub_queue_name(queue_name = nil)
34
+ return queue_name if @is_basic && !queue_name.nil?
35
+
36
+ return 'queue_name.not.provided' if @is_basic && queue_name.nil?
37
+
38
+ @config.sub_queue_name
39
+ end
40
+
41
+ def pub_queue_name(queue_name = nil)
42
+ return queue_name if @is_basic && !queue_name.nil?
43
+
44
+ return 'queue_name.not.provided' if @is_basic && queue_name.nil?
45
+
46
+ @config.pub_queue_name
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,21 @@
1
+ require_relative './rabbit_wrapper.rb'
2
+ require_relative './base.rb'
3
+
4
+ module Services
5
+ module RabbitWrapper
6
+ module FromConfig
7
+ def self.run(config: nil, sub_proc: nil)
8
+ rabbit_wrapper = Services::RabbitWrapper::Base.new(config: config)
9
+ msg_counter = 0
10
+
11
+ rabbit_wrapper.subscribe do |body|
12
+ msg_counter += 1
13
+ puts " Processing Msg Number #{msg_counter}"
14
+ sub_proc.call(body, rabbit_wrapper)
15
+ end
16
+
17
+ rabbit_wrapper
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,6 @@
1
+ require_relative '../services.rb'
2
+
3
+ module Services
4
+ module RabbitWrapper
5
+ end
6
+ end
File without changes
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cultivated_rabbit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Cultivated Code
8
+ - Damien Touchette
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2019-02-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.16'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.16'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: bunny
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '2.13'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '2.13'
70
+ description: " Used to more easily interact with rabbit mq. "
71
+ email:
72
+ - damien@cultivatedcode.com
73
+ - cj@cultivatedcode.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - bin/console
88
+ - bin/setup
89
+ - cultivated_rabbit.gemspec
90
+ - lib/config_example.rb
91
+ - lib/cultivated_rabbit.rb
92
+ - lib/cultivated_rabbit/version.rb
93
+ - lib/services/config/base.rb
94
+ - lib/services/config/config.rb
95
+ - lib/services/config/config_struct.rb
96
+ - lib/services/config/default_settings.yml
97
+ - lib/services/config/errors/errors.rb
98
+ - lib/services/config/errors/parser.rb
99
+ - lib/services/config/parser.rb
100
+ - lib/services/public_api/public_api.rb
101
+ - lib/services/rabbit_wrapper/base.rb
102
+ - lib/services/rabbit_wrapper/base_helpers.rb
103
+ - lib/services/rabbit_wrapper/from_config.rb
104
+ - lib/services/rabbit_wrapper/rabbit_wrapper.rb
105
+ - lib/services/services.rb
106
+ homepage: https://cultivatedcode.com
107
+ licenses:
108
+ - MIT
109
+ metadata:
110
+ allowed_push_host: https://rubygems.org
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.7.7
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Cultivated Code's rabbit wrapper
131
+ test_files: []