<?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>Geoff Evason &#187; api</title>
	<atom:link href="http://geoff.evason.name/tag/api/feed/" rel="self" type="application/rss+xml" />
	<link>http://geoff.evason.name</link>
	<description></description>
	<lastBuildDate>Wed, 30 Nov 2011 03:41:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Simple Currency Conversion Rate API Consumption For Ruby / Rails</title>
		<link>http://geoff.evason.name/2008/10/27/simple-currency-conversion-rate-api-consumption-for-ruby-rails/</link>
		<comments>http://geoff.evason.name/2008/10/27/simple-currency-conversion-rate-api-consumption-for-ruby-rails/#comments</comments>
		<pubDate>Mon, 27 Oct 2008 13:38:51 +0000</pubDate>
		<dc:creator>Geoff</dc:creator>
				<category><![CDATA[tech]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[currency]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://geoff.evason.name/?p=53</guid>
		<description><![CDATA[I had the need to consume some exchange rate data for an internal project so I began looking for an about. My searching found no api for Google Finance, Yahoo Finance, XE, or Oanda.  
Fortunately I found the currency converter from Xavier Media and their simple currency exchange rate xml API, which includes historical data [...]]]></description>
			<content:encoded><![CDATA[<p>I had the need to consume some exchange rate data for an internal project so I began looking for an about. My searching found no api for Google Finance, Yahoo Finance, XE, or Oanda. <img src='http://geoff.evason.name/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
<p>Fortunately I found the <a href="http://finance.xaviermedia.com/">currency converter from Xavier Media</a> and their simple <a href="http://forum.xaviermedia.com/xavier-finance-currency-exchange-rate-api-t10979.html">currency exchange rate xml API</a>, which includes historical data too. <img src='http://geoff.evason.name/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /> </p>
<p>I put together an absolutely minimal lib to get the data I need.  I just needed the AU/US rate.  The xml provides all data as base to EUR, but with some simple math I can find the rate I need with reasonable accuracy.  In this case &#8216;accuracy&#8217; is based on spot checking it against the yahoo rates.</p>
<p>I thought it worth sharing in case others are looking for something similar.</p>
<p><strong>UPDATE:</strong> I changed the xml string below to better handle single digit date months. Xavier needs &#8216;01&#8242; instead of &#8216;1&#8242;</p>
<pre><code class="ruby">require "cgi"
require "uri"
require "net/https"
require "rexml/document"

module XavierMedia
  # Returns the exchange rate (AUD/USD) on the given date.
  def self.exchange_rate_on(date)
    url = URI.parse("http://api.finance.xaviermedia.com/api/#{date.year}/#{date.strftime("%m")}/#{date.strftime("%d")}.xml")

    resp = Net::HTTP.get(url)
    xml  = REXML::Document.new(resp)

    us_to_eur = 1.0
    au_to_eur = 1.0
    xml.elements.each("//exchange_rates/fx") { |el|
      if el.elements[1].text == "USD"
        us_to_eur = el.elements[2].text.to_f rescue 1.0
      end
      if el.elements[1].text == "AUD"
        au_to_eur = el.elements[2].text.to_f rescue 1.0
      end
    }

    return us_to_eur/au_to_eur
  end
end
</code></pre>
<p> </p>
]]></content:encoded>
			<wfw:commentRss>http://geoff.evason.name/2008/10/27/simple-currency-conversion-rate-api-consumption-for-ruby-rails/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

