Skip to content

Instantly share code, notes, and snippets.

#!/bin/bash
CHARGE_LEVEL=${1:-80}
while true
do
battery_level=`acpi -b | grep -P -o '[0-9]+(?=%)'`
if [ ! $battery_level ]; then
echo 'No battery found'
exit
else
status=`cat /sys/class/power_supply/BAT1/status`
require 'set'
# copy a string to the clipboard
def cp(string = IRB.CurrentContext.last_value)
IO.popen('xclip -sel clip', 'w') { |f| f << string.to_s }
string
end
if ARGV[0] && ARGV[0][0].downcase == 't'
@lines = "".split("\n")
elsif ARGV[0] && ARGV[0][0].downcase == 'c'
@imme5150
imme5150 / heroku_db_import.sh
Created November 5, 2014 10:27
Automatically capture, download and import a postgres DB from Heroku. You can supply an argument to the script to specify the app to use like this: `heroku_db_import.sh -amyapp` It reads the username and database name from config/database.yml
heroku pgbackups:capture $1 --expire
if [ "$?" == 0 ]
then
rake db:drop db:create
curl -o heroku_db_backup.dump `heroku pgbackups:url $1`
pg_restore --verbose --clean --no-acl --no-owner -h localhost `ruby -e "require 'yaml';db = YAML::load(File.read 'config/database.yml')['development']; puts ' -U ' + (db['username'] || 'postgres') + ' -d ' + db['database']"` heroku_db_backup.dump
rm heroku_db_backup.dump
fi
@imme5150
imme5150 / payment_and_shipment_decorator.rb
Created January 21, 2014 07:12
Partial payments for shipments in Spree 1.1.0 - only for credit card payments and shipping is applied only to the first shipment.
module Spree
Payment.class_eval do
def complete_or_partial_capture!(amount)
return false if amount <= 0 || self.state != 'pending'
# if it's close to total payment, round up or down to total payment
# this is to counteract rounding errors from tax calculations
amount = self.amount if ((amount-0.02)..(amount+0.02)).include?(self.amount)
# check to make sure there is enough money authorized for this payment
return false if amount > self.amount
if amount < self.amount # split the payment
@imme5150
imme5150 / any_of.rb
Last active January 3, 2016 00:19 — forked from oelmekki/any_of.rb
# Add this to an initializer file to extend ActiveRecord
# You can pass in an array of hashes. The key/values of your hashes will be joined w/ ANDs
# The hashes in the array will be joined w/ ORs
# You can also pass in an array of strings or arrays
class ActiveRecord::Base
def self.any_of( queries )
queries = queries.map do |query|
query = where( query ) if [ String, Hash ].any? { |type| query.kind_of? type }
query = where( *query ) if query.kind_of?( Array )
@imme5150
imme5150 / generate_refresh_token.rb
Created October 16, 2013 19:55
A script to generate an offline refresh token for Google API access
#!/usr/bin/env ruby
# this is pretty rough, but I hope it helps!
#
# if you run into any problems, this is a big help:
# https://developers.google.com/oauthplayground/
require 'net/http'
require 'json'
@imme5150
imme5150 / .irbrc
Created September 13, 2013 09:12
This is my current irbrc file. Lots of helpful things I've collected over the years. Enjoy
script_console_running = ENV.include?('RAILS_ENV') && IRB.conf[:LOAD_MODULES] && IRB.conf[:LOAD_MODULES].include?('console_with_helpers')
if script_console_running # log SQL for Rails 2
require 'logger'
Object.const_set(:RAILS_DEFAULT_LOGGER, Logger.new(STDOUT))
end
# log SQL for Rails 3
ActiveRecord::Base.logger = Logger.new(STDOUT) if defined? Rails::Console
require 'bigdecimal'
@imme5150
imme5150 / gist:6083600
Last active December 20, 2015 06:09 — forked from 1o1brian/gist:2919347
# thanks to railscasts:
# http://railscasts.com/episodes/48-console-tricks-revised?view=comments
# add this to your ~/.irbrc file
class Object
# look up source location of a method
def sl(method_name)
self.method(method_name).source_location