<?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>Justin Spradlin &#187; Open Source</title>
	<atom:link href="http://www.justinspradlin.com/tag/open-source/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.justinspradlin.com</link>
	<description>Coding and such...</description>
	<lastBuildDate>Wed, 03 Mar 2010 17:52:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Introducing GMoney – A RubyGem for Interacting with the Google Finance API</title>
		<link>http://www.justinspradlin.com/programming/introducing-gmoney-a-rubygem-for-interacting-with-the-google-finance-api/</link>
		<comments>http://www.justinspradlin.com/programming/introducing-gmoney-a-rubygem-for-interacting-with-the-google-finance-api/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 06:00:30 +0000</pubDate>
		<dc:creator>Justin Spradlin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.justinspradlin.com/?p=87</guid>
		<description><![CDATA[That's why I wrote GMoney, a <a href="http://docs.rubygems.org/">RubyGem</a> for interacting with the <a href="http://code.google.com/apis/finance/">Google Finance API]]></description>
			<content:encoded><![CDATA[<div class="blockquote"> 
<p><strong>Update March 03, 2010:</strong></p><p>Due to changes in the Google Finance API only GMoney gem versions >= 0.4.3 are working.  If you run <code>gem update gmoney</code> to get the latest version or install the gem from scratch you will be fine.</p>
</div>
<p>Let&#8217;s pretend the year is 2007 and I have a close &#8220;friend&#8221; who is really excited about the upcoming <a href="http://www.fifa.com/worldcup/">2010 World Cup</a>.  My friend is not too naive and realizes that airfare to Africa, lodging, tickets to the games, and Safaris are all very expensive.  My friend has a little extra cash to save each month and with a three year time horizon he figures he can stow away a little money in a moderately aggressive mutual fund in order to take advantage of some growth opportunities.</p>
<p>Unfortunately for my friend the next two years would be some of the <a href="http://en.wikipedia.org/wiki/Financial_crisis_of_2007%E2%80%932010">worst the world economy has ever seen</a>.  Like many people, my friend <a href="http://idioms.thefreedictionary.com/took+a+bath#MainTxt">took a bath</a> on his investments and his hopes and dreams of going to the World Cup went down the drain along with his money.</p>
<p>It&#8217;s too bad my friend didn&#8217;t keep better track of his investments.  Perhaps if he had he could have pulled his money out of the stock market and salvaged his vacation.  Since my friend is an avid Ruby lover I figured I&#8217;d help make sure he doesn&#8217;t end up in this situation ever again.  That&#8217;s why I wrote GMoney, a <a href="http://docs.rubygems.org/">RubyGem</a> for interacting with the <a href="http://code.google.com/apis/finance/">Google Finance API</a>.</p>
<span id="more-87"></span>
<h3>Background</h3>
<p>The Google Finance API allows users to interact with <a href="http://www.google.com/finance">Google Finance</a> and can help developers programmatically &#8220;request a list of a user&#8217;s portfolios, retrieve performance and return statistics on an existing portfolio, query the positions and transactions in a portfolio, and create new portfolios and transactions.&#8221;</p>
<p>GMoney is a wrapper for this API and allows developers to take advantage of the API&#8217;s functionality using the <a href="http://www.ruby-lang.org/en/">Ruby programming language</a>.  To minimize the learning curve I tried to give GMoney an interface similar to that of <a href="http://ar.rubyonrails.org/">ActiveRecord</a> so that developers would have a familiar starting point.</p>
<h3>Google Finance Model</h3>
<p>The Google Finance model is pretty simple.  It is made up of the following three entities:</p>
<p><strong>Portfolios</strong></p>
<div class="blockquote"> 
<p>A portfolio is a collection of positions that the user holds in various securities, plus metadata.  Each portfolio entry contains the portfolio&#8217;s title along with data such as currency and total market value. Each portfolio entry also contains a link to the portfolio&#8217;s position feed (i.e. a Portfolio &#8220;has_many&#8221; Positions).</p>
</div>
<p><strong>Positions</strong></p>
<div class="blockquote"> 
<p>A position is a collection of information about a security that the user holds.  Each position entry contains the ticker exchange and symbol for a stock, mutual fund, or other security, along with the number of units of that security that the user holds. Each position entry also contains a link to the position&#8217;s transaction feed (i.e. a Position &#8220;has_many&#8221; Transactions).</p>
<p>You can&#8217;t directly create or update position entries; positions are derived from transactions.</p>
</div>
<p><strong>Transactions</strong></p>
<div class="blockquote"> 
<p>A transaction is a collection of information about an instance of buying or selling a particular security.  Each transaction entry contains a transaction type (such as buy or sell), a number of units, the price, and so on.</p>
</div>
<h3>Installation</h3>
<p>To install GMoney simply run the following command:</p>
<div class="codeBlock"> 
	<pre><code>gem install gmoney</code></pre> 
</div> 
<h3>Login</h3>
<p>You will need a <a href="http://www.google.com/accounts">Google account</a> in order to use the Google Finance API.  Once you have obtained an account you can login via GMoney using the following code:</p>
<div class="codeBlock"> 
	<pre><code>require 'rubygems'
require 'gmoney'

GMoney::GFSession.login('&lt;YOUR GOOGLE USER ID&gt;','&lt;YOUR GOOGLE PASSWORD&gt;')</code></pre> 
</div>
<h3>Create a portfolio</h3>
<p>If you were to login to Google Finance for the first time using a browser you would see the following screen showing the default Google Finance Portfolio called &#8220;My Portfolio&#8221;:</p>
<p><a href="http://www.justinspradlin.com/images/default_portfolio.jpg"><img src="http://www.justinspradlin.com/images/default_portfolio_s.jpg" class="center" alt="Create Portfolio" /></a></p>
<p>To create your own Portfolio using GMoney use the following code:</p>
<div class="codeBlock"> 
	<pre><code>portfolio = GMoney::Portfolio.new
portfolio.title = "My New Ruby Portfolio"
portfolio.save</code></pre> 
</div>
<p>After executing the code and refreshing the browser you should see that the &#8220;My New Ruby Portfolio&#8221; has been created.</p>
<p><a href="http://www.justinspradlin.com/images/create_portfolio.jpg"><img src="http://www.justinspradlin.com/images/create_portfolio_s.jpg" class="center" alt="Create Portfolio" /></a></p>
<h3>Create a transaction(s)</h3>
<p>As mentioned above transactions are used to buy and sell stocks within a given portfolio.  The positions within your portfolio will be based on the transaction information.  Running the following code will buy shares of Apple, Red Hat, Google, and Microsoft and place them into your portfolio:</p>
<div class="codeBlock"> 
	<pre><code>stocks = {"NASDAQ:GOOG" => 529.00, 
          "NASDAQ:AAPL" => 192.00, 
          "NYSE:RHT" => 27.00, 
          "NASDAQ:MSFT" => 28.00}
 
stocks.each do |symbol, price| 
  transaction = GMoney::Transaction.new
  #pid is the human readable id of the portfolio created above
  transaction.portfolio = portfolio.pid
  transaction.type = GMoney::BUY
  transaction.shares = 50
  transaction.ticker = symbol
  transaction.price = price  
  transaction.save
end</code></pre> 
</div>
<p>The result of running the code is as follows:</p>
<p><a href="http://www.justinspradlin.com/images/buy_stocks.jpg"><img src="http://www.justinspradlin.com/images/buy_stocks_s.jpg" class="center" alt="Create Portfolio" /></a></p>
<h3>Update Transaction</h3>
<p>If you actually meant to buy 100 shares of Goolge instead of 50 you can update the transaction using the following code:</p>
<div class="codeBlock"> 
	<pre><code>#I know the transaction id is '4'
transaction = GMoney::Transaction.find "#{portfolio.pid}/NASDAQ:GOOG/4"
transaction.shares = 100
transaction.save</code></pre> 
</div>
<p>The result:</p>
<p><a href="http://www.justinspradlin.com/images/update_goog.jpg"><img src="http://www.justinspradlin.com/images/update_goog_s.jpg" class="center" alt="Create Portfolio" /></a></p>
<h3>Delete Transaction</h3>
<p>We can delete a transaction like so:</p>
<div class="codeBlock"> 
	<pre><code>GMoney::Position.find("#{portfolio.pid}/NASDAQ:MSFT").delete</code></pre> 
</div>
<p>Now we have no more MSFT stock:</p>
<p><a href="http://www.justinspradlin.com/images/delete_trans.jpg"><img src="http://www.justinspradlin.com/images/delete_trans_s.jpg" class="center" alt="Create Portfolio" /></a></p>
<h3>Reading all the Positions in a Porfolio</h3>
<p>If we&#8217;d like to see all of the positions within our portfolio we can execute this code:</p>
<div class="codeBlock"> 
	<pre><code>portfolio.positions.each do |position| 
  puts position.title
end</code></pre> 
</div>
<p><strong>Output:</strong></p>
<div class="outputBlock"> 
<pre>Red Hat, Inc.
Apple Inc.
Google Inc.</pre> 
</div> 
<h3>Gotcha</h3>
<p>There is a bit of a &#8220;gotcha&#8221; right now with the Google Finance API.  For whatever reason, stock returns information is not being sent by Google.  I figured it was something I was doing wrong in my code, but then I used <a href="http://code.google.com/apis/gdata/docs/client-libraries.html">Google&#8217;s official API client</a> and was still unable to retrieve the returns data.  I&#8217;ll be sure to file a <a href="http://code.google.com/p/gdata-issues/issues/list">bug report</a> with Google over the next few days.</p>
<h3>Conclusion</h3>
<p>GMoney offers a lot more features than what has been written about here.  Be sure to check out the documentation and the source code on <a href="http://github.com/jspradlin/gmoney">Github</a>.  The full script of the code used for this blog entry can also be found on <a href="http://github.com/jspradlin/justinspradlin.com-blog-examples/tree/master/2010-02-01_Introducing-GMoney-A-RubyGem-for-Interacting-with-the-Google-Finance_API/">Github</a>.</p>
<p>Overall building GMoney was an incredible learning experience.  It was my first significant Ruby project and also my first Open Source project.  As of today GMoney has been downloaded almost <a href="http://gemcutter.org/gems/gmoney">200 times</a>.  I&#8217;ve tested GMoney on Windows and Linux using Ruby versions 1.8.6, 1.8.7, and 1.9.1 (via <a href="http://rvm.beginrescueend.com/">RVM</a>).  Feedback and bug reports are more than welcome.</p>
<p>With GMoney in place my friend will be sure to keep much better track of his investments and is looking forward to the <a href="http://en.wikipedia.org/wiki/2014_FIFA_World_Cup">2014 World Cup in Brazil</a>.</p>
<p><i>Disclaimer:</i> I am in no way endorsing any of the companies mentioned in this blog entry and all company references are for example purposes only.</p>]]></content:encoded>
			<wfw:commentRss>http://www.justinspradlin.com/programming/introducing-gmoney-a-rubygem-for-interacting-with-the-google-finance-api/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
