<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: ruby code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 18:16:00 GMT</pubDate>
    <description>DZone Snippets: ruby code</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>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>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>Capistrano : apply local patches when deploying from an external source code repository</title>
      <link>http://snippets.dzone.com/posts/show/3147</link>
      <description>I've submitted patches to a couple rails apps, and want to run off of their SCM's trunk code, but with my local patches applied. These Capistrano tasks will take any files matching &lt;pre&gt;patches/*.diff&lt;/pre&gt; in your local directory, and apply them before restarting your app.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;task :after_setup do&lt;br /&gt;  patches_setup&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;task :after_update_code do&lt;br /&gt;  send_and_apply_patches&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;task :patches_setup do&lt;br /&gt;  run "mkdir -p #{deploy_to}/#{shared_dir}/patches" &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;task :send_and_apply_patches do&lt;br /&gt;  Dir[File.join(File.dirname(__FILE__), '../patches/*.diff')].sort.each do |patch|&lt;br /&gt;    puts "sending #{File.basename(patch)}"&lt;br /&gt;    put(File.read(patch),&lt;br /&gt;       "#{deploy_to}/#{shared_dir}/patches/#{File.basename(patch)}",&lt;br /&gt;       :mode =&gt; 0777)&lt;br /&gt;    puts "applying #{File.basename(patch)}"&lt;br /&gt;    run "cd #{release_path}; patch -p0 &lt; #{deploy_to}/#{shared_dir}/patches/#{File.basename(patch)}"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 13 Dec 2006 20:51:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3147</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>redirect or render</title>
      <link>http://snippets.dzone.com/posts/show/1623</link>
      <description>Quick method to help your XHR requests degrade gracefully - via the &lt;a href="http://blog.caboo.se/articles/2006/03/02/redirect-or-render"&gt;caboo.se&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def redirect_or_render( redirect_to_hash, render_page  )  &lt;br /&gt;  if @request.xhr?&lt;br /&gt;    render(render_page)&lt;br /&gt;  else&lt;br /&gt;    redirect_to(redirect_to_hash)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Use like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;redirect_or_render(  &lt;br /&gt;  {:action=&gt;'foo'},&lt;br /&gt;  { :partial =&gt; 'monkey', :locals =&gt; { :obj = &gt; 'x' } }&lt;br /&gt;)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 03 Mar 2006 08:04:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1623</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
  </channel>
</rss>
