Skip to content

Instantly share code, notes, and snippets.

View temochka's full-sized avatar
🪒
🐃

Artem Chistyakov temochka

🪒
🐃
View GitHub Profile
@temochka
temochka / jaccard.clj
Created January 17, 2014 03:35
Jaccard Measure
(defn setify-str [str]
(apply hash-set (seq str)))
(defn jaccard [set1 set2]
(/ (count (clojure.set/intersection set1 set2))
(count (clojure.set/union set1 set2))))
(defn jaccard-str [str1 str2]
(apply jaccard (map setify-str [str1 str2])))
(defn trie-leaves [& args]
(start-root (map seq args)))
(defn start-root [seqs]
(map
(fn [group]
(if (< 1 (count group))
(start-root (map rest group))
(seq group)))
(vals (group-by first seqs))))

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 / postmark_rails_headers.rb
Created April 29, 2014 14:39
An example of sending custom headers with postmark-rails and ActionMailer
class TestMailer < ActionMailer::Base
def email
headers['X-Tag-Via-Hash-Access'] = 'value'
headers 'X-Tag-Via-Headers-Method' => 'value'
mail(to: 'support@postmarkapp.com', from: 'tema@wildbit.com', subject: 'Headers test') do |format|
format.text { render text: 'Headers test' }
end
@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
@temochka
temochka / cloudfront.json
Last active October 7, 2015 20:29
IAM policy to deploy to S3 subdirectory via Beanstalk/Dploy
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1422638998000",
"Effect": "Allow",
"Action": [
"cloudfront:CreateInvalidation",
"cloudfront:GetDistribution"
],