Skip to content

Instantly share code, notes, and snippets.

View dpaluy's full-sized avatar

David Paluy dpaluy

  • Majestic Labs
  • Austin, TX
  • X @dpaluy
View GitHub Profile
@dpaluy
dpaluy / messages_spec.rb
Last active August 29, 2015 14:26
JSON Helper for API specs
# spec/requests/api/v1/messages_spec.rb
describe "Messages API", type: :request do
it 'retrieves a specific message' do
message = FactoryGirl.create(:message)
get "/api/v1/messages/#{message.id}"
# test for the 200 status-code
expect(response).to be_success
# check that the message attributes are the same.
@dpaluy
dpaluy / appication.rb
Created August 26, 2015 00:14
Solving CORS with rack-cors gem
class Application < Rails::Application
# ...
config.middleware.insert_before Warden::Manager, Rack::Cors do
allow do
origins %r{^https?:\/\/[a-z0-9\-]+.yourdomain.com:?\d*$}i
resource '*',
headers: :any,
methods: [:get, :post, :put, :create, :delete, :options]
@dpaluy
dpaluy / ga.html.erb
Created October 4, 2015 17:57
Google Analytics with Turbolinks
<% if Rails.env.production? %>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXX-X', 'auto');
//ga('send', 'pageview');
</script>
@dpaluy
dpaluy / time_to_decimal.rb
Created November 2, 2015 08:14
Time to decimal convert
def time_to_decimal(duration)
if duration =~ /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/
time_pieces = duration.split(":")
hours = time_pieces[0].to_i
minutes = (time_pieces[1].to_f/60.0)
# Final value ready
(hours + minutes).round(2)
end
end
@dpaluy
dpaluy / enumerable.rb
Last active November 25, 2015 15:16
Sorting an Array by Frequency of Appearance
module Enumerable
def to_histogram
inject(Hash.new(0)) { | h, x | h[x] += 1; h }
end
def sort_by_frequency
histogram = inject(Hash.new(0)) { | hash, x | hash[x] += 1; hash }
sort_by { | x | [histogram[x], x] }
end
@dpaluy
dpaluy / pipelined_example.rb
Last active May 23, 2020 15:58
Pipeling Multiple REDIS Commands in Ruby
Pipeliner.pipeline redis do |pipe|
objects.each do |object|
pipe.enqueue redis.hgetall(object.key) do |result|
object.values = result
end
end
end
@dpaluy
dpaluy / threads.rb
Created December 11, 2015 19:42
Multi-threaded Queue in Ruby
num_workers = 20
queue = SizedQueue.new num_workers * 2
threads = num_workers.times.map do
Thread.new do
while item = queue.pop
#do_something item
end
end
end
list.each {|item| queue << item}
@dpaluy
dpaluy / pg_stat_statements.md
Last active January 1, 2016 21:48
pg_stat_statements for where to index

Once enabled, it automatically records all queries run against your database and records often and how long they took.

Enable: create extension pg_stat_statements

Note: it has a performance cost to leaving this always on, but it’s pretty small.

SELECT 
  (total_time / 1000 / 60) as total_minutes, 
 (total_time/calls) as average_time, 
@dpaluy
dpaluy / cache_counter_service.rb
Created March 20, 2016 09:37
Recalculate counter cache columns in Rails
class CacheCounterService
def initialize
@id = SecureRandom.hex(6)
end
def call
# Make sure to load all models first
Rails.application.eager_load!
ActiveRecord::Base.descendants.each do |many_class|
@dpaluy
dpaluy / .pryrc
Last active April 18, 2021 04:04
pryrc settings with AwesomePrint
if defined?(PryByebug)
Pry.commands.alias_command 'con', 'continue'
Pry.commands.alias_command 's', 'step'
Pry.commands.alias_command 'n', 'next'
Pry.commands.alias_command 'f', 'finish'
end
begin
require 'awesome_print'