Skip to content

Instantly share code, notes, and snippets.

View temochka's full-sized avatar
🪒
🐃

Artem Chistyakov temochka

🪒
🐃
View GitHub Profile

QueueingRabbit Build Status Code Climate

QueueingRabbit provides a Ruby DSL to interact with RabbitMQ. It is fairly flexible and allows you to integrate with existing infrastructure and naming conventions. It currently offers gems bunny and amqp as supported back-ends.

Example

The following Ruby program publishes an excerpt of Joseph Brodsky’s poem line by line to a RabbitMQ exchange and prints received messages on the screen.

require 'queueing_rabbit'
require 'postmark'
client = Postmark::ApiClient.new('API_TOKEN')
puts client.bounces.
first(10).
map { |b| b.values_at(:email, :name, :inactive) }.
unshift(["Email Address", "Bounce Type", "Is inactive?"]).
map(&:to_csv).
join
@temochka
temochka / bounce_dump.rb
Last active August 29, 2015 14:04
Parsing a raw Postmark bounce dump. Ruby 2.0.0, mail 2.5.4, postmark 1.1.0
require 'mail'
require 'postmark'
client = Postmark::ApiClient.new('xxxx-xxxx-xxxx-xxxx')
bounce = client.get_bounces(offset: 0, count: 1).first
dump = client.dump_bounce(bounce[:id])
msg = Mail::Message.new(dump[:body])
headers = Hash[*msg.header.fields.flat_map { |f| [f.name, f.value] }]
# => {"Return-Path"=>"", "Date"=>"Tue, 29 Jul 2014 05:50:48 -0400", "From"=>"", "To"=>"", "Message-ID"=>"", "Subject"=>"Delivery report", "Mime-Version"=>"1.0", "Content-Type"=>"multipart/report; report-type=delivery-status; boundary=\"\"", "Content-Transfer-Encoding"=>"7bit", "X-Your-Custom-Header"=>"Value"}
@temochka
temochka / pack_number.clj
Created August 7, 2014 13:44
A quick & dirty number to string packing implementation
(import '[java.util Random])
(def ^:const seed 42)
(def ^Random shuffler (Random. seed))
(def alphabet (->> "abcdefghijklmnopqrstuvwxyz0123456789"
(sort-by (fn [_] (.nextInt shuffler)))
vec))
(def N (count alphabet))
(defn pack-number
([n] (pack-number n ""))
([n buf]
@temochka
temochka / euler.erl
Last active August 29, 2015 14:06
Solving Project Euler’s problems in Erlang. Most of the proposed solutions are very naive and inefficient and therefore only viable for educational purposes (if there were any other).
-module(euler).
-compile(export_all).
%% 1
%% Find the sum of all the multiples of 3 or 5 below 1000.
problem1() ->
lists:sum([I || I <- lists:seq(0,999), I rem 3 =:= 0 orelse I rem 5 =:= 0]).
%% 2
@temochka
temochka / mail.rb
Created December 21, 2014 06:35
Oh, wait, since you’ve looked, I’m a text now. Heard of quantum mechanics?
require 'mail'
msg = Mail.new { body 'Text' }
# => #<Mail::Message:70116771523620, Multipart: false, Headers: >
msg.text?
# => false
puts msg
# Date: Sun, 21 Dec 2014 01:29:26 -0500
# Message-ID: <549668c6e2caa_33e13fc554c65be84831f@Artems-MacBook-Pro.local.mail>
# Mime-Version: 1.0
class EmailBatch
attr_reader :messages
delegate :<<, to: :messages
def initialize(messages = [])
@messages = messages
end
def deliver_now
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" .vimrc
"
" Artem Chistyakov <chistyakov.artem@gmail.com>
"
" Remember that:
" Reload .vimrc without restarting Vim
" :source %
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
require 'tmail'
require 'postmark'
module Mailer
class PostmarkTransport
attr_accessor :content_type
def initialize(api_key)
Postmark.api_key= api_key
end
post '/contact' do
subject = params[:title]
body = erb :email, :layout => false
begin
transport = Mailer::PostmarkTransport.new(POSTMARK_API_KEY)
transport.send_message(SENDER, RECIPIENT, subject, body) do |message|
message.tag = "contact-form"
end
rescue ArgumentError