<?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>Martin&#039;s World &#187; rails</title>
	<atom:link href="http://www.freakent.co.uk/archives/tag/rails/feed" rel="self" type="application/rss+xml" />
	<link>http://www.freakent.co.uk</link>
	<description>Martin&#039;s world of Gadgets, Internet and Technology</description>
	<lastBuildDate>Sun, 14 Feb 2010 16:22:14 +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>Using the eBay SOAP API from Ruby-on-Rails</title>
		<link>http://www.freakent.co.uk/archives/341</link>
		<comments>http://www.freakent.co.uk/archives/341#comments</comments>
		<pubDate>Fri, 02 Oct 2009 11:41:28 +0000</pubDate>
		<dc:creator>martin</dc:creator>
				<category><![CDATA[Ruby-on-Rails]]></category>
		<category><![CDATA[ebay]]></category>
		<category><![CDATA[ebay4r]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[soap]]></category>
		<category><![CDATA[soap4r]]></category>

		<guid isPermaLink="false">http://www.freakent.co.uk/?p=341</guid>
		<description><![CDATA[This article applies to:
Rails 2.3.4, Ruby 1.8.6, soap4r gem 1.5.8, ebay4r 1.1

I have been taking a look at ways to integrate a Rails application with eBay. They support a number of different programming interfaces and, if you are using Javascript, PHP, Java or .Net, have some good examples to follow. I managed to get a [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://developer.ebay.com/DevZone/XML/docs/Reference/eBay/i/logoEbayDev_x45_ss.gif" style="float:right"><em>This article applies to:<br />
Rails 2.3.4, Ruby 1.8.6, <a href="http://dev.ctor.org/soap4r">soap4r </a>gem 1.5.8, <a href="http://github.com/up_the_irons/ebay4r">ebay4r </a>1.1<br />
</em></p>
<p>I have been taking a look at ways to integrate a Rails application with eBay. They support a number of different programming interfaces and, if you are using Javascript, PHP, Java or .Net, have some good examples to follow. I managed to get a Javascript interface working quite easily from inside a Rails _form view, but it just does not feel like the most elegant solution to me. </p>
<p>Since eBay provide a well documented SOAP API I thought it would be a good opportunity to investigate Ruby&#8217;s support for SOAP.  SOAP support is a standard feature of Ruby and is provided by a library called <a href="http://dev.ctor.org/soap4r">soap4r</a>. I tried implementing my own call to eBay&#8217;s GetItem interface, but I encountered a few problems and soap4r&#8217;s documentation didn&#8217;t help much either. Then I discovered a library called <a href="http://github.com/up_the_irons/ebay4r">ebay4r </a>which is built on top of soap4r. The rest of this article describes how I successfully integrated my Rails application with eBay using ebay4r.</p>
<p><span id="more-341"></span></p>
<p><strong>Step 1) Load the gems into your Rails application</strong><br />
ebay4r needs a later version of the soap4r API than ships with Ruby. It&#8217;s easy to install the gem but there are some <a href="http://dev.ctor.org/soap4r/ticket/433">warnings </a>on the ebay4r and soap4r web sites about how to avoid API version conflicts caused by gem. Adding both via config.gem seems to avoid this problem completely so the workarounds suggested should not be needed.  Edit your environment.rb and add the two new config.gem lines shown below :<br />
<code>config.gem "soap4r", :lib => false<br />
config.gem "ebay", :lib=> false<br />
</code><br />
Next, use rake to ensure these gems are installed:<br />
<code>rake gems:install<br />
</code></p>
<p><strong>Step 2) Obtain eBay Authentication Keys<br />
</strong>The eBay SOAP api requires that you authenticate with a set of four keys; AppId, DevId, CertId and AuthToken. You get these authentication keys by signing on to the <a href="http://developer.ebay.com/">eBay Developers Program</a>. You should be able to view the first three keys from your <a href="https://developer.ebay.com/DevZone/account/Default.aspx">eBay Developer Account</a> page. The AuthToken is a little bit special. Whilst the other keys are used to authenticate your use of the API, the AuthToken is used to actually determine which user your API calls will run as. Without an AuthToken you can only retrieve public information. I&#8217;m not even sure if ebay4r supports making API calls without an AuthToken. To get your AuthToken, click on the &#8220;<a href="https://developer.ebay.com/DevZone/account/tokens/">get a user token</a>&#8221; link on your profile page and follow the instructions.</p>
<p><strong>Step 3) Set up eBay configuration parameters<br />
</strong>ebay4r suggests saving the four authentication keys in a Ruby file called &#8216;myCredentials.rb&#8217; and then loading the file to create some global variables. Whilst it works, it&#8217;s not very Rails. I created a custom initialiser and a yaml config file to hold my values.<br />
<code><br />
#config/initializers/ebay_config.rb<br />
require 'yaml'<br />
EBAY_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/ebay.yml")[RAILS_ENV]<br />
</code><br />
<code><br />
# config/ebay.yml<br />
common: &#038;common<br />
  DevId: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br />
  AppId: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br />
  CertId: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<br />
  AuthToken: xxxxxxxxxxxx ...more... xxxxxxxxxxx</p>
<p>development:<br />
  << : *common</p>
<p>test:<br />
  << : *common</p>
<p>production:<br />
  << : *common<br />
</code></p>
<p><strong>Step 4) Create an application model to represent the eBay Interface<br />
</strong>Whilst I could just invoke the ebay4r methods directly from my business logic, I prefer to insulate the rest of my code from an external API. I created an EbayTrader class in my application's models directory:<br />
<code># app/models/ebay_trader.rb<br />
require 'eBayAPI'</p>
<p>class EbayTrader</p>
<p>  attr_accessor :eBay</p>
<p>  def initialize<br />
    @eBay = EBay::API.new(EBAY_CONFIG['AuthToken'], EBAY_CONFIG['DevId'], EBAY_CONFIG['AppId'], EBAY_CONFIG['CertId'])<br />
  end</p>
<p>  def test<br />
    @eBay.GeteBayOfficialTime<br />
  end</p>
<p>end<br />
</code><br />
Two things to notice:</p>
<ol>
<li> the <em>test</em> method. Throughout the eBay API documentation you will see the GeteBayOfficialTime call being used to test the interface is configured correctly. My test method does exactly the same and makes it easy to test in the script/console.</li>
<li> the EBAY_CONFIG built in step 3 is used to provide the authentication keys during initialisation. </li>
</ol>
<p><strong>Step 5) Test Your eBay API Configuration<br />
</strong>Before we go any further it's worthwhile testing to make sure you can actually connect to eBay. Use script/console to run our test method.<br />
<code>$ script/console<br />
Loading Development Environment (Rails 2.3.4)<br />
>> et=EbayTrader.new<br />
...<br />
>> et.test<br />
...</code></p>
<p>You should see irb output to indicate that the code connected to eBay and returned the eBay Official Time. If not check the errors and try again.</p>
<p><strong>Step 6) Implement Specific Calls to the API<br />
</strong>Now we are ready to put our set up to work. The ebay4r library makes calling the eBay APIs very simple. The full list of API calls available can be found on the <a href="http://developer.ebay.com/support/docs/Default.aspx">eBay Developer Program Documentation</a> pages. </p>
<p>eBay are very careful to version their API so that you will be less affected by any changes they make. The underlying SOAP API requires you to specify which version of the API you are using when you make an API call. Version 1.1 of ebay4r was built for eBay API v583, so just make sure that the API call and the parameters you want to use are supported by v583 of the eBay API. Rebuilding ebay4r for the latest eBay API should be straightforward (thanks to soap4r) but is beyond the scope of this article. </p>
<p>I want to use the API to get detailed information about an auction item. The underlying eBay API for this is <a href="http://developer.ebay.com/DevZone/XML/docs/Reference/eBay/GetItem.html">GetItem</a>, which is part of the Trading API set. If you take a look at the API documentation you can see the in and out parameters for the API call. For GetItem the main in parameter is ItemID, most of the others are optional. ebay4r allows parameters to be passed to an API call as a Ruby symbol. The out parameters are converted you object methods making the results very easy to navigate.  So in our model we can use:<br />
<code># app/models/ebay_trader.rb<br />
...<br />
  def get_item(item_id)<br />
    result = @eBay.GetItem(:ItemID => item_id)<br />
    result.item<br />
  end<br />
...<br />
</code> </p>
<p><strong>Summary</strong><br />
That's the basic eBay integration done. Accessing the EbayTrader model from a controller or some other piece of business logic should be straight forward. I will add a step 7 to this article in the not too distant future to demonstrate this and to explain more about how to access the results of an Ebay API call.  In the mean time just take a look at the examples provided by ebay4r.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.freakent.co.uk/archives/341/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails validation error messages</title>
		<link>http://www.freakent.co.uk/archives/326</link>
		<comments>http://www.freakent.co.uk/archives/326#comments</comments>
		<pubDate>Tue, 29 Sep 2009 13:52:47 +0000</pubDate>
		<dc:creator>martin</dc:creator>
				<category><![CDATA[Ruby-on-Rails]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.chart-geek.com/?p=326</guid>
		<description><![CDATA[There&#8217;s no denying that Rails&#8217; built-in Forms Helpers are incredibly good. However if they have one flaw, it is the way error messages from validations are handled. The default messages generated by Rails are terse to say the least! If you provide your own message (with :message=>&#8221;blah blah&#8221;, Rails automatically appends the name of the [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://rubyonrails.org/images/rails.png" alt="Rails" style="float:right"/>There&#8217;s no denying that Rails&#8217; built-in Forms Helpers are incredibly good. However if they have one flaw, it is the way error messages from validations are handled. The default messages generated by Rails are terse to say the least! If you provide your own message (with :message=>&#8221;blah blah&#8221;, Rails automatically appends the name of the underlying field to the front of the message, which is restrictive and looks ugly. </p>
<p>I spent some time looking around for a good solution to this problem. I assumed it would be a simple configuration issue but it turns out the format of these messages is hard baked into the ActiveRecord::Errors.full_messages method.</p>
<p><span id="more-326"></span></p>
<p>The best solution by a mile that I found was on <a href="http://adamhooper.com/eng/articles/5">Adam Hooper&#8217;s blog</a>. Adam quite rightly points out that this is a fundamental internationalisation (&#8221;i18n&#8221;) issue. Although his solution might seem a little more long winded than some, it is the only solution that I found that would work with multiple languages, leveraging Rails&#8217; i18n features.</p>
<p>Here is the full link to the article on Adam&#8217;s blog.<br />
<a href="http://adamhooper.com/eng/articles/5">http://adamhooper.com/eng/articles/5</a> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.freakent.co.uk/archives/326/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Install Ruby-on-Rails and Phusion Passenger (mod_rails) on Ubuntu 9.04</title>
		<link>http://www.freakent.co.uk/archives/306</link>
		<comments>http://www.freakent.co.uk/archives/306#comments</comments>
		<pubDate>Wed, 26 Aug 2009 12:43:22 +0000</pubDate>
		<dc:creator>martin</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ruby-on-Rails]]></category>
		<category><![CDATA[mod_rails]]></category>
		<category><![CDATA[passenger]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.chart-geek.com/?p=306</guid>
		<description><![CDATA[The following is how I installed and configured Ruby-on-Rails on my Ubuntu 9.04 Server (x86 64 bit).
1. Update Ubuntu before we start
$ sudo apt-get update
$ sudo apt-get upgrade
2. Check whether MySQL is already installed
 $ mysql --version
mysql  Ver 14.12 Distrib 5.0.75, for debian-linux-gnu (x86_64) using readline 5.2
MySQL was already installed so OK to proceed.
2. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://rubyonrails.org/images/rails.png" alt="Rails" style="float:right"/>The following is how I installed and configured Ruby-on-Rails on my Ubuntu 9.04 Server (x86 64 bit).</p>
<p><strong>1. Update Ubuntu before we start</strong><br />
<code>$ sudo apt-get update<br />
$ sudo apt-get upgrade</code></p>
<p><strong>2. Check whether MySQL is already installed</strong><br />
<code> $ mysql --version<br />
mysql  Ver 14.12 Distrib 5.0.75, for debian-linux-gnu (x86_64) using readline 5.2</code><br />
MySQL was already installed so OK to proceed.</p>
<p><strong>2. Install Ruby</strong><br />
<code>$ sudo apt-get install ruby irb rdoc</code><br />
$ ruby -v<br />
ruby 1.8.7 (2008-08-11 patchlevel 72) [x86_64-linux]</code></p>
<p><strong>4. Install Ruby Gems (the Ruby package management system)</strong><br />
There are two ways to install Ruby Gems on Ubuntu, either through apt-get or by downloading the Gem archive and running the set up script. I tried the apt-get method as that looked quicker:</p>
<p><code>$ sudo apt-get install rubygems1.8<br />
$ sudo gem update --system</code></p>
<p>Unfortunately the apt-get package disables the <code>gem update --system</code> and insists that you use apt to update instead. This might be more preferable to the Linux Sysadmin crowd but I prefer to keep everything pure Ruby. It's a few extra commands but nothing too bad. </p>
<p><code>$ wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz<br />
$ tar xzvf rubygems-1.3.5.tgz<br />
$ cd rubygems-1.3.5/<br />
$ sudo ruby setup.rb<br />
$ gem -v<br />
$ sudo gem update --system</code></p>
<p><strong>5. Install and test Rails</strong><br />
<code>$ sudo gem install rails<br />
$ cd<br />
$ rails hello<br />
$ cd hello<br />
$ script/server<br />
...<br />
/usr/lib/ruby/gems/1.8/gems/rails-2.3.3/lib/initializer.rb:271:in `require_frameworks': no such file to load -- net/https (RuntimeError)</code></p>
<p>A quick Google of this error message revealed that I probably should have installed the Ruby OpenSSL library when I originally installed Ruby.<br />
<code>$ sudo apt-get install libopenssl-ruby<br />
$ script/server</code><br />
This time the Mongrel server started fine. </p>
<p><strong>6. Install Passenger (mod_rails)</strong><br />
The Rails community seem to all be switching from Mongrel to Passanger, the mod_rails Apache plug-in module. I was keen to see how a Passenger deployment compares to Mongrel.<br />
<code>$ sudo gem install passenger<br />
...<br />
Failed to build gem native extension.</code><br />
Apparently the libraries needed to build a Gem that contains native code isn't included with the main Ruby package, you need to install the Ruby Dev package too (something else I should have installed when I installed Ruby).<br />
<code>$ sudo apt-get install ruby1.8-dev<br />
$ sudo gem install passenger<br />
$ sudo passenger-install-apache2-module<br />
</code><br />
Following the prompts and messages on screen it turned out I didn't have a number of Apache libraries. The passenger installer is very good and gives you the exact commands to run:<code><br />
 * To install Apache 2 development headers:<br />
   Please run apt-get install apache2-prefork-dev as root.<br />
 * To install Apache Portable Runtime (APR) development headers:<br />
   Please run apt-get install libapr1-dev as root.<br />
 * To install Apache Portable Runtime Utility (APU) development headers:<br />
   Please run apt-get install libaprutil1-dev as root.<br />
</code><br />
I only needed to run the first command as the other two packages must have been pulled in as dependencies.</p>
<p><code><br />
$ sudo passenger-install-apache2-module<br />
...<br />
The Apache 2 module was successfully installed.</p>
<p>Please edit your Apache configuration file, and add these lines:</p>
<p>   LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.4/ext/apache2/mod_passenger.so<br />
   PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.4<br />
   PassengerRuby /usr/bin/ruby1.8</p>
<p>...</p>
<p><strong>7. Adding Passenger to the Ubuntu Apache2 Configuration</strong><br />
Whilst you could just add the Passenger configuration to your apache2.conf file, the Debian/Ubuntu way is to have a separate file for each module. So create a new file called passenger.load in /etc/apache2/mods-available and add the lines provided by the Passenger installer above. Then run the a2enmod to enable the Passenger module. This little script actually creates links in the mods-enabled subdirectory which are then picked up by the main Apache configuration file.<br />
<code>$sudo a2enmod<br />
Which module would you like to enable?<br />
Your choices are: actions asis auth_anon auth_dbm auth_digest auth_ldap cache cern_meta cgid cgi dav_fs dav deflate disk_cache expires ext_filter file_cache headers imap include info ldap mem_cache mime_magic <strong>passenger</strong> php5 proxy_connect proxy_ftp proxy_http proxy rewrite speling ssl suexec unique_id userdir usertrack vhost_alias<br />
          Module name? passenger<br />
          Module passenger installed; run /etc/init.d/apache2 force-reload to enable.<br />
...<br />
</code><br />
To finish enabling your Passenger module you just need to tell Apache to reload.<br />
<code>    $ sudo /etc/init.d/apache2 reload<br />
</code></p>
<p><strong>8. Adding Your Rails App to Passenger</strong><br />
Again, you could just add your site to the Apache config file using a <code>virtual host<code> directive, but the Debian/Ubuntu way is to use a sites-available/sites-enabled set up, similar to the Apache modules set up. A default web site was configured using <code>/etc/apache2/sites-available/default</code>, which publishes the directory <code>/var/www</code> via Apache. </p>
<p>I wanted to make my Rails app available as a subdirectory off the server's domain, e.g. http://server.local/testapp. All you have to do is create a soft link from your Rails app's public directory to the /var/www directory and the tell Passenger to treat this direftory as a Rails app.</p>
<p><code>$ ln -s /usr/martin/testapp/public /var/www/testapp</code></p>
<p>Add the following to <code>/etc/apache2/sites-available/default</code>:<br />
<code>...<br />
RailsBaseURI /testapp<br />
</code><br />
<em>If you want to use http://testapp.domain/ then you'll have to check the documentation on the <a href="http://www.modrails.com/documentation/Users%20guide%20Apache.html#_deploying_to_a_virtual_host_8217_s_root">Phusion Passenger</a> site and create a new sites_available file.  </em> </p>
<p><strong>Useful Resources</strong><br />
<a href="http://www.railsgarden.com/2008/04/12/configurating-passenger-mod_rails-on-slicehost-with-ubuntu-710/">Blog entry on installing and configuring Passenger on Ubuntu 7.10.</a> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.freakent.co.uk/archives/306/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Background Jobs and Queues with Ruby On Rails</title>
		<link>http://www.freakent.co.uk/archives/130</link>
		<comments>http://www.freakent.co.uk/archives/130#comments</comments>
		<pubDate>Sun, 16 Dec 2007 08:13:21 +0000</pubDate>
		<dc:creator>martin</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ruby-on-Rails]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.freakent.co.uk/archives/130</guid>
		<description><![CDATA[http://labnotes.org/cgi-bin/trac.cgi/wiki/Ruby/ReliableMessaging
http://rubyforge.org/projects/backgroundrb/
http://ap4r.rubyforge.org/wiki/wiki.pl?HomePage
http://cleanair.highgroove.com/articles/2006/06/23/running-background-jobs
]]></description>
			<content:encoded><![CDATA[<p><img src="http://rubyonrails.org/images/rails.png" alt="Rails" style="float:right"/>http://labnotes.org/cgi-bin/trac.cgi/wiki/Ruby/ReliableMessaging</p>
<p>http://rubyforge.org/projects/backgroundrb/</p>
<p>http://ap4r.rubyforge.org/wiki/wiki.pl?HomePage</p>
<p>http://cleanair.highgroove.com/articles/2006/06/23/running-background-jobs</p>
]]></content:encoded>
			<wfw:commentRss>http://www.freakent.co.uk/archives/130/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
