<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Ellipsis: Tag ruby</title>
    <link>http://typo.pburkholder.com/articles/tag/ruby?tag=ruby</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>...</description>
    <item>
      <title>Ruby epoch2localtime</title>
      <description>&lt;p&gt;I love it when 82 characters do a ton of work for me.&lt;/p&gt;


	&lt;p&gt;Today I&amp;#8217;m trying to debug Courier-IMAP ssl errors that are reported in the log file as &amp;#8220;DEBUG: Unexpected &lt;span class="caps"&gt;SSL&lt;/span&gt; connection shutdown&amp;#8221;.   We&amp;#8217;re using QMail  so the times in the log file are in tai64, and I&amp;#8217;m using Eric Rescorla&amp;#8217;s SSLDump:&amp;#8221;http://www.rtfm.com/ssldump/&amp;#8221; to debug the &lt;span class="caps"&gt;SSL&lt;/span&gt; traffic, which reports time in epoch seconds.  I&amp;#8217;d like to correlate the &lt;span class="caps"&gt;SSL&lt;/span&gt; traffic with the logged errors.  Here&amp;#8217;s how:&lt;/p&gt;


	&lt;p&gt;In window 1,  I monitor the QMail logs with:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
tail -f /var/log/qmail/imap4-ssl/current | tail64nlocal
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;which gives output like:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
2007-10-30 06:26:02.322254500 tcpserver: status: 30/40
2007-10-30 06:26:02.322255500 tcpserver: pid 9635 from 1.2.3.4
2007-10-30 06:26:02.322257500 tcpserver: ok 9635 buzz.example.net:10.1.1.20:993 :216.220.209.17::57693
2007-10-30 06:26:02.439369500 DEBUG: Connection, ip=[1.2.3.4]
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;In window 2, I monitor the &lt;span class="caps"&gt;SSL&lt;/span&gt; traffic with:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
sudo ssldump -e -k imap.example.com.pem  port 993 | ~/bin/epoch2local.rb
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;where the script @epoch2local.rb is:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
#!/usr/bin/ruby -p

$_.sub!(/1\d{9}/) { |t| Time.at(t.to_f).strftime("%Y-%m-%d %H:%M:%S") }
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;A quick dissection:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;code&gt;ruby -p&lt;/code&gt; places your code in a &lt;code&gt;while gest; ....; print; end&lt;/code&gt; loop&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;$_&lt;/code&gt; is the current line&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;.sub!&lt;/code&gt; does in place substitution, changing the value of &lt;code&gt;$_&lt;/code&gt;&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;sub!(pattern) { |match| block }&lt;/code&gt; the string matting &lt;code&gt;pattern&lt;/code&gt; is passed into the {} block as the variable &lt;code&gt;match&lt;/code&gt;.  The result from the block is substituted for the original string&lt;/li&gt;
		&lt;li&gt;&lt;code&gt;/1\d{9}/&lt;/code&gt;: assume that a 10-digit number starting with 1 is the epoch time (true for another 30 years or so)&lt;/li&gt;
		&lt;li&gt;&lt;code&gt; |t| Time.at(t.to_f).strftime("%Y-%m-%d %H:%M:%S") &lt;/code&gt;: &lt;span class="caps"&gt;PFM&lt;/span&gt;. No, not really. Pass the match into the block as &lt;code&gt;t&lt;/code&gt;.  Convert to a float, then a Time value, then format as &lt;code&gt;%Y-%m-%d %H:%M:%S&lt;/code&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;I then get SSLDump out put as:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
23 21 2007-10-30 06:41:19.1945 (0.1309)  C&amp;gt;S  application_data
24 3  2007-10-30 06:41:19.2474 (0.1185)  S&amp;gt;C  Handshake
      Certificate
24 4  2007-10-30 06:41:19.2474 (0.0000)  S&amp;gt;C  Handshake
      ServerHelloDone  
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;And I can match up the times with the QMail logs.  Fini (although I still have the original question to resolve)&lt;/p&gt;</description>
      <pubDate>Tue, 30 Oct 2007 06:21:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:78dad476-44d2-4be1-806d-0ec2aeecc63a</guid>
      <author>Peter Burkholder</author>
      <link>http://typo.pburkholder.com/articles/2007/10/30/ruby-epoch2localtime</link>
      <category>Ruby and Rails</category>
      <category>time</category>
      <category>ruby</category>
      <category>noobie</category>
    </item>
    <item>
      <title>Ruby MySQL bindings for Intel Macs</title>
      <description>&lt;p&gt;Stefan Saasen points out &lt;a href="http://www.juretta.com/log/2006/10/13/ruby_installing_ruby_mysql_driver_on_intel_macs/"&gt;that the Ruby MySQL bindings are broken on Intel Macs&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s my version of the fix:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
sudo gem install mysql -- --include=/opt/local
# select option 2 for Ruby 2.7, it will fail to build
cd /opt/local/lib/ruby/gems/1.8/gems/mysql-2.7
sudo ruby extconf.rb install mysql -- --with-mysql-dir=/opt/local
vim mysql.c
# now here's where you add the line '#define ulong unsigned long' 
# just before the line '#define MYSQL_RUBY_VERSION...'
sudo make
sudo make install
&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Fri, 17 Nov 2006 14:47:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:a8bd6045-5d25-4dd9-ae1c-1a2bdb356410</guid>
      <author>Peter Burkholder</author>
      <link>http://typo.pburkholder.com/articles/2006/11/17/ruby-mysql-bindings-for-intel-macs</link>
      <category>Ruby and Rails</category>
      <category>ruby</category>
      <category>mysql</category>
      <category>macosx</category>
      <trackback:ping>http://typo.pburkholder.com/articles/trackback/907</trackback:ping>
    </item>
    <item>
      <title>Installing Rails on Mac OS X with MacPort</title>
      <description>&lt;p&gt;Yesterday I installed Ruby on Rails on my new(-ish) Intel MacBook.   Last time around I built with a combination of &lt;a href="http://fink.sourceforge.net"&gt;Fink packages&lt;/a&gt; and hand-built applications following &lt;a href="http://hivelogic.com/articles/2005/12/01/ruby_rails_lighttpd_mysql_tiger"&gt;this posting at Hivelogic&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;This time around I&amp;#8217;ve been using &lt;a href="http://www.macports.org"&gt;MacPorts&lt;/a&gt;, and it&amp;#8217;s making my life much easier.   &lt;a href="http://www.evanweaver.com"&gt;Evan Weaver&lt;/a&gt; got me started with &lt;a href="http://blog.evanweaver.com/articles/2006/06/26/building-ruby-rails-lighttpd-mysql-and-postgres-on-os-x-tiger"&gt;his post on building ruby, rails and associated pieces&lt;/a&gt;, but enough has changed changed since June 26 to merit my own updated take on the process.&lt;/p&gt;


	&lt;h2&gt;Getting started&lt;/h2&gt;


	&lt;p&gt;As Evan notes, &amp;#8220;First, install the Apple Xcode tools from your &lt;span class="caps"&gt;OS X&lt;/span&gt; installation disc&amp;#8221;.  Please do so.&lt;/p&gt;


	&lt;p&gt;Next, install a recent version of &amp;#8220;MacPorts&amp;#8221; (what used to be known as DarwinPorts) from their Subversion respository.  Installing from a .dmg file is easiest, then you can let MacPorts upgrade itself later on.   As of this writing, Ports 1.3.2 is out, but disk images are only available for 1.3.1, e.g. at &lt;a href="http://svn.macosforge.org/repository/macports/downloads/DarwinPorts-1.3.1/DarwinPorts-1.3.1-10.4.dmg"&gt;DarwinPorts-1.3.1-10.4.dmg&lt;/a&gt;&lt;/p&gt;


Next, you&amp;#8217;ll want to update your executable path so the Ports installations in /opt/local are found before your Apple binaries.  You should edit both &lt;code&gt;/etc/profile&lt;/code&gt; and your &lt;code&gt;~/.bashrc&lt;/code&gt; (or equivalent if you&amp;#8217;re using some other shell.  Your path should end up looking something like this:
&lt;pre&gt;
&lt;code&gt;
PATH="/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin" 
&lt;/code&gt;
&lt;/pre&gt;

	&lt;h2&gt;Install the ports&lt;/h2&gt;


	&lt;p&gt;Now open a terminal (&lt;code&gt;/Applications/Utilities/Terminal&lt;/code&gt;) and run the following:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
sudo port -d selfupdate 
sudo port install lighttpd +ssl 
sudo port install rb-rubygems
sudo port install rb-fcgi
sudo port install mysql4 +server
&lt;/pre&gt;&lt;/code&gt;

	&lt;h2&gt;Set up MySQL&lt;/h2&gt;


	&lt;p&gt;You&amp;#8217;ll also need to get mysql4 set up with these commands:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
# set up the mysql database:
sudo -u mysql mysql_install_db
# start the server:
sudo /opt/local/bin/mysqld_safe --user=mysql
# set the root password (picking your own password, of course)
/opt/local/bin/mysqladmin -u root password newpassword
&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;If you want Launcher to start MySQL automatically on reboot, you can run the following:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
sudo launchclt load -w  \
  /Library/LaunchDaemons/org.macports.mysql4.plist
# stop the server
sudo launchctl stop org.macports.mysql4
# start the server
sudo launchctl start org.macports.mysql4
&lt;/code&gt;&lt;/pre&gt;

	&lt;h2&gt;Install the gems&lt;/h2&gt;


	&lt;p&gt;Running gems with the &amp;#8216;-y&amp;#8217; option automatically takes care of prerequisites&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;
sudo gem install -y rails
sudo gem install -y capistrano
&lt;/pre&gt;&lt;/code&gt;

	&lt;h2&gt;Test!&lt;/h2&gt;


	&lt;p&gt;First, are you hitting the right version of Ruby?  &lt;code&gt;ruby --version&lt;/code&gt; should return something like &lt;code&gt;ruby 1.8.5 (2006-08-25) [i686-darwin8.8.1]&lt;/code&gt; &lt;strong&gt;not this&lt;/strong&gt;: &lt;code&gt;ruby 1.8.2 (2004-12-25) [universal-darwin8.0]&lt;/code&gt;&lt;/p&gt;


	&lt;p&gt;Next, can you build a Rails application with&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
cd ~/tmp
rails widgetapp
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Okay?  Good.  Now let&amp;#8217;s &lt;code&gt;cd widgetapp&lt;/code&gt; and put the database through it&amp;#8217;s paces.  Save the following code as &lt;code&gt;test_rails_db.sh&lt;/code&gt; (or &lt;a href="/files/testdb.sh"&gt;download it here&lt;/a&gt;)&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
#!/bin/sh

echo -n "Enter MySQL root password: " 
read PASSWD

mysqladmin -u root -p$PASSWD create widgetapp_development

cat &amp;gt;db/create.sql &amp;lt;&amp;lt;EOF
DROP table if exists widgets;

CREATE table widgets (
    id  int not null    auto_increment,
    name    varchar(40)  not null,
    description   varchar(100)   not null,
    primary key (id)
    );

INSERT INTO widgets (name, description) VALUES ("Tool", "Useful item");
INSERT INTO widgets (name, description) VALUES ("Food", "Tasty stuff");

EOF

mysql -u root -p$PASSWD  widgetapp_development &amp;lt; db/create.sql

mv config/database.yml config/database.yml.dist

cat &amp;gt;config/database.yml &amp;lt;&amp;lt;EOF 

development:
  adapter: mysql
  database: widgetapp_development
  username: root
  password: $PASSWD
  socket: /opt/local/var/run/mysqld/mysqld.sock

EOF
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;and run &lt;code&gt;sh ./test_rails_db.sh&lt;/code&gt;.  Enter your password when prompted.&lt;/p&gt;


	&lt;p&gt;Now the proof is in the pudding.  If the following run s while you&amp;#8217;re in your &lt;code&gt;widgetapp&lt;/code&gt; rails directory, you&amp;#8217;re golden:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
script/generate scaffold Widget
script/server
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Now browse to &lt;a href="http://0.0.0.0:3000/widgets/list"&gt;http://0.0.0.0:3000/widgets/list&lt;/a&gt; and you should utter a little gasp of joy.&lt;/p&gt;</description>
      <pubDate>Fri, 17 Nov 2006 13:08:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:3848e11a-8a2a-44c9-b8df-2f3ad2903c13</guid>
      <author>Peter Burkholder</author>
      <link>http://typo.pburkholder.com/articles/2006/11/17/installing-rails-on-mac-os-x-with-macport</link>
      <category>System Administration</category>
      <category>Ruby and Rails</category>
      <category>ruby</category>
      <category>rails</category>
      <category>lighttpd</category>
      <category>mysql</category>
      <category>macports</category>
      <category>darwin</category>
      <category>darwinports</category>
      <category>macosx</category>
      <enclosure type="application/x-sh" length="800" url="http://typo.pburkholder.com/files/testdb.sh"/>
      <trackback:ping>http://typo.pburkholder.com/articles/trackback/886</trackback:ping>
    </item>
  </channel>
</rss>
