Skip to content

Instantly share code, notes, and snippets.

☂️
Messing around.

John Norman jgn

☂️
Messing around.
Block or report user

Report or block jgn

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@jgn
jgn / opendns.rb
Created Apr 27, 2018
Block via OpenDNS
View opendns.rb
require 'mechanize'
require 'set'
require 'pry'
ACCOUNT = ENV['OPENDNS_ACCOUNT']
USERNAME = ENV['OPENDNS_USERNAME']
PASSWORD = ENV['OPENDNS_PASSWORD']
BLOCK_LIMIT = 25
AGENT_NAME = 'Mac Safari'
View git-hash-blob-object
#!/usr/bin/env ruby
# from https://stackoverflow.com/posts/24458934/revisions
require 'digest/sha1'
def git_hash(filename)
data = File.read(filename)
Digest::SHA1.hexdigest("blob #{data.bytesize.to_s}\0#{data}")
end
@jgn
jgn / session.irb
Last active Feb 3, 2016
Getting access to a view in AR
View session.irb
irb
irb(main):001:0> require 'active_record'
=> true
irb(main):002:0> ActiveRecord::Base.establish_connection(adapter: :postgresql, database: :view_demo)
=> #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x007ffc83b088d0 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007ffc83b099d8>, @spec=#<ActiveRecord::ConnectionAdapters::ConnectionSpecification:0x007ffc84134d08 @config={:adapter=>:postgresql, :database=>:view_demo}, @adapter_method="postgresql_connection">, @checkout_timeout=5, @reaper=#<ActiveRecord::ConnectionAdapters::ConnectionPool::Reaper:0x007ffc83b09c58 @pool=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x007ffc83b088d0 ...>, @frequency=nil>, @size=5, @reserved_connections=#<ThreadSafe::Cache:0x007ffc83b0b440 @backend={}, @default_proc=nil>, @connections=[], @automatic_reconnect=true, @available=#<ActiveRecord::ConnectionAdapters::ConnectionPool::Queue:0x007ffc83b03f10 @lock=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x007ffc83b088d0 ...>, @cond=#<MonitorMixin::ConditionVariab
View arbritrary_ddl.rb
require 'active_record'
spec = { adapter: :postgresql, database: :jgn }
ActiveRecord::Base.establish_connection(spec)
if ActiveRecord::Base.connection.table_exists?(:members)
ActiveRecord::Base.connection.drop_table :members
puts 'Dropped table'
end
View blacklist.rb
require 'active_record'
blacklist = {
icis_production: [ 'patient_versions', 'patients' ],
snowflake_production: [ 'persons' ]
}
blacklist.each_pair do |database, blacklisted_tables|
spec = {
adapter: :postgresql,
@jgn
jgn / get_metadata.rb
Last active Jan 20, 2016
Get table, column data
View get_metadata.rb
# this is how you bring in a gem (a module). The gem should previous be
# installed with: gem install activerecord (yes, the gem name and what
# "require" are different.)
require 'active_record'
# This is a hash, like a dictionary in Python.
# The classic syntax looks like this:
# spec = { 'adapter' => 'postgresql', 'database' => 'icis_production' }
# However, the tokens preceded with the colon are actually symbols, where
# are like strings but they can't be changed. So, the following hash
View addr0-with-mock.rb
class UnexpectedOutputError < StandardError; end
class MockLogger
def initialize(expected_indirect_outputs)
@eio = Array(expected_indirect_outputs)
end
def info(s)
expected = @eio.shift
if expected != s
raise UnexpectedOutputError.new("Fail: got #{s}; expected #{expected}")
end
View adder0-with-spy-leveraging-logger.rb
require 'logger'
def get_logger
logger = Logger.new(STDOUT)
def logger.info(s)
@log_output ||= ""
@log_output << s << "\n"
end
def logger.log_output
@log_output
end
View adder0-with-spy.rb
class Logger
attr_reader :log_output
def info(s)
@log_output ||= ""
@log_output << s << "\n"
end
end
examples.each do |example|
a, b, c, s = example
View adder0-with-logger-and-stub.rb
class HalfAdder
attr_reader :sum, :carry
def initialize(augend, addend, logger)
@augend, @addend, @logger = augend, addend, logger
end
def compute
@carry = @augend & @addend
@sum = @augend ^ @addend
You can’t perform that action at this time.