<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Netphase &#187; ruby</title>
	<atom:link href="http://blog.netphase.com/category/blog/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.netphase.com</link>
	<description>for a connected world</description>
	<lastBuildDate>Mon, 14 Dec 2009 21:40:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>AAAP Updated!</title>
		<link>http://blog.netphase.com/2008/12/05/acts_as_amazon_product-update/</link>
		<comments>http://blog.netphase.com/2008/12/05/acts_as_amazon_product-update/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 17:45:02 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Acts As Amazon Product]]></category>
		<category><![CDATA[Netphase News]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[aaap]]></category>
		<category><![CDATA[acts_as_amazon_product]]></category>
		<category><![CDATA[gem]]></category>

		<guid isPermaLink="false">http://www.netphase.com/?p=160</guid>
		<description><![CDATA[It&#8217;s been a while since we have called attention to it, but over the course of the year, our Amazon product integration gem, acts_as_amazon_product, has been improved.
First, we got some participation from David Eisinger to help improve testing and some other critical features. Thanks, David! Recently though, we added some new functionality we had lying [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while since we have called attention to it, but over the course of the year, our Amazon product integration gem, <a href="http://github.com/netphase/aaap/tree/master" target="_new">acts_as_amazon_product</a>, has been improved.</p>
<p>First, we got some participation from David Eisinger to help improve testing and some other critical features. Thanks, David! Recently though, we added some new functionality we had lying around in an old plugin called acts_as_amazonable.</p>
<p>The new functionality allows you to load Amazon product attributes into a new or existing model object on your system. You can define which fields are loaded, and which attributes in your model they map to. We are using this functionality on a new double-secret project that we are working on, and it is great. In that project, we are letting our users type in an ISBN code, and we pre-populate a form with all of the Amazon data we want for that product, saving the user from a lot of typing. We also pick up the images, so they don&#8217;t even have  to load an image to sell their item. Pretty cool!</p>
<p>Here&#8217;s how you use aaap&#8217;s new functionality:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'acts_as_amazon_product'</span>
&nbsp;
 <span style="color:#9966CC; font-weight:bold;">class</span> LocalBook <span style="color:#006600; font-weight:bold;">&amp;</span>lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
   acts_as_amazon_product<span style="color:#006600; font-weight:bold;">&#40;</span>
     <span style="color:#ff3333; font-weight:bold;">:asin</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'isbn'</span>, <span style="color:#ff3333; font-weight:bold;">:name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'title'</span>,
     <span style="color:#ff3333; font-weight:bold;">:access_key</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'0123456'</span>, <span style="color:#ff3333; font-weight:bold;">:associate_tag</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'assoc-20'</span>,
     <span style="color:#ff3333; font-weight:bold;">:auto_load_fields</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#123;</span>
         <span style="color:#ff3333; font-weight:bold;">:title</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'title'</span>,
         <span style="color:#ff3333; font-weight:bold;">:isbn</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'asin'</span>,
         <span style="color:#ff3333; font-weight:bold;">:publisher_name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'manufacturer'</span>,
         <span style="color:#ff3333; font-weight:bold;">:author</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'author'</span><span style="color:#006600; font-weight:bold;">&#125;</span>
   <span style="color:#006600; font-weight:bold;">&#41;</span>
 <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The keys in the auto_load_fields hash are the fields in your object, and the values are their Amazon equivalents. In the true Rails convention over configuration spirit, we have included some of the most popular auto load fields as defaults. If your object contains the following attributes, they will automatically be loaded with their Amazon equivalents:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#ff3333; font-weight:bold;">:auto_load_fields</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#123;</span>
     <span style="color:#ff3333; font-weight:bold;">:title</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'title'</span>,
     <span style="color:#ff3333; font-weight:bold;">:isbn</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'asin'</span>,
     <span style="color:#ff3333; font-weight:bold;">:publisher_name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'manufacturer'</span>,
     <span style="color:#ff3333; font-weight:bold;">:author</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'author'</span>,
     <span style="color:#ff3333; font-weight:bold;">:binding</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'binding'</span>,
     <span style="color:#ff3333; font-weight:bold;">:list_price</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'listprice/amount'</span>,
     <span style="color:#ff3333; font-weight:bold;">:pages</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'numberofpages'</span>,
     <span style="color:#ff3333; font-weight:bold;">:description</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'content'</span>,
     <span style="color:#ff3333; font-weight:bold;">:small_image_url</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'smallimage/url'</span>,
     <span style="color:#ff3333; font-weight:bold;">:medium_image_url</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'mediumimage/url'</span>,
     <span style="color:#ff3333; font-weight:bold;">:large_image_url</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'largeimage/url'</span>,
     <span style="color:#ff3333; font-weight:bold;">:detail_url</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'detailpageurl'</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Don&#8217;t worry if your object does not have some or most of these fields; Acts as Amazon Product is smart enough to check to see if the method exists before attempting to load up the Amazon value.</p>
<p>Now, if by some coincidence you have some fields in your local object that have the same name as our auto_load defaults, you can choose to specifically ignore those fields using, you guessed it, :ignore_fields.  :ignore_fields is a simple array of fields in your object that you do NOT want to be replaced with the Amazon equivalent.  You would specify them like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">acts_as_amazon_product<span style="color:#006600; font-weight:bold;">&#40;</span>
    <span style="color:#ff3333; font-weight:bold;">:asin</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'isbn'</span>, <span style="color:#ff3333; font-weight:bold;">:name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'title'</span>,
    <span style="color:#ff3333; font-weight:bold;">:access_key</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'0123456'</span>, <span style="color:#ff3333; font-weight:bold;">:associate_tag</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'assoc-20'</span>,
    <span style="color:#ff3333; font-weight:bold;">:ignore_fields</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:small_image_url</span>, <span style="color:#ff3333; font-weight:bold;">:medium_image_url</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>In this instance, the object in question would load Amazon info for any of the default fields that the object has, with the exception of the small_image_url and medium_image_url values.</p>
<p>To load the Amazon values into your object for local storage, you may call the following methods:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">load_from_amazon<span style="color:#006600; font-weight:bold;">&#40;</span>title_or_asin, by_title = <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>
load_from_amazon!<span style="color:#006600; font-weight:bold;">&#40;</span>title_or_asin, by_title = <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>The two methods do exactly the same thing, except the first simply returns a new, unsaved object with its Amazon-like fields loaded with their appropriate Amazon values.  The second version (load_from_amazon!) goes one step further and saves the object for good measure.</p>
<p>You may pass either an isbn (asin) or a title in.  If you are passing a title, you need to specify that you are passing a title by also passing true as the second parameter so we will perform an item_search rather than an item_lookup. Note that passing a title is risky business, since currently the code will take the first result returned and load that into your object. So unless your title is unique, I would recommend sticking with the isbn (asin) value.</p>
<p>Since that is the default, all you need to do would be something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@local_book</span> = LocalBook.<span style="color:#9900CC;">load_from_amazon</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'1590598415'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>If your object was similar to the LocalBook object in our example, for instance, you would not need to specify the auto_load_fields in the acts_as call at all.  The title, isbn, publisher_name and author methods would automatically be called once you call load_from_amazon, and their values populated appropriately.</p>
<p>It is important to note that if you call load_from_amazon with an invalid asin, you will simply get back an empty object.</p>
<p>Now, calling load_from_amazon still allows you to take advantage of the other Acts as Amazon features, like the dynamic loading of Amazon attributes on the fly by using the various amazon.foo methods as it always has.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.netphase.com/2008/12/05/acts_as_amazon_product-update/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>nested error_messages_for</title>
		<link>http://blog.netphase.com/2008/03/06/nested-error_messages_for/</link>
		<comments>http://blog.netphase.com/2008/03/06/nested-error_messages_for/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 03:20:55 +0000</pubDate>
		<dc:creator>scottned</dc:creator>
				<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.netphase.com/2008/03/06/nested-error_messages_for/</guid>
		<description><![CDATA[I think lots of folks have run into this problem of using error_messages_for to display errors of nested or subordinate objects.  The current Rails method doesn&#8217;t handle it well.  In addition, there&#8217;s the problem of not being able to specify the full message.  So&#8230; I tackled this problem today and came up [...]]]></description>
			<content:encoded><![CDATA[<p>I think lots of folks have run into this problem of using error_messages_for to display errors of nested or subordinate objects.  The current Rails method doesn&#8217;t handle it well.  In addition, there&#8217;s the problem of not being able to specify the full message.  So&#8230; I tackled this problem today and came up with a pretty good solution using recursion.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> nested_error_messages_for<span style="color:#006600; font-weight:bold;">&#40;</span>object<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC00FF; font-weight:bold;">Symbol</span>, <span style="color:#CC0066; font-weight:bold;">String</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">member</span>? object.<span style="color:#9966CC; font-weight:bold;">class</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    object = instance_variable_get<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@#{object}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">unless</span> object.<span style="color:#0000FF; font-weight:bold;">nil</span>? <span style="color:#006600; font-weight:bold;">||</span> object.<span style="color:#9900CC;">errors</span>.<span style="color:#9900CC;">count</span>.<span style="color:#9900CC;">zero</span>?
    error_messages = object.<span style="color:#9900CC;">errors</span>.<span style="color:#9900CC;">to_a</span>.<span style="color:#9900CC;">uniq</span>.<span style="color:#9900CC;">map</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>key, value<span style="color:#006600; font-weight:bold;">|</span>
      object2 = object.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>key<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> object2.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">Array</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        object2.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>obj<span style="color:#006600; font-weight:bold;">|</span> 
          content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:li</span>, nested_error_messages_for<span style="color:#006600; font-weight:bold;">&#40;</span>obj<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> 
        <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">elsif</span> object2.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:li</span>, nested_error_messages_for<span style="color:#006600; font-weight:bold;">&#40;</span>object2<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">elsif</span> value.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^\^<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:li</span>, value<span style="color:#006600; font-weight:bold;">&#91;</span>1..<span style="color:#9900CC;">value</span>.<span style="color:#9900CC;">length</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:li</span>, 
           <span style="color:#996600;">&quot;#{key.underscore.split('_').join(' ').humanize} #{value}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:div</span>,
      content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:div</span>, 
        <span style="color:#996600;">&quot;#{object.class} has errors&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'error_field'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span>
      content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:ul</span>, error_messages<span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#ff3333; font-weight:bold;">:class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'error_block'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#996600;">''</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>To use it, just replace your current call to error_messages_for with nested_error_messages_for.  There&#8217;s nothing extra that needs to be done in the controller as with some recipes.  You&#8217;ll also want to make it look good with some CSS.</p>
<p>I&#8217;m pretty sure there&#8217;s more that could be done with this, but thought I&#8217;d present it here first and try to get some feedback before turning it into a gem.  Let me know how it works for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.netphase.com/2008/03/06/nested-error_messages_for/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Extending ActiveRecord with count_since</title>
		<link>http://blog.netphase.com/2008/02/23/extending-activerecord-with-count_since/</link>
		<comments>http://blog.netphase.com/2008/02/23/extending-activerecord-with-count_since/#comments</comments>
		<pubDate>Sat, 23 Feb 2008 02:29:15 +0000</pubDate>
		<dc:creator>scottned</dc:creator>
				<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.netphase.com/2008/02/23/extending-activerecord-with-count_since/</guid>
		<description><![CDATA[I just implemented a statistics page for an application and found that I was using this pattern over and over again:

User.count&#40;:conditions =&#62; &#91;'created_at &#62; ?', 30.days.ago&#93;&#41;

Here&#8217;s a simple extension I made to ActiveRecord to DRY it up:

module ActiveRecord
  class Base
      def self.count_since&#40;time_ago&#41;
        [...]]]></description>
			<content:encoded><![CDATA[<p>I just implemented a statistics page for an application and found that I was using this pattern over and over again:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">User.<span style="color:#9900CC;">count</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'created_at &gt; ?'</span>, 30.<span style="color:#9900CC;">days</span>.<span style="color:#9900CC;">ago</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Here&#8217;s a simple extension I made to ActiveRecord to <abbr title="Don't Repeat Yourself">DRY</abbr> it up:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ActiveRecord
  <span style="color:#9966CC; font-weight:bold;">class</span> Base
      <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">count_since</span><span style="color:#006600; font-weight:bold;">&#40;</span>time_ago<span style="color:#006600; font-weight:bold;">&#41;</span>
        count<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'created_at &gt; ?'</span>, time_ago<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Put that snippet in a file in your lib directory.  I called mine rails_extensions.rb.  Then add <em>require &#8216;rails_extensions&#8217;</em> to the bottom of your environment.rb file.</p>
<p>Now you can just do:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">User.<span style="color:#9900CC;">count_since</span><span style="color:#006600; font-weight:bold;">&#40;</span>30.<span style="color:#9900CC;">days</span>.<span style="color:#9900CC;">ago</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>That&#8217;s a little cleaner, don&#8217;t you think?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.netphase.com/2008/02/23/extending-activerecord-with-count_since/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Autotest CPU Fix</title>
		<link>http://blog.netphase.com/2008/01/05/autotest-cpu-fix/</link>
		<comments>http://blog.netphase.com/2008/01/05/autotest-cpu-fix/#comments</comments>
		<pubDate>Sat, 05 Jan 2008 22:17:57 +0000</pubDate>
		<dc:creator>scottned</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.netphase.com/2008/01/05/autotest-cpu-fix/</guid>
		<description><![CDATA[
Update 1/15/2008
Autotest and rspec both posted updates today.  Now, the way to fix this issue is slightly different:

Autotest.add_hook :run do &#124;autotest&#124;
  autotest.add_exception(/^\.\/vendor/)
  autotest.add_exception(/\.svn/)
end

Thanks Ryan and David for the updates!

I&#8217;ve been noticing in my current project that running autotest was constantly consuming about 25-30% of my cpu and causing my macbook pro to [...]]]></description>
			<content:encoded><![CDATA[<div style="background:#eee">
<b>Update 1/15/2008</b><br />
Autotest and rspec both posted updates today.  Now, the way to fix this issue is slightly different:</p>
<pre>
Autotest.add_hook :run do |autotest|
  autotest.add_exception(/^\.\/vendor/)
  autotest.add_exception(/\.svn/)
end
</pre>
<p>Thanks <a href="http://blog.zenspider.com/">Ryan</a> and <a href="http://blog.davidchelimsky.net/">David<a/> for <a href="http://blog.davidchelimsky.net/articles/2008/01/15/rspec-1-1-2-and-zentest-3-8-0">the updates</a>!
</div>
<p>I&#8217;ve been noticing in my current project that running autotest was constantly consuming about 25-30% of my cpu and causing my macbook pro to run really hot.  I did a little googling and found <a href="http://rails.aizatto.com/2007/11/28/taming-the-autotest-beast-with-fsevents/">this discussion</a> on the topic.</p>
<p>For me, my problem was definitely the vendor directory (~3500 files in 4 plugins: rspec, rspec_on_rails, restful_open_id_authentication and active_merchant).  I tried excluding the entire directory by adding this to my ~/.autotest file:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Autotest.<span style="color:#9900CC;">add_hook</span> <span style="color:#ff3333; font-weight:bold;">:initialize</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>autotest<span style="color:#006600; font-weight:bold;">|</span>
  autotest.<span style="color:#9900CC;">exceptions</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span>
   <span style="color:#006600; font-weight:bold;">%</span>r<span style="color:#006600; font-weight:bold;">%</span>^\.<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>?:db<span style="color:#006600; font-weight:bold;">|</span>doc<span style="color:#006600; font-weight:bold;">|</span>log<span style="color:#006600; font-weight:bold;">|</span>public<span style="color:#006600; font-weight:bold;">|</span>script<span style="color:#006600; font-weight:bold;">|</span>vendor<span style="color:#006600; font-weight:bold;">|</span>previous_failures.<span style="color:#9900CC;">txt</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">%</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>However, that didn&#8217;t work.  After some investigation, I found out that the rspec_on_rails plugin in my vendor directory was subclassing Autotest::Rspec and setting it&#8217;s own exceptions string like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> initialize
  <span style="color:#9966CC; font-weight:bold;">super</span>
  <span style="color:#0066ff; font-weight:bold;">@exceptions</span> = 
   <span style="color:#006600; font-weight:bold;">%</span>r<span style="color:#006600; font-weight:bold;">%</span>^\.<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>?:db<span style="color:#006600; font-weight:bold;">|</span>doc<span style="color:#006600; font-weight:bold;">|</span>log<span style="color:#006600; font-weight:bold;">|</span>public<span style="color:#006600; font-weight:bold;">|</span>script<span style="color:#006600; font-weight:bold;">|</span>vendor\<span style="color:#006600; font-weight:bold;">/</span>rails<span style="color:#006600; font-weight:bold;">|</span>previous_failures.<span style="color:#9900CC;">txt</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">%</span>
...</pre></div></div>

<p>See the problem?  It dutifully calls super so AutoTest:initialize can do it&#8217;s stuff (including calling my hook) and then wipes out the exception string with it&#8217;s own.</p>
<p>So, I browsed the lib/autotest.rb code and found another hook.  Adding this to my ~/.autotest now lowers the cpu usage to about 5%.  Huzzah!</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Autotest.<span style="color:#9900CC;">add_hook</span> <span style="color:#ff3333; font-weight:bold;">:run</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>autotest<span style="color:#006600; font-weight:bold;">|</span>
  autotest.<span style="color:#9900CC;">exceptions</span> = <span style="color:#CC00FF; font-weight:bold;">Regexp</span>.<span style="color:#9900CC;">union</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^\.\<span style="color:#006600; font-weight:bold;">/</span>vendor<span style="color:#006600; font-weight:bold;">/</span>, autotest.<span style="color:#9900CC;">exceptions</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.netphase.com/2008/01/05/autotest-cpu-fix/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>acts_as_amazon_product</title>
		<link>http://blog.netphase.com/2007/09/26/acts_as_amazon_product/</link>
		<comments>http://blog.netphase.com/2007/09/26/acts_as_amazon_product/#comments</comments>
		<pubDate>Wed, 26 Sep 2007 20:31:58 +0000</pubDate>
		<dc:creator>scottned</dc:creator>
				<category><![CDATA[Acts As Amazon Product]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.netphase.com/2007/09/26/acts_as_amazon_product/</guid>
		<description><![CDATA[Today we released a new Ruby Gem that makes integrating with Amazon E-Commerce Service (ECS) a snap.  It&#8217;s called acts_as_amazon_product and you can find it on RubyForge.
All that&#8217;s necessary to integrate any of your existing models with Amazon is to add a require line to the top of your model file and then an [...]]]></description>
			<content:encoded><![CDATA[<p>Today we released a new Ruby Gem that makes integrating with Amazon E-Commerce Service (ECS) a snap.  It&#8217;s called acts_as_amazon_product and you can <a href="http://rubyforge.org/projects/aaap/">find it on RubyForge</a>.</p>
<p>All that&#8217;s necessary to integrate any of your existing models with Amazon is to add a <em>require</em> line to the top of your model file and then an acts_as_amazon_product line just inside your class definition.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'acts_as_amazon_product'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Book <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  acts_as_amazon_product<span style="color:#006600; font-weight:bold;">&#40;</span>
      <span style="color:#ff3333; font-weight:bold;">:asin</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'isbn'</span>, <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'title'</span>, 
      <span style="color:#ff3333; font-weight:bold;">:access_key</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'0123456'</span>, 
      <span style="color:#ff3333; font-weight:bold;">:associate_tag</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'assoc-20'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>After this, you can access Amazon data in your controllers or views like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@book</span> = Book.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:title</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Getting Things Done'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#0066ff; font-weight:bold;">@book</span>.<span style="color:#9900CC;">amazon</span>.<span style="color:#9900CC;">isbn</span>
<span style="color:#0066ff; font-weight:bold;">@book</span>.<span style="color:#9900CC;">amazon</span>.<span style="color:#9900CC;">title</span>
<span style="color:#0066ff; font-weight:bold;">@book</span>.<span style="color:#9900CC;">amazon</span>.<span style="color:#9900CC;">author</span>
<span style="color:#0066ff; font-weight:bold;">@book</span>.<span style="color:#9900CC;">amazon</span>.<span style="color:#9900CC;">small_image_url</span>
&nbsp;
<span style="color:#0066ff; font-weight:bold;">@book</span>.<span style="color:#9900CC;">amazon</span>.<span style="color:#9900CC;">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'itemattributes/foobar'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>The code does not require changing any current database tables.  It only requires adding one migration for a single new table used to cache responses from Amazon:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">connection</span>.<span style="color:#9900CC;">create_table</span> <span style="color:#ff3333; font-weight:bold;">:amazon_products</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
  t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:asin</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
  t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:xml</span>, <span style="color:#ff3333; font-weight:bold;">:text</span>
  t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:created_at</span>, <span style="color:#ff3333; font-weight:bold;">:datetime</span>
  t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:amazonable_id</span>, <span style="color:#ff3333; font-weight:bold;">:integer</span>
  t.<span style="color:#9900CC;">column</span> <span style="color:#ff3333; font-weight:bold;">:amazonable_type</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.netphase.com/2007/09/26/acts_as_amazon_product/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apache Log Quick Summary</title>
		<link>http://blog.netphase.com/2007/05/10/apache-log-quick-summary/</link>
		<comments>http://blog.netphase.com/2007/05/10/apache-log-quick-summary/#comments</comments>
		<pubDate>Thu, 10 May 2007 19:46:23 +0000</pubDate>
		<dc:creator>scottned</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.netphase.com/2007/05/10/apache-log-quick-summary/</guid>
		<description><![CDATA[Sometimes I set up a quick site for a client and want to summarize hits without setting up awstats.  So, I wrote a quick and dirty script to sum up the hits per day in Apache&#8217;s access_log file.
Example:

$ cklog access_log

04-15-2007: 21
04-16-2007: 2134
04-17-2007: 304
04-18-2007: 6960
04-19-2007: 951
04-20-2007: 412

Here&#8217;s the script:

require 'date'
&#160;
daily = Hash.new
File.open&#40;ARGV&#91;0&#93; &#124;&#124; &#34;access_log&#34;, &#34;r&#34;&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes I set up a quick site for a client and want to summarize hits without setting up awstats.  So, I wrote a quick and dirty script to sum up the hits per day in Apache&#8217;s access_log file.</p>
<p>Example:</p>
<pre>
$ cklog access_log

04-15-2007: 21
04-16-2007: 2134
04-17-2007: 304
04-18-2007: 6960
04-19-2007: 951
04-20-2007: 412
</pre>
<p>Here&#8217;s the script:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'date'</span>
&nbsp;
daily = <span style="color:#CC00FF; font-weight:bold;">Hash</span>.<span style="color:#9900CC;">new</span>
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#996600;">&quot;access_log&quot;</span>, <span style="color:#996600;">&quot;r&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>file<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#9966CC; font-weight:bold;">while</span> line = file.<span style="color:#CC0066; font-weight:bold;">gets</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> line =~ <span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#125;</span>\<span style="color:#006600; font-weight:bold;">/</span>\w<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#125;</span>\<span style="color:#006600; font-weight:bold;">/</span>\d<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#006600; font-weight:bold;">*</span>GET\s<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>^\?\s<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span>
      date = <span style="color:#CC00FF; font-weight:bold;">Date</span>.<span style="color:#9900CC;">strptime</span> $<span style="color:#006666;">1</span>, <span style="color:#996600;">'%d/%b/%Y'</span>
      daily<span style="color:#006600; font-weight:bold;">&#91;</span>date<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">0</span> <span style="color:#9966CC; font-weight:bold;">if</span> daily<span style="color:#006600; font-weight:bold;">&#91;</span>date<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>?
      daily<span style="color:#006600; font-weight:bold;">&#91;</span>date<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
daily.<span style="color:#9900CC;">sort</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>d, f<span style="color:#006600; font-weight:bold;">|</span> 
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{d.strftime '%m-%d-%Y'}: #{f}&quot;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.netphase.com/2007/05/10/apache-log-quick-summary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Web Crawler</title>
		<link>http://blog.netphase.com/2007/04/19/ruby-web-crawler/</link>
		<comments>http://blog.netphase.com/2007/04/19/ruby-web-crawler/#comments</comments>
		<pubDate>Thu, 19 Apr 2007 21:26:43 +0000</pubDate>
		<dc:creator>scottned</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.netphase.com/2007/04/19/ruby-web-crawler/</guid>
		<description><![CDATA[Here&#8217;s a web crawler I wrote awhile back.  It&#8217;s pretty simple, but does the job.  If you need something more, you might try rdig or Nutch.
You can run this as a stand-alone script and just pass in the URL to crawl as an argument.
[ruby]
require &#8216;net/http&#8217;
require &#8216;uri&#8217;
class SiteCrawler
  def initialize(url)
    [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a web crawler I wrote awhile back.  It&#8217;s pretty simple, but does the job.  If you need something more, you might try <a href="http://rdig.rubyforge.org/">rdig</a> or <a href="http://lucene.apache.org/nutch/">Nutch</a>.</p>
<p>You can run this as a stand-alone script and just pass in the URL to crawl as an argument.</p>
<p>[ruby]<br />
require &#8216;net/http&#8217;<br />
require &#8216;uri&#8217;</p>
<p>class SiteCrawler<br />
  def initialize(url)<br />
    @site_uri = URI.parse(url)<br />
    @site_uri.path = &#8220;/&#8221; if @site_uri.path == &#8220;&#8221;<br />
    @visited = Hash.new<br />
    @queue = Array.new<br />
    addPath(@site_uri.path)<br />
    puts &#8220;Initialized site crawl for #{@site_uri}&#8221;<br />
  end</p>
<p>  def addPath(path)<br />
    @queue.push path<br />
    @visited[path] = false<br />
  end</p>
<p>  def getPage(path)<br />
    begin<br />
      uri = @site_uri.clone<br />
      uri.path = uri.path + path if path != &#8220;/&#8221;<br />
      puts &#8220;getting #{uri}&#8221;<br />
      response = Net::HTTP.get_response(uri)<br />
    rescue Exception<br />
      puts &#8220;Error: #{$!}&#8221;<br />
      return &#8220;&#8221;<br />
    end<br />
    return response.body<br />
  end</p>
<p>  def queueLocalLinks(html)<br />
    html.scan(/<a href\s*=\s*["']([^"]+)"/i) {|w|<br />
      uri = URI.parse("#{w}")<br />
      if !@visited.has_key?(uri.path) and<br />
         (uri.relative? or uri.host == @site_uri.host)<br />
        addPath(uri.path)<br />
      end<br />
    }<br />
  end</p>
<p>  def crawlSite()<br />
    while (!@queue.empty?)<br />
      uri = @queue.shift<br />
      page = getPage(uri)<br />
      yield uri, page<br />
      queueLocalLinks(page)<br />
      @visited[uri] = true<br />
    end<br />
  end<br />
end</p>
<p>sc = SiteCrawler.new(ARGV[0])<br />
@pages = Array.new<br />
sc.crawlSite { |url, page_text|<br />
  @pages << url<br />
  # SITE_INDEX << { :url => url, :context => page_text }<br />
}<br />
[/ruby]</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.netphase.com/2007/04/19/ruby-web-crawler/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Accessing a Java AXIS web service with Ruby and SOAP</title>
		<link>http://blog.netphase.com/2007/02/12/accessing-a-java-axis-web-service-with-ruby-and-soap/</link>
		<comments>http://blog.netphase.com/2007/02/12/accessing-a-java-axis-web-service-with-ruby-and-soap/#comments</comments>
		<pubDate>Mon, 12 Feb 2007 19:04:38 +0000</pubDate>
		<dc:creator>scottned</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.netphase.com/2007/02/12/accessing-a-java-axis-web-service-with-ruby-and-soap/</guid>
		<description><![CDATA[I&#8217;ve been working with soap4r in Ruby to access some Java web services which use AXIS 1.1.  The particular api I&#8217;m accessing requires an initial authentication call which returns a token that must be passed in all subsequent SOAP headers.  This is a pretty common scenario, but it took me awhile to get [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working with soap4r in Ruby to access some Java web services which use AXIS 1.1.  The particular api I&#8217;m accessing requires an initial authentication call which returns a token that must be passed in all subsequent SOAP headers.  This is a pretty common scenario, but it took me awhile to get it working correctly.</p>
<p>There were basically two &#8220;tricks&#8221;.  The first which I found on a few other blogs entails creating a HeaderHandler (some suggested SimpleHeaderHandler, but that didn&#8217;t work for me because of trick #2).  Here&#8217;s what I ended up with:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ClientAuthHeaderHandler <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">SOAP::Header::Handler</span>
  HEADER = 
    <span style="color:#6666ff; font-weight:bold;">XSD::QName</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'http://www.foo.com/SecurityPass'</span>, 
                             <span style="color:#996600;">'SecurityPass'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>token<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">super</span><span style="color:#006600; font-weight:bold;">&#40;</span>HEADER<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@token</span> = token
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> on_outbound
    el = <span style="color:#6666ff; font-weight:bold;">SOAP::SOAPElement</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>HEADER, <span style="color:#0066ff; font-weight:bold;">@token</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    el.<span style="color:#9900CC;">extraattr</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'xsi:type'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;xsd:string&quot;</span>
    <span style="color:#6666ff; font-weight:bold;">SOAP::SOAPHeaderItem</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>el, <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The second &#8220;trick&#8221; was in the on_outbound method.  What I found was that AXIS was requiring the xsi:type to be included in the header that I added or it wouldn&#8217;t be recognized.  Using extraattr was the only way I could figure out how to coerce it to be included.  I suspect at some point this will be unnecessary, but for now it did the trick.</p>
<p>Here&#8217;s a simple example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> FooService
  <span style="color:#008000; font-style:italic;"># pass STDOUT to wiredump to see messages</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>uri, wiredump=<span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@uri</span> = uri
    <span style="color:#0066ff; font-weight:bold;">@wiredump</span> = wiredump
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> svc<span style="color:#006600; font-weight:bold;">&#40;</span>ref<span style="color:#006600; font-weight:bold;">&#41;</span>
    ws = <span style="color:#6666ff; font-weight:bold;">SOAP::WSDLDriverFactory</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{@uri}#{ref}?wsdl&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">create_rpc_driver</span>
    ws.<span style="color:#9900CC;">wiredump_dev</span> = <span style="color:#0066ff; font-weight:bold;">@wiredump</span>
    ws.<span style="color:#9900CC;">generate_explicit_type</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> !@sec_token.<span style="color:#0000FF; font-weight:bold;">nil</span>?
      ws.<span style="color:#9900CC;">headerhandler</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> ClientAuthHeaderHandler.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>@sec_token<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    ws
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> authenticate<span style="color:#006600; font-weight:bold;">&#40;</span>username, password<span style="color:#006600; font-weight:bold;">&#41;</span>
    ws = svc<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/services/AuthenticationService&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@sec_token</span> = ws.<span style="color:#9900CC;">authenticate</span><span style="color:#006600; font-weight:bold;">&#40;</span>username, password<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> lookup<span style="color:#006600; font-weight:bold;">&#40;</span>stuff<span style="color:#006600; font-weight:bold;">&#41;</span>
    ws = svc<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/services/LookupService&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    ws.<span style="color:#9900CC;">lookup</span><span style="color:#006600; font-weight:bold;">&#40;</span>stuff<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.netphase.com/2007/02/12/accessing-a-java-axis-web-service-with-ruby-and-soap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Geocoding webservice with Ruby and Perl</title>
		<link>http://blog.netphase.com/2007/01/13/geocoding-webservice-with-ruby-and-perl/</link>
		<comments>http://blog.netphase.com/2007/01/13/geocoding-webservice-with-ruby-and-perl/#comments</comments>
		<pubDate>Sat, 13 Jan 2007 07:34:07 +0000</pubDate>
		<dc:creator>scottned</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.netphase.com/archives/3</guid>
		<description><![CDATA[A year or so ago I was trying out the Google Maps API.  At the time, Google did not provide a means to geocode your own locations (although now they do), so I set out to create my own geocoder.  There was a good overview on how to create your own database of [...]]]></description>
			<content:encoded><![CDATA[<p>A year or so ago I was trying out the <a href="http://www.google.com/apis/maps/">Google Maps API</a>.  At the time, Google did not provide a means to geocode your own locations (although now they do), so I set out to create my own geocoder.  There was a <a title="good overview" href="http://cpan.uwinnipeg.ca/htdocs/Geo-Coder-US/Geo/Coder/US/Import.html">good overview</a> on how to create your own database of locations based on the Census Bureau&#8217;s TIGER/line data.  I took quite awhile to download all the data and then construct the berkley database (about 1gb in size), but in the end it worked great.  Next, I made use of the Geo::Coder::US Perl module, wrote a Perl web service and created my Rails app to pull everthing together.</p>
<p>Anyway, I finally sat down to blog about what I did (a year ago) and, of course,  some other folks <a href="http://www.developer.com/tech/article.php/3557171">beat me to it</a>.  There&#8217;s also a pretty cool Ruby <a href="http://geocoder.rubyforge.org/">geocoder</a> Gem that I&#8217;ve just been playing with.  It uses the the geocoding webservices provided by <a href="http://developer.yahoo.net/maps/rest/V1/geocode.html">Yahoo</a> and <a href="http://geocoder.us/">geocoder.us</a>.  Of course, their are limits on these: the Yahoo service is limited to 5,000 requests per day and the geocoder.us service is for non-commercial use only.  In any case, either should acceptable for many purposes.  It would be nice if support is added for the Google service, which allows 50,000 request per day.  I&#8217;ll probably keep my service around, but will likely switch to the gem version so I can let Yahoo worry about keeping the database up-to-date.</p>
<p>Here&#8217;s a basic example using the gem version in irb:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'geocoder'</span>  
=<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>  
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> geocoder = <span style="color:#6666ff; font-weight:bold;">Geocoder::Yahoo</span>.<span style="color:#9900CC;">new</span> <span style="color:#996600;">&quot;my_yahoo_app_id&quot;</span>  
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;Geocoder::Yahoo:0x6f8038 @appid=&quot;my_yahoo_app_id&quot;&gt;  </span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> result = 
  geocoder.<span style="color:#9900CC;">geocode</span> <span style="color:#996600;">&quot;1600 pennsylvania ave nw washington dc&quot;</span>  
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#008000; font-style:italic;">#&lt;struct Geocoder::Result latitude=&quot;38.898563&quot;, </span>
  longitude=<span style="color:#996600;">&quot;-77.037223&quot;</span>, address=<span style="color:#996600;">&quot;1600 PENNSYLVANIA AVE NW&quot;</span>, 
  city=<span style="color:#996600;">&quot;WASHINGTON&quot;</span>, state=<span style="color:#996600;">&quot;DC&quot;</span>, zip=<span style="color:#996600;">&quot;20500-0003&quot;</span>, 
  country=<span style="color:#996600;">&quot;US&quot;</span>, precision=<span style="color:#996600;">&quot;address&quot;</span>, warning=nil<span style="color:#006600; font-weight:bold;">&gt;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> result.<span style="color:#9900CC;">lat</span> 
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;38.898563&quot;</span>  
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> result.<span style="color:#9900CC;">lng</span>  
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;-77.037223&quot;</span></pre></div></div>

<p>That&#8217;s all that&#8217;s needed.  You can be getting your own coordinates in minutes.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.netphase.com/2007/01/13/geocoding-webservice-with-ruby-and-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
