Skip to content

Instantly share code, notes, and snippets.

View ridiculous's full-sized avatar

Ryan Buckley ridiculous

  • California
View GitHub Profile
Rails::Rack::Logger.class_eval do
def started_request_message_with_no_ip(request)
'Started %s "%s" at %s' % [
request.request_method,
request.filtered_path,
Time.now.to_default_s]
end
alias_method_chain :started_request_message, :no_ip
end
@ridiculous
ridiculous / app_reloader.rb
Last active August 29, 2015 14:21
File reloader middleware
module API
module Middleware
class AppReloader
class << self
def mtimes
@mtimes ||= {}
end
def files
@ridiculous
ridiculous / lean_application.rb
Last active August 29, 2015 14:20
Lean Rails Application
# config/lean_application.rb
#
# Require and run only a part of your Rails app (maybe as a background process)
#
require File.expand_path('../boot', __FILE__)
require 'active_record/railtie'
require 'action_mailer/railtie'
@ridiculous
ridiculous / intercom.js
Created March 31, 2015 22:57
intercom.io
s = $document[0].createElement('script')
s.type = 'text/javascript'
s.async = true
s.src = "https://widget.intercom.io/widget/#{chat_settings.app_id}"
tags = $document[0].getElementsByTagName('script')
x = tags[tags.length - 1]
x.parentNode.insertBefore(s, x)
class DummyController
include Logging
protected
def log_file
'log_file_name'
end
end
USER_AGENT_ALIASES = ['Windows Mozilla', 'Mac Safari', 'Mac FireFox', 'Mac Mozilla', 'Linux Mozilla', 'Linux Firefox']
agent = Mechanize.new do |mech|
mech.open_timeout = 10
mech.read_timeout = 10
mech.follow_meta_refresh = true
mech.keep_alive = true
mech.max_history = 1
mech.user_agent_alias = USER_AGENT_ALIASES.sample
end
agent.set_proxy(proxy_ip, proxy_port, AppConfig.http_proxy.username, AppConfig.http_proxy.password)
class Merchant
include Cequel::Record
key :user_id, :int
key :source, :text
key :source_id, :text
column :name, :text
# @example
# Merchant.by_key(user_id = 1, source = 'facebook', source_id = '4213578641d')
module HashieMashExtensions
# To play nice with ActiveSupport Array#extract_options!
def extractable_options?
true
end
end
Hashie::Mash.send(:include, HashieMashExtensions)
@ridiculous
ridiculous / notes.rb
Last active August 29, 2015 14:11
Notes + operator
def +(other)
new_data = data.merge(other.data) do |_, val, other_val|
case val
when Array, Integer
val + other_val
when Hash
val.merge(other_val) { |__, val1, val2| val1 + val2 }
else
nil
end
@ridiculous
ridiculous / object_tracker.rb
Last active June 25, 2019 09:47
Method tracking. Could be used to unobtrusively track method calls
module ObjectTracker
def track(*args)
@__tracking ||= []
args.each { |arg| @__tracking << arg unless tracking?(arg) }
end
def track!(*args)
track(*args)
start_tracking!
end