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 / 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 / ActiveDirectoryDecoratorFilter.java
Created June 27, 2012 13:31
Obtain additional user information from ActiveDirectory using Com4J
//
// from http://waffle.codeplex.com/workitem/10034
//
package waffle.servlet.spi;
import java.io.IOException;
import java.security.Principal;
import java.util.Date;
import java.util.Enumeration;
@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
@dblock
dblock / delayed_job_observer.rb
Created August 16, 2012 13:21
A mongoid delayed job observer, used in test environment to kick off any delayed jobs as they are queued.
class DelayedJobObserver < Mongoid::Observer
observe Delayed::Job
class << self
attr_accessor :total_processed
attr_accessor :total_errors
attr_accessor :enabled
def enabled?
!! DelayedJobObserver.enabled
@dblock
dblock / mongoid_collection.rb
Created October 29, 2012 16:52
A monkey patch for detecing nil IDs.
require 'mongoid/collection'
module Mongoid
class Collection
alias_method :_update, :update
def update(selector, document, options = {})
document.deep_fetch("_id").each do |value|
if value.nil?
Rails.logger.error "Invalid nil _id in #{document}."
@dblock
dblock / mongoid.yml
Created November 9, 2012 14:24
MongoHQ configuration for Mongoid 3.x
development:
sessions:
default:
database: mydb_development
hosts:
- localhost:27017
options:
safe: true
test:
@dblock
dblock / mongoid_slug_unique_slug.rb
Created November 22, 2012 15:39
Monkey-patch mongoid-slug to allow slug = _id
module Mongoid
module Slug
class UniqueSlug
alias_method :_find_unique, :find_unique
# by default Mongoid::Slug returns id-1 if you try to get a slug from the model's ID
def find_unique attempt = nil
if ! attempt
candidate = url_builder.call(model)
@dblock
dblock / newrelic.yml
Created November 29, 2012 17:11
NewRelic RPM configuration
# Here are the settings that are common to all environments
common: &default_settings
# ============================== LICENSE KEY ===============================
# You must specify the license key associated with your New Relic
# account. This key binds your Agent's data to your account in the
# New Relic service.
license_key: '<%= ENV["NEW_RELIC_LICENSE_KEY"] %>'
# Agent Enabled (Rails Only)