Skip to content

Instantly share code, notes, and snippets.

View dmichael's full-sized avatar

David Michael dmichael

View GitHub Profile
module DickCheney
def self.speak
puts "meeeh..."
end
end
DickCheney.speak
# => meeeh...
# From the OpenURI documentation
open("http://www.ruby-lang.org/en") {|f|
f.each_line {|line| p line}
p f.base_uri # <URI::HTTP:0x40e6ef2 URL:http://www.ruby-lang.org/en/>
p f.content_type # "text/html"
p f.charset # "iso-8859-1"
p f.content_encoding # []
p f.last_modified # Thu Dec 05 02:45:02 UTC 2002
}
# From the RestClient documentation
RestClient.get 'http://example.com/resource'
RestClient.get 'https://user:password@example.com/private/resource'
RestClient.post 'http://example.com/resource', :param1 => 'one', :nested => { :param2 => 'two' }
RestClient.delete 'http://example.com/resource'
# ActiveResource-Style
begin
response = RestClient.get("http://cupcakestastegood.com")
# Net::HTTP connection error
rescue Errno::ECONNREFUSED => error
p "This error has no response body: #{e}"
# RestClient exceptions
rescue RestClient::Exception => error_response
p "This error is actually a response body"
# Both of these examples are taken from John's examples
require 'httparty'
# Basic usage
# You can also use post, put, delete in the same fashion
response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
puts response.body, response.code, response.message, response.headers.inspect
require 'rubygems'
require 'httparty'
# authority = [ userinfo "@" ] host [ ":" port ]
# http://labs.apache.org/webarch/uri/rfc/rfc3986.html#authority
module HTTPartyHarder
attr_accessor :authority
def authority
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/249436
class Array
def each_cycle(window, start=0)
(start...length+start).each do |i|
yield((i..i+window).map {|n| self[n % length]})
end
end
end
# Something a little more complicated from
# http://blog.craigambrose.com/articles/2007/03/01/a-rake-task-for-database-backups
# http://edge.matrixprojects.com/2008/5/21/backing-up-your-rails-mysql-db-via-a-rake-task
require 'find'
namespace :db do desc "Backup the database to a file. Options: DIR=base_dir RAILS_ENV=production MAX=20"
task :backup => [:environment] do
datestamp = Time.now.strftime("%Y-%m-%d_%H-%M-%S")
base_path = ENV["DIR"] || "db"
backup_base = File.join(base_path, 'backup')
# There really is no need for this class. One could use java.util.Properties in JRuby,
# but figuring out how to use it from JRuby was more cumbersome than just writing this class.
# Parser stolen from
# http://devender.wordpress.com/2006/05/01/reading-and-writing-java-property-files-with-ruby/
class JavaProperties < Hash
attr_accessor :file
# Takes a file and loads the properties in that file
#!/bin/sh
LC_CTYPE=en_US.UTF-8
SVN=`which svn`
echo Changing to Bundles directory...
mkdir -p /Library/Application\ Support/TextMate/Bundles
cd /Library/Application\ Support/TextMate/Bundles
if [ -d /Library/Application\ Support/TextMate/Bundles/ Erlang.tmbundle ]; then