Skip to content

Instantly share code, notes, and snippets.

View igkuz's full-sized avatar

Igor Kuznetsov igkuz

  • Antalya, Turkey
View GitHub Profile
@igkuz
igkuz / start.rb
Created March 12, 2019 16:06
Starting point to create RabbitMQ exchanges and queues with ruby
#!/usr/bin/env ruby
# encoding utf-8
require 'bunny'
STDOUT.sync = true
conn = Bunny.new(host: 'rmq', user: 'guest', pass: 'guest')
conn.start
@igkuz
igkuz / consumer.rb
Created March 12, 2019 15:59
Ruby consumer for RabbitMQ queue with rate-limiting based on Redis
#!/usr/bin/env ruby
# encoding utf-8
require 'bunny'
require 'logger'
require 'redis'
logger = Logger.new(STDOUT)
STDOUT.sync = true
@igkuz
igkuz / publisher.rb
Created March 12, 2019 15:58
Ruby publisher for RabbitMQ queue with bunny gem
#!/usr/bin/env ruby
# encoding utf-8
require 'bunny'
require 'json'
STDOUT.sync = true
conn = Bunny.new(host: 'rmq', user: 'guest', pass: 'guest')
conn.start
@igkuz
igkuz / Gemfile
Last active March 12, 2019 15:08
Gemfile for bunny
source 'https://rubygems.org'
gem 'bunny'
gem 'redis'
@igkuz
igkuz / factories.rb
Created March 9, 2019 16:21
Define factories with rom-factory
let(:factories) do
ROM::Factory.configure do |config|
config.rom = MAIN_CONTAINER
end
end
before do
factories.define(:post) do |f|
f.url { fake(:internet, :url, 'example.com') }
f.se_post false
@igkuz
igkuz / create_company.rb
Created March 9, 2019 14:18
ROM Commands for Post and Company
class CreateCompany < ROM::Commands::Create[:sql]
relation :companies
register_as :create
result :one
use :timestamps
timestamp :created_at, :updated_at
end
@igkuz
igkuz / companies.rb
Created March 9, 2019 13:59
ROM Relations for Post & Company
class Companies < ROM::Relation[:sql]
schema(:companies, infer: true) do
associations do
has_many :posts
end
end
end
ROM::SQL.migration do
change do
create_table :companies do
primary_key :id
column :name, String, null: false
column :domain, String, null: false
column :state, String, null: false, default: 'running'
column :created_at, DateTime
column :updated_at, DateTime
end
@igkuz
igkuz / Rakefile
Created March 9, 2019 13:18
Rakefile for sample ROM app with db:setup
require_relative 'boot'
require 'rom/sql/rake_task'
namespace :db do
task :setup do
configuration = ROM::Configuration.new(:sql, "mysql2:///#{CONFIG['db']['name']}", CONFIG['db'])
ROM::SQL::RakeSupport.env = configuration
end
end
#!/usr/bin/env ruby
require_relative 'boot'
db_config = {
name: 'rom_app_development',
host: 'db',
user: 'root',
password: '',
port: 3306,
encoding: 'utf8mb4'