<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Jnewland's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 16 May 2008 15:03:26 GMT</pubDate>
    <description>DZone Snippets: Jnewland's Code Snippets</description>
    <item>
      <title>Restore a single table from a large MySQL backup</title>
      <link>http://snippets.dzone.com/posts/show/4819</link>
      <description>Say, for some reason, you need to restore the entire contents of a single table from a HUGE mysqldump generated backup containing several tables. For example:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;create table `baz`;&lt;br /&gt;&lt;br /&gt;GIGS OF SQL YOU DON'T WANT;&lt;br /&gt;&lt;br /&gt;create table `foo`;&lt;br /&gt;&lt;br /&gt;A COUPLE THOUSAND LINES YOU DO WANT;&lt;br /&gt;&lt;br /&gt;create table `bar`;&lt;br /&gt;&lt;br /&gt;MORE SQL YOU DON'T WANT;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;With a little dash 'o ruby, you can extract just the part you want:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$ ruby -ne '@found=true if $_ =~ /^CREATE TABLE `foo`/i; next unless @found; exit if $_ =~ /^CREATE TABLE (?!`foo`)/i; puts $_;' giant_sql_dump.sql &gt; foo.sql&lt;br /&gt;$ cat foo.sql&lt;br /&gt;create table `foo`;&lt;br /&gt;&lt;br /&gt;A COUPLE THOUSAND LINES YOU DO WANT;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;You can then easily restore that entire table:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$ mysql mydatabase -e 'drop table foo'&lt;br /&gt;$ mysql mydatabase &lt; foo.sql&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 28 Nov 2007 14:10:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4819</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>open_body_tag</title>
      <link>http://snippets.dzone.com/posts/show/4726</link>
      <description>&lt;code&gt;&lt;br /&gt;  #creates a body tag uniquely indentifying this page&lt;br /&gt;  #takes an options Hash with two keys:&lt;br /&gt;  #&lt;br /&gt;  #&lt;tt&gt;id&lt;/tt&gt;::        string that will be used as the body's ID. defaults to &lt;tt&gt;controller.controller_name.singularize&lt;/tt&gt;&lt;br /&gt;  #&lt;tt&gt;classes&lt;/tt&gt;::   an Array of class names. defaults to &lt;tt&gt;[params[:action]]&lt;/tt&gt;&lt;br /&gt;  #&lt;br /&gt;  #Examples:&lt;br /&gt;  #&lt;br /&gt;  # in HomeController#index:&lt;br /&gt;  #&lt;br /&gt;  # &lt;%= open_body_tag %&gt;&lt;br /&gt;  # =&gt; &lt;body id='home' class='index'&gt;&lt;br /&gt;  #&lt;br /&gt;  # &lt;%= open_body_tag(:id =&gt; 'foo') %&gt;&lt;br /&gt;  # =&gt; &lt;body id='foo' class='index'&gt;&lt;br /&gt;  #&lt;br /&gt;  # &lt;%= open_body_tag(:id =&gt; 'foo', :classes =&gt; %w(one two)) %&gt;&lt;br /&gt;  # =&gt; &lt;body id='foo' class='one two'&gt;&lt;br /&gt;  def open_body_tag(options = { :id =&gt; controller.controller_name.singularize, :classes =&gt; [params[:action]] })&lt;br /&gt;    "&lt;body id='#{options[:id]}' class='#{options[:classes].join(' ')}'&gt;"&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 01 Nov 2007 19:24:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4726</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>Test Trackback using cURL</title>
      <link>http://snippets.dzone.com/posts/show/4540</link>
      <description>I've Googled this about 20 times - documenting it here for convenience.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;curl -d url=TRACKBACK_URL -d title=TRACKBACK_TITLE -d blog_name=TRACKBACK_BLOG_NAME -d excerpt=TRACKBACK_EXCERPT URL&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;A correct response:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;br /&gt;&lt;response&gt;&lt;br /&gt;&lt;error&gt;0&lt;/error&gt;&lt;br /&gt;&lt;/response&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 14 Sep 2007 01:31:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4540</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>Rails URL Validation</title>
      <link>http://snippets.dzone.com/posts/show/4532</link>
      <description>No regexes, allows URLs with ports or IPs. Inspiration from &lt;a href="http://actsasblog.wordpress.com/2006/10/16/url-validation-in-rubyrails/"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  validates_each :href, :on =&gt; :create do |record, attr, value|&lt;br /&gt;    begin&lt;br /&gt;      uri = URI.parse(value)&lt;br /&gt;      if uri.class != URI::HTTP&lt;br /&gt;        record.errors.add(attr, 'Only HTTP protocol addresses can be used')&lt;br /&gt;      end&lt;br /&gt;    rescue URI::InvalidURIError&lt;br /&gt;      record.errors.add(attr, 'The format of the url is not valid.')&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 12 Sep 2007 14:03:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4532</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>Mofo - Parse Microformats with Ruby</title>
      <link>http://snippets.dzone.com/posts/show/3750</link>
      <description>&lt;code&gt;&lt;br /&gt;$ sudo gem install mofo &lt;br /&gt;Successfully installed mofo-0.2.1&lt;br /&gt;$ irb -rubygems&lt;br /&gt;&gt;&gt; require 'mofo'&lt;br /&gt;=&gt; true&lt;br /&gt;&gt;&gt; HResume.find("http://resume.jnewland.com").tags.uniq.sort&lt;br /&gt;=&gt; ["AJAX", "Apache", "CSS", "Capistrano", "DNS", "GNU/Linux", "HTML", "Javascript", "LAMP", "Mongrel", "Movable Type", "MySQL", "PHP", "Perl", "REST", "RSS", "Ruby", "Ruby on Rails", "SEO", "XHTML", "XML", "XSLT", "high availability", "lighttpd", "load-balanced"]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 30 Mar 2007 16:58:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3750</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>RSS Twitter Bot</title>
      <link>http://snippets.dzone.com/posts/show/3714</link>
      <description>Republish an RSS feed on a twitter account. This was the source I used to run the &lt;a href="http://twitter.com/woot"&gt;Woot Twitter Bot&lt;/a&gt; before they took it over.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'rubygems'&lt;br /&gt;require 'active_record'&lt;br /&gt;require 'simple-rss'&lt;br /&gt;require 'open-uri'&lt;br /&gt;require 'twitter'&lt;br /&gt;&lt;br /&gt;#twitter account to post to&lt;br /&gt;twitter_email = "yourtwitteremail@bla.com"&lt;br /&gt;twitter_password = "secret"&lt;br /&gt;&lt;br /&gt;#rss feed to post&lt;br /&gt;rss_url = "http://yoursite.com/index.xml"&lt;br /&gt;rss_user_agent = "http://twitter.com/yourbot"&lt;br /&gt;&lt;br /&gt;#sqlite db&lt;br /&gt;path_to_sqlite_db = "/PATH/TO/db.sqlite"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;ActiveRecord::Base.logger = Logger.new(STDERR)&lt;br /&gt;ActiveRecord::Base.colorize_logging = false&lt;br /&gt;&lt;br /&gt;ActiveRecord::Base.establish_connection(&lt;br /&gt;    :adapter =&gt; "sqlite3",&lt;br /&gt;    :dbfile  =&gt; path_to_sqlite_db&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;#uncomment this section the first time to create the table&lt;br /&gt;#&lt;br /&gt;#ActiveRecord::Schema.define do&lt;br /&gt;#    create_table :item do |table|&lt;br /&gt;#        table.column :title, :string&lt;br /&gt;#        table.column :link, :string&lt;br /&gt;#    end&lt;br /&gt;#end&lt;br /&gt;&lt;br /&gt;class Item &lt; ActiveRecord::Base&lt;br /&gt;  def to_s&lt;br /&gt;    "#{self.title[0..(130-self.link.length)]} - #{self.link}"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;#run the beast&lt;br /&gt;rss_items = SimpleRSS.parse open(rss_url ,"User-Agent" =&gt; rss_user_agent)&lt;br /&gt;&lt;br /&gt;for item in rss_items.items&lt;br /&gt;  Item.transaction do&lt;br /&gt;    unless existing_item = Item.find(:all, :conditions =&gt; ["link=?", item.link]).first&lt;br /&gt;      twitter ||= Twitter::Base.new(twitter_email, twitter_password)&lt;br /&gt;      new_item = Item.create(:title =&gt; item.title, :link =&gt; item.link) &lt;br /&gt;      twitter.post(new_item.to_s)&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Run this once with the lines uncommented to create the DB, then slap it in your crontab.</description>
      <pubDate>Thu, 22 Mar 2007 04:20:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3714</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>Repair Spy</title>
      <link>http://snippets.dzone.com/posts/show/3586</link>
      <description>Takes a screenshot and a webcam snapshot and emails them to flickr. Requires &lt;a href="http://www.intergalactic.de/hacks.html"&gt;isightcapture&lt;/a&gt;. Hacked up to spy on the Apple repair team as they try to repair my Mac Book Pro's backlight for the second time.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;# requires isightcapture http://www.intergalactic.de/hacks.html&lt;br /&gt;&lt;br /&gt;# path to save the images&lt;br /&gt;PATH=/Users/default/&lt;br /&gt;&lt;br /&gt;# flickr email address&lt;br /&gt;EMAIL="yourflickremail+private@photos.flickr.com"&lt;br /&gt;&lt;br /&gt;# tags to attach to each picture uploaded&lt;br /&gt;TAGS="repair_spy"&lt;br /&gt;&lt;br /&gt;TIMESTAMP=`/bin/date "+%y%m%d%H%M%S"`&lt;br /&gt;FRIENDLY_TIMESTAMP=`/bin/date "+%Y/%m/%d %H:%M"`&lt;br /&gt;&lt;br /&gt;#take pics&lt;br /&gt;/usr/sbin/screencapture -mxC $PATHscreen_$TIMESTAMP.png&lt;br /&gt;/usr/local/bin/isightcapture -t png $PATHisight_$TIMESTAMP.png&lt;br /&gt;&lt;br /&gt;#post to flickr&lt;br /&gt;/usr/bin/uuencode $PATHscreen_$TIMESTAMP.png $PATHscreen_$TIMESTAMP.png | /usr/bin/mail -s "Repair Spy Screenshot - $FRIENDLY_TIMESTAMP tags: $TAGS" $EMAIL&lt;br /&gt;/usr/bin/uuencode $PATHisight_$TIMESTAMP.png $PATHisight_$TIMESTAMP.png | /usr/bin/mail -s "Repair Spy iSight Cature - $FRIENDLY_TIMESTAMP tags: $TAGS" $EMAIL&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Toss this in your crontab like so, making sure to run it as the user that you provide to the Apple Techs&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;*/1    *       *       *       *       default /Users/default/repair_spy &gt;&amp; /dev/null&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 26 Feb 2007 16:38:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3586</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>Ruby One Time Web Server</title>
      <link>http://snippets.dzone.com/posts/show/3475</link>
      <description>An interpretation of &lt;a href="http://c2.com/cgi/wiki?OneTimeWebServer"&gt;OneTimeWebServer&lt;/a&gt; in Ruby. OneTimeWebServer holds a single "page" in memory, serves it to the first visitor, and evaporates. Useful for all sorts of hijinks.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/local/bin/ruby&lt;br /&gt;require 'socket'&lt;br /&gt;t = STDIN.read&lt;br /&gt;while s = TCPServer.new('127.0.0.1', (ARGV[0] or 8080)).accept&lt;br /&gt;  puts s.gets&lt;br /&gt;  s.print "HTTP/1.1 200/OK\rContent-type: text/plain\r\n\r\n" + t&lt;br /&gt;  s.close&lt;br /&gt;  exit&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Usage:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;echo "test" | ./otws.rb [optional port number, defaults to 8080]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Or for recursive fun:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;cat otws.rb | ./otws.rb [optional port number, defaults to 8080]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 07 Feb 2007 23:45:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3475</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>Export del.icio.us links to MT Import Format</title>
      <link>http://snippets.dzone.com/posts/show/3396</link>
      <description>A friend of mine needed his del.icio.us links in MT Import format for some reason. This solved that problem.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'rexml/document'&lt;br /&gt;require 'net/https'&lt;br /&gt;require 'rubygems'&lt;br /&gt;&lt;br /&gt;# change credentials!&lt;br /&gt;user = 'username'&lt;br /&gt;pass = 'password'&lt;br /&gt;&lt;br /&gt;# User-Agent: required for del.icio.us api&lt;br /&gt;agent = 'delicious-mt'&lt;br /&gt;xml = ''&lt;br /&gt;&lt;br /&gt;http = Net::HTTP.new('api.del.icio.us', 443)&lt;br /&gt;http.use_ssl = true&lt;br /&gt;http.start do |http|&lt;br /&gt;  request = Net::HTTP::Get.new('/v1/posts/all', {'User-Agent' =&gt; agent})&lt;br /&gt;  request.basic_auth(user, pass)&lt;br /&gt;  response = http.request(request)&lt;br /&gt;  response.value&lt;br /&gt;  xml = response.body&lt;br /&gt;end&lt;br /&gt; &lt;br /&gt;REXML::Document.new(xml).elements.each('posts/post') do |el|&lt;br /&gt;  puts "TITLE: #{el.attributes['description']}"&lt;br /&gt;  puts "AUTHOR: Author Name"&lt;br /&gt;  puts "DATE: #{DateTime.parse(el.attributes['time']).strftime("%m/%d/%Y %H:%M:%S")}"&lt;br /&gt;  puts "-----"&lt;br /&gt;  puts "BODY:"&lt;br /&gt;  puts el.attributes['extended']&lt;br /&gt;  puts "-----"&lt;br /&gt;  puts "EXCERPT:"&lt;br /&gt;  puts el.attributes['href']&lt;br /&gt;  puts "KEYWORDS:"&lt;br /&gt;  puts el.attributes['tag']&lt;br /&gt;  puts "-----"&lt;br /&gt;  puts "--------"&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 31 Jan 2007 18:36:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3396</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>Synchronizing Rails DB Contents via Fixtures</title>
      <link>http://snippets.dzone.com/posts/show/3393</link>
      <description>The following rake task will dump the contents of the current environment's database to YAML fixtures. Stick the following in lib/tasks/fixtures.rake:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;namespace :db do&lt;br /&gt;  namespace :fixtures do&lt;br /&gt;    &lt;br /&gt;    desc 'Create YAML test fixtures from data in an existing database.  &lt;br /&gt;    Defaults to development database.  Set RAILS_ENV to override.'&lt;br /&gt;    task :dump =&gt; :environment do&lt;br /&gt;      sql  = "SELECT * FROM %s"&lt;br /&gt;      skip_tables = ["schema_info"]&lt;br /&gt;      ActiveRecord::Base.establish_connection(:development)&lt;br /&gt;      (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|&lt;br /&gt;        i = "000"&lt;br /&gt;        File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file|&lt;br /&gt;          data = ActiveRecord::Base.connection.select_all(sql % table_name)&lt;br /&gt;          file.write data.inject({}) { |hash, record|&lt;br /&gt;            hash["#{table_name}_#{i.succ!}"] = record&lt;br /&gt;            hash&lt;br /&gt;          }.to_yaml&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;After making changes to the database that you'd like to dump to fixtures:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rake db:fixtures:dump&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;After checking out updated fixtures from SVN:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rake db:migrate&lt;br /&gt;rake db:fixtures:load&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 31 Jan 2007 01:04:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3393</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
  </channel>
</rss>
