Skip to content

Instantly share code, notes, and snippets.

View dblock's full-sized avatar
🐝
Alexa, ask the future of America to be great again! (try it)

Daniel (dB.) Doubrovkine dblock

🐝
Alexa, ask the future of America to be great again! (try it)
View GitHub Profile
@dblock
dblock / new_relic_agent_instrumentation_api.rb
Created September 21, 2011 21:49
New Relic API Rackup
if Rails.env.production?
require 'new_relic/agent/instrumentation/controller_instrumentation'
module NewRelic
module Agent
module Instrumentation
module API
def newrelic_request_headers
@newrelic_request.env
@dblock
dblock / assets.rake
Created December 10, 2011 20:12
Upload assets to S3, preserve two most recent versions.
namespace :assets do
# uploads assets to s3 under assets/githash, deletes stale assets
task :uploadToS3, [ :to ] => :environment do |t, args|
from = File.join(Rails.root, 'public/assets')
to = args[:to]
hash = (`git rev-parse --short HEAD` || "").chomp
logger.info("[#{Time.now}] fetching keys from #{to}")
existing_objects_hash = {}
@dblock
dblock / oauth_controller.rb
Created December 11, 2011 15:16
An updated OAuth2 controller for a Rails app (implies you have ClientApplication and AccessGrant)
class OauthController < ApplicationController
class ApiOAuthError < StandardError
attr_accessor :code, :description, :uri, :state
def initialize(code, description, uri = nil, state = nil)
@code = code
@description = description
@uri = uri
@dblock
dblock / garb_model.rb
Created March 31, 2012 14:59
An iterator over all Google Analytics results with Garb
module Garb
module Model
def all(profile, options = {}, &block)
limit = options.delete(:limit)
total = 0
while ((rs = results(profile, options)) && rs.any?)
rs.each do |r|
yield r
total += 1
break if limit and total >= limit
@dblock
dblock / action_mailer_with_text_part.rb
Created May 17, 2012 14:57
Insert an automatic text MIME part into HTML e-mail.
#
# Insert an automatic text MIME part into HTML e-mail.
# (c) Daniel Doubrovkine, Art.sy 2012
# MIT License
#
class ActionMailerWithTextPart < ActionMailer::Base
def collect_responses_and_parts_order(headers)
responses, parts_order = super(headers)
html_part = responses.detect { |response| response[:content_type] == "text/html" }
@dblock
dblock / mongoid_criteria.rb
Created May 24, 2012 19:38
Mongoid::Criteria each_by iterator
module Mongoid
class Criteria
def each_by(by, &block)
idx = 0
total = 0
set_limit = options[:limit]
while ((results = ordered_clone.limit(by).skip(idx)) && results.any?)
results.each do |result|
return self if set_limit and set_limit >= total
@dblock
dblock / api_conditional_get_helper.rb
Created June 2, 2012 01:13
Conditional GET helper for a RESTful API
# Written by https://github.com/macreery (c) Art.sy, 2012, MIT License
module ApiConditionalGetHelper
# Borrows generously from http://themomorohoax.com/2009/01/07/using-stale-with-rails-to-return-304-not-modified
# See also RFC 2616: http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.4
# for explanation of how If-Modified-Since and If-None-Match request headers are handled.
def fresh?(metadata = {})
self.last_modified = metadata[:last_modified]
self.etag = metadata[:etag]
@dblock
dblock / api_cache.rb
Created June 19, 2012 13:29
Api Cache Helper
# Written (mostly) by https://github.com/macreery (c) Art.sy, 2012, MIT License
module ApiCache
# cache wrapper, yields an executable block
def self.cache(options = {})
# options set default expiration time and force a miss if specified
options = standardize_options(options)
cache_options = options[:cache_options] || {}
cache_options[:expires_in] = 24.hours if not cache_options[:expires_in]
@dblock
dblock / date_helper.rb
Created June 29, 2012 15:42
Date ranges, typically for shows ...
module ActionView
module Helpers
module DateHelper
# returns a date range in English
# July 3-5, both days are the same month
# July 3 - August 5, days are different months
# December 20, 2010 - January 10, 2012, days are over a year apart
def date_range_in_words(start_date, end_date)
# convert to date
start_date = start_date.to_date if start_date && start_date.respond_to?(:to_date)
@dblock
dblock / mongoid_criteria.rb
Created July 15, 2012 00:11
Returning a sample set from a Mongoid collection.
module Mongoid
class Criteria
def sample(n = 1)
indexes = (0..self.count-1).sort_by{ rand }.slice(0,n).collect!
if n == 1
return self.skip(indexes.first).first
else
return indexes.map{ |index| self.skip(index).first }
end