Skip to content

Instantly share code, notes, and snippets.

@samsondav
samsondav / gist:743d6b8e4d7c4d462bdb
Last active August 29, 2015 14:07
Ruby script to remove all twitter following
require 'twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key = "xxx"
config.consumer_secret = "yyy"
config.access_token = "123-zzz"
config.access_token_secret = "www"
end
@samsondav
samsondav / gist:d8c242050c4b3bdb2566
Last active August 29, 2015 14:10
Recursive URL looker-upper
# recursively looks up a URL and returns a unicode
def self.lookup_url(uri_str, limit = 10, old_uri = nil)
raise ArgumentError, 'too many HTTP redirects' if limit == 0
response = Net::HTTP.get_response(URI(uri_str))
case response
when Net::HTTPSuccess then
return self.unicode_parse(old_uri)
when Net::HTTPRedirection then
location = response['location']
@samsondav
samsondav / gist:e1951194a287e46cbda2
Created December 4, 2014 19:26
Tokenize an abitrary Spanish string into words
def self.tokenize_spanish(text)
text = text.sub(/(https?:\/\/[\S]*[$\z\s])/i, '') # strip urls
words = text.scan(/[\w@#ñÑáÁéÉíÍóÓúÚü]+/) # tokenize into words
words.map! {|word| word.downcase} # lowercase everything
words.reject! {|word| word.length < 4} # reject words with 3 or fewer characters
return words
end
@samsondav
samsondav / gist:a47c687b665c51844708
Last active August 29, 2015 14:11
Asynchronously handle tweet queue for twitter-streaming-client
;; create the client with a twitter.api streaming method
(def stream (twitter-client/create-twitter-stream twitter.api.streaming/statuses-filter
:oauth-creds twitter-creds
:params {:follow user-id}))
(defn create-tweet-entities [twitter-tweet]
; the entirety of this method should be wrapped in a database transaction
(insert stream-tweets
(values (twitter-tweet-to-stream-tweet twitter-tweet))))
@samsondav
samsondav / anti-keygrabber.user.js
Last active August 29, 2015 14:14 — forked from rodneyrehm/anti-keygrabber.user.js
DISABLE binding of some key combinations to web apps, especially Gmail's cmd-enter to send 'feature'
// ==UserScript==
// @name anti key-grabber
// @description Prevent web apps from capturing and muting vital keyboard shortcuts
// @grant none
// @version 1.1
// ==/UserScript==
(function(){
var isMac = unsafeWindow.navigator.oscpu.toLowerCase().contains("mac os x");
unsafeWindow.document.addEventListener('keydown', function(e) {
if (e.keyCode === 116) {
@samsondav
samsondav / gist:616627066d593a1275cc
Created June 6, 2015 17:17
Extract DATABASE_URL environment variable for app running inside Apache Tomcat
(import [javax.naming Context InitialContext])
(def development-db-string "postgresql://localhost:5432/foobar_db")
(def db-uri
(atom
(try
(. (. (InitialContext.) lookup "java:comp/env") lookup "DATABASE_URL") ; will pull environment variables from a Tomcat server
(catch javax.naming.NoInitialContextException e
(or
@samsondav
samsondav / configurable.rb
Last active August 29, 2015 14:23
Rails configuration model with JSON column
# A helper module to be included in any class that requires a configuration
# hash.
#
# The rationale is that after a while, adding configuration options as columns
# on a model becomes unwieldy, especially if there are very many configuration
# options but only a few used by each record, this results in lots of nil columns
# and is not very flexible. Also it requires migration the database for each new
# option.
#
# This module comes with a method called configuration_accessors that defines virtual attributes
require 'cases/helper'
class HotCompatibilityTest < ActiveRecord::TestCase
self.use_transactional_tests = false
ActiveRecord::Base.logger = Logger.new(STDOUT)
class ::SolarSystem < ActiveRecord::Base
has_many :solid_planets, -> {where(classification: "terrestrial")}, class_name: 'Planet'
end
@samsondav
samsondav / hot_compatibility.rb
Last active November 1, 2015 21:13
hot_compatiblity.rb
require 'cases/helper'
require 'support/connection_helper'
class HotCompatibilityTest < ActiveRecord::TestCase
self.use_transactional_tests = false
include ConnectionHelper
setup do
@klass = Class.new(ActiveRecord::Base) do
connection.create_table :hot_compatibilities, force: true do |t|
@samsondav
samsondav / database_comment.rb
Created July 2, 2016 12:43
Rails read Postgres database column comments
# frozen_string_literal: true
# A tiny class for reading column database comments
class DatabaseComment
DATABASE_NAME = Rails.configuration.database_configuration[Rails.env]['database'].freeze
# Reads back a postgres comment based on table_name and column_name
# Sanitizes inputs but it would be better not to allow user input strings here regardless
def self.read(table_name, column_name)
unless table_name.is_a?(Symbol) && column_name.is_a?(Symbol)
raise TypeError, <<~STERN_WARNING