<?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>BadPopcorn &#187; Technology</title>
	<atom:link href="http://badpopcorn.com/blog/category/technology/feed/" rel="self" type="application/rss+xml" />
	<link>http://badpopcorn.com/blog</link>
	<description>Solutions for anything... except popcorn.</description>
	<lastBuildDate>Mon, 12 Apr 2010 15:38:15 +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>Send your emails using Postmark</title>
		<link>http://badpopcorn.com/blog/2010/04/12/send-your-emails-using-postmark/</link>
		<comments>http://badpopcorn.com/blog/2010/04/12/send-your-emails-using-postmark/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 15:33:38 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://badpopcorn.com/blog/?p=1013</guid>
		<description><![CDATA[

Wildbit recently launched a great new email delivery service named Postmark.  Once I read what it was about I knew that we had to integrate in to beyond the whiteboard.
First a little background.
Sending emails from your web application is one of those things that all the programming guides show you how to set up and [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://postmarkapp.com/images/landing/logo.png" style="margin-top:10px;"/><br />
<br />
<a href="http://wildbit.com/" target="_blank">Wildbit</a> recently launched a great new email delivery service named <a href="http://postmarkapp.com" target="_blank">Postmark</a>.  Once I read what it was about I knew that we had to integrate in to <a title="CrossFit stats logger" href="http://beyondthewhiteboard.com" target="_blank">beyond the whiteboard</a>.</p>
<h3>First a little background.</h3>
<p>Sending emails from your web application is one of those things that all the programming guides show you how to set up and get going in a few easy steps.  However, they never point out the problems that you might come across while trying to send out emails.</p>
<p>When launched the beta version of beyond the whiteboard we setup Gmail to be our smarthost for sending emails.  That was pretty easy to get going and worked out pretty well, there where copies of the sent emails in that Gmail account and emails where sent out.  However, there where a couple of things which I didn&#8217;t really like.  First off Gmail, replaces the from field with the user you are sending email through.  For example, if your Gmail user is &#8216;mycoolapp&#8217;, that&#8217;s what the from will be, even if you in your application set the from to be something else like &#8217;support&#8217;.  A minor little issue, but we wanted some emails to be from the application and others be from individuals which just wasn&#8217;t possible.  Lastly, Gmail limits the number of emails that you can send through it in a given period.  This was the big killer for us, we hit limit and didn&#8217;t know it right away.  That cause us some headaches because there really wasn&#8217;t a good way to know if we where close to the limit or what.</p>
<p>After the few Gmail incidents I finally broke down and decided to set up an SMTP server to send out our application emails.  I really didn&#8217;t want to because, well email is kind of scary to me.  The server configurations are very complex and they have warnings everywhere that if you make a mistake every spammer in the world will use your server.  Then if that happens every ISP will hate you and little kids will point and laugh when you go to the mall.  Despite all that I did manage to get a configuration going and we didn&#8217;t let any evil spammers or anything bad like that.  Everything appeared to be going well, or so I thought.</p>
<p>You see, I setup our SMTP to only send out emails, the receiving part was still handled by Gmail.  I hadn&#8217;t keeping tabs on the Gmail inbox of the address we where sending from in some time.  When I did happen to remember to look in that account, several months later, I discovered that EarthLink and AT&amp;T had decided that we where spammers and blocked all emails from our server.  What a drag that was, I had no clue how long that had been going on, and it wasn&#8217;t a very good feeling to be labeled a spammer, even if it was from an automated system.  They have methods to get you removed from list and I filled out their forms, but it happened at least one other time.  That&#8217;s when I decided that there had to be something better out there and that&#8217;s when I found <a href="http://postmarkapp.com" target="_blank">Postmark</a>.</p>
<h3>Why Postmark is good.</h3>
<p>Right when I read the description of the Postmark service I signed up, it was an easy decision for me.  Here&#8217;s how it works, you post your email to Postmark through their HTTP API and then they send your email to it&#8217;s destination.  See super simple, but wait there&#8217;s more, they have a nice overview with a graph of how many emails you&#8217;ve sent, how many bounced, and how many where marked as spam.  You can then dig deeper and see which emails bounced, why they bounce, and there a way to reactivate those emails if you think there was a mistake.</p>
<p>The integration was also pretty easy, they have published a Ruby gem that hooks right in to ActionMailer.  After a couple lines of configuration you can just use ActionMailer just like before and instead of sending emails via SMTP, they are sent to Postmark for delivery.  I also really liked the way the set it up by changing ActionMailer&#8217;s delivery method.  Why is that good?  Well, different ActionMailer subclasses can have different delivery methods, which is completely different from the SMTP settings which are shared across all instances.  That makes it possible to have emails sent to users got through Postmark, while system emails, like error reports can go through SMTP.  Which is exactly how we have it set up today.</p>
<p>I&#8217;m really happy with the service, now I know exactly how many emails to users we are sending out each day and how many are bouncing back.  Even though Postmark just launched a few weeks ago I know that the folks at Wildbit are going to continue to make it better because <a href="http://wildbit.com/blog/2010/02/03/using-postmark-to-reveal-trends/" target="_blank">they use it</a> with their other web applications. Personally I&#8217;m really looking forward to when they get some sort of webhooks going for bounced emails.  I would like to inform our users that we couldn&#8217;t send them an email at the address they have given us.</p>
<p>All of that totally justifies the cost of the Postmark service, which is currently $1.50 for 1,000 emails.  If you are building a web application and are sending out transactional emails to your users, I would really encourage you to investigate a service like <a href="http://postmarkapp.com" target="_blank">Postmark</a>.  Why worry about emails when you should be worrying about your application&#8217;s main features.</p>
]]></content:encoded>
			<wfw:commentRss>http://badpopcorn.com/blog/2010/04/12/send-your-emails-using-postmark/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How an AD can ruin design</title>
		<link>http://badpopcorn.com/blog/2010/01/27/how-an-ad-can-ruin-design/</link>
		<comments>http://badpopcorn.com/blog/2010/01/27/how-an-ad-can-ruin-design/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 03:47:31 +0000</pubDate>
		<dc:creator>Moe</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://badpopcorn.com/blog/?p=1002</guid>
		<description><![CDATA[Should design and good user interface be sacrificed for optimal ad viewing? Not in my opinion. CNN, compared to other big news media, has a pretty good site design. However, their new pulse page is ruined by horrible ad placement. In a way the design was successful, because I noticed the ad. However, I noticed [...]]]></description>
			<content:encoded><![CDATA[<p>Should design and good user interface be sacrificed for optimal ad viewing? Not in my opinion. CNN, compared to other big news media, has a pretty good site design. However, their new <a href="http://newspulse.cnn.com/">pulse page</a> is ruined by horrible ad placement. In a way the design was successful, because I noticed the ad. However, I noticed it like a teenager notices a pimple on their face. I was disgusted, confused and worried it may leave a scar. I&#8217;m seeing this more and more online, and it&#8217;s getting more and more annoying.  Insert <a href="http://sethgodin.typepad.com/">Seth Godin</a> quotes here.</p>

<a href='http://badpopcorn.com/blog/2010/01/27/how-an-ad-can-ruin-design/cnn_ad_ruined_design/' title='cnn_ad_ruined_design'><img width="150" height="125" src="http://badpopcorn.com/blog/wp-content/uploads/2010/01/cnn_ad_ruined_design.png" class="attachment-thumbnail" alt="" title="cnn_ad_ruined_design" /></a>
<a href='http://badpopcorn.com/blog/2010/01/27/how-an-ad-can-ruin-design/cnn_ad_ruined_design_2/' title='cnn_ad_ruined_design_2'><img width="150" height="126" src="http://badpopcorn.com/blog/wp-content/uploads/2010/01/cnn_ad_ruined_design_2.png" class="attachment-thumbnail" alt="" title="cnn_ad_ruined_design_2" /></a>

]]></content:encoded>
			<wfw:commentRss>http://badpopcorn.com/blog/2010/01/27/how-an-ad-can-ruin-design/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Adding Memory To Your Mac</title>
		<link>http://badpopcorn.com/blog/2009/11/08/adding-memory-to-your-mac/</link>
		<comments>http://badpopcorn.com/blog/2009/11/08/adding-memory-to-your-mac/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 03:58:27 +0000</pubDate>
		<dc:creator>Moe</dc:creator>
				<category><![CDATA[Apple]]></category>

		<guid isPermaLink="false">http://badpopcorn.com/blog/?p=992</guid>
		<description><![CDATA[
You need a small screwdriver.
]]></description>
			<content:encoded><![CDATA[<p><img src="http://badpopcorn.com/blog/wp-content/uploads/2009/11/mac_memory.jpg" alt="mac_memory" title="mac_memory" width="448" height="336" class="aligncenter size-full wp-image-993" /></p>
<p>You need a small screwdriver.</p>
]]></content:encoded>
			<wfw:commentRss>http://badpopcorn.com/blog/2009/11/08/adding-memory-to-your-mac/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Affion Crockett, Great Example of Online Branding/Marketing</title>
		<link>http://badpopcorn.com/blog/2009/10/17/affion-crockett-onlie-branding-marketing/</link>
		<comments>http://badpopcorn.com/blog/2009/10/17/affion-crockett-onlie-branding-marketing/#comments</comments>
		<pubDate>Sun, 18 Oct 2009 01:42:37 +0000</pubDate>
		<dc:creator>Moe</dc:creator>
				<category><![CDATA[Entertainment]]></category>
		<category><![CDATA[Television]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://badpopcorn.com/blog/?p=961</guid>
		<description><![CDATA[


Who is Affion Crockett?
 Before this video I thought he was just a funny guy on Wild&#8217;n Out. I now know he is also a musician, oskamill, and was recently in the movie Dance Flick. Affion is using the internet to show off his versatility as an entertainer. Best of all, he is slowly eliminating [...]]]></description>
			<content:encoded><![CDATA[<div style="margin-bottom:10px;">
<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/MTw5wZXunyU&#038;hl=en&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/MTw5wZXunyU&#038;hl=en&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>
</div>
<h1>Who is Affion Crockett?</h1>
<p> Before this video I thought he was just a funny guy on Wild&#8217;n Out. I now know he is also a musician, oskamill, and was recently in the movie Dance Flick. Affion is using the internet to show off his versatility as an entertainer. Best of all, he is slowly eliminating the middle man (studios).  He has over 47,000 subscribers on youtube (his latest video has about a million views as of now), over 7000 friends on myspace, about 7000 followers on twitter, and has 5000 friends on facebook ( i couldn&#8217;t find a real fan page).  He has a, almost instant, connection to about 70,000 fans. Fans who will most likely share, retweet and email anything he puts in his feeds.  </p>
]]></content:encoded>
			<wfw:commentRss>http://badpopcorn.com/blog/2009/10/17/affion-crockett-onlie-branding-marketing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Yaml to stream lots of data</title>
		<link>http://badpopcorn.com/blog/2009/09/24/using-yaml-to-stream-lots-of-data/</link>
		<comments>http://badpopcorn.com/blog/2009/09/24/using-yaml-to-stream-lots-of-data/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 01:38:07 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://badpopcorn.com/blog/?p=965</guid>
		<description><![CDATA[Most people just use Yaml for configuration. The prime example is the database.yml file found in almost every single Rails app. But here&#8217;s a way to take advantage of Yaml in Ruby as a way to serialize and transfer lots of  separate records from one place to another.
The standard yaml library found in Ruby [...]]]></description>
			<content:encoded><![CDATA[<p>Most people just use <a href="http://www.yaml.org/">Yaml</a> for configuration. The prime example is the database.yml file found in almost every single <a href="http://www.rubyonrails.org/">Rails</a> app. But here&#8217;s a way to take advantage of Yaml in Ruby as a way to serialize and transfer lots of  separate records from one place to another.</p>
<p>The standard yaml library found in <a href="http://www.ruby-lang.org/">Ruby</a> is all you need. First, to serialize the target set of records. For our example, we&#8217;re just going to stream to a file, but one can use any IO stream. And instead of streaming an indeterminate number of records, we&#8217;re going to bound this process with a fixed number of records.</p>
<blockquote><p>
require &#8216;yaml&#8217;<br />
file = File.open(&#8216;test.yml&#8217;, &#8216;w&#8217;)<br />
for x in 0..100<br />
  hash = {<br />
    &#8216;id&#8217; => x,<br />
    &#8216;field1&#8242; => &#8220;somedata&#8221;,<br />
    &#8216;field2&#8242; => &#8220;someother data&#8221;,<br />
    &#8216;option&#8217; => true<br />
  }<br />
  file.write hash.to_yaml<br />
end<br />
file.close
</p></blockquote>
<p>Notice that all we needed to do is call the #to_yaml method to do the serialization. It also handles adds the Yaml document start character sequence automatically (i.e. &#8220;&#8212;&#8221;, three dashes alone on a new line).</p>
<p>To read that stream of data:</p>
<blockquote><p>
File.open(&#8216;test.yml&#8217;, &#8216;r&#8217;) do |io|<br />
  YAML.each_document(io) do |record|<br />
    # Do something here<br />
    puts record.inspect<br />
  end<br />
end
</p></blockquote>
<p>Question: What&#8217;s the advantage here?</p>
<p>Answer: Constant memory footprint. We only read in as much as we need to handle the next Yaml document (object, hash). So if you&#8217;re processing a 2 Gig file, the process won&#8217;t try to load the entire file into memory at once. Or you may not actually know how many records will come over the wire.</p>
<p>Some Uses:</p>
<ul>
<li>Mass dumping data from one system to another where the source system lacks write permissions, or even network access, to the target system. </li>
<li>Data Archival in a common format. Yaml is accessible by tools without the need for a SQL database&#8230; if one archived everything in SQL dumps. And this lets you keep rows clustered in manageable sized files.</li>
<li>Stream real-time updates from one system to another. Either HTTP post a batch document stream to a Webhook, or just read and write on raw TCP sockets.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://badpopcorn.com/blog/2009/09/24/using-yaml-to-stream-lots-of-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Quickstart Merb Slice Development</title>
		<link>http://badpopcorn.com/blog/2008/11/02/how-to-quickstart-merb-slice-development/</link>
		<comments>http://badpopcorn.com/blog/2008/11/02/how-to-quickstart-merb-slice-development/#comments</comments>
		<pubDate>Sun, 02 Nov 2008 23:17:50 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://badpopcorn.com/blog/?p=950</guid>
		<description><![CDATA[What are Merb Slices?
Merb Slices are a kind of mini Merb Application that can be packaged up as gems and used as is (or with customizations) within actual Merb Applications. They are full Model-View-Controller stacks to support a large feature within a larger application. Examples could be a full blogging system, user management system or [...]]]></description>
			<content:encoded><![CDATA[<p>What are Merb Slices?</p>
<p>Merb Slices are a kind of mini Merb Application that can be packaged up as gems and used as is (or with customizations) within actual Merb Applications. They are full Model-View-Controller stacks to support a large feature within a larger application. Examples could be a full blogging system, user management system or a file upload system.</p>
<p>Where else can I find good overview information about Merb Slices?</p>
<p>There are a few places, but you should start with the <a href="http://merbcamp.com/video/neighman1.mp4">MerbCamp 2008 MerbSlices talk</a> by Daniel Neighman aka <a href="http://github.com/hassox">hassox</a>. His slides are found <a href="http://github.com/hassox/merb-camp-talks/tree/master">here</a> and <a href="http://www.slideshare.net/hassox/merb-slices-presentation">here</a>. Watch the other <a href="http://merbcamp.com/video">MerbCamp videos</a> for more Merb info.</p>
<p>So what does this article cover?</p>
<p>This just provides some missing details for a developer to immediately get a slice working. The above material gives a great overview of slices in general and the why/how to use/install them in main Merb applications.</p>
<p>Let&#8217;s start by creating our slice.</p>
<div class="CodeRay">
<pre><code>$ merb-gen slice myslice
Generating with slice generator:
     [ADDED]  app/controllers/application.rb
     [ADDED]  app/controllers/main.rb
     [ADDED]  app/helpers/application_helper.rb
     [ADDED]  app/views/layout/myslice.html.erb
     [ADDED]  app/views/main/index.html.erb
     [ADDED]  config/init.rb
     [ADDED]  config/router.rb
     [ADDED]  lib/myslice.rb
     [ADDED]  Rakefile
     [ADDED]  README
     [ADDED]  spec/myslice_spec.rb
     [ADDED]  spec/controllers/main_spec.rb
     [ADDED]  spec/spec_helper.rb
     [ADDED]  stubs/app/controllers/application.rb
     [ADDED]  stubs/app/controllers/main.rb
     [ADDED]  TODO
     [ADDED]  public/javascripts/master.js
     [ADDED]  public/stylesheets/master.css
     [ADDED]  LICENSE
     [ADDED]  lib/myslice/merbtasks.rb
     [ADDED]  lib/myslice/slicetasks.rb
     [ADDED]  lib/myslice/spectasks.rb
$ cd myslice</code></pre>
</div>
<p>Our goal here is to set up our slice so we can actually do development without needing to install it within a Merb application, and to give an example run through of creating a resource.</p>
<p>Let&#8217;s start by editing the slice&#8217;s init.rb file. This file is solely used in your slice development cycle; it is omitted from the final packaged gem; it is NOT used in production. If you look at the slice&#8217;s Rakefile, you will see that NO file in the config/ directory is included in the gem.</p>
<div class="CodeRay">
<pre><code>$ vi config/init.rb</code></pre>
</div>
<div class="CodeRay">
<pre>
<code># Add the following to the top of the slice's config/init.rb file.
# USE THE CORRECT GEM VERSIONS.
merb_gems_version = "0.9.12"
dm_gems_version   = "0.9.6"

# Uncomment the following two lines to develop with haml instead of erb.
# dependency "merb-haml", merb_gems_version
# use_template_engine :haml

dependency "dm-core", dm_gems_version
dependency "dm-aggregates", dm_gems_version
dependency "dm-migrations", dm_gems_version
dependency "dm-timestamps", dm_gems_version
dependency "dm-types", dm_gems_version
dependency "dm-validations", dm_gems_version

use_orm :datamapper</code>
</pre>
</div>
<p>What we did above is to declare a dependency on the DataMapper ORM. You can use whatever ORM you wish, but I&#8217;ll be using DataMapper as the example in this article.</p>
<p>Also, I have included commented lines to show how one would use Haml instead of Erb as the templating engine. I highly suggest that developers write views for BOTH Erb and Haml when developing slices. This gives the users of such slices a choice.</p>
<p>Since we&#8217;re editing files in the config/ directory, we&#8217;ll go ahead and create the database.yml file we&#8217;ll need.</p>
<div class="CodeRay">
<pre><code>$ vi config/database.yml</code></pre>
</div>
<div class="CodeRay">
<pre>
<code># This is a sample database file for the DataMapper ORM
development: &amp;defaults
  # These are the settings for repository :default
  adapter:  sqlite3
  database: sample_development.db</code>
</pre>
</div>
<p>Next, we go ahead and try creating a resource as most developers will be doing. It&#8217;s just the same merb-gen command as one would do for any regular Merb application.</p>
<div class="CodeRay">
<pre><code>$ merb-gen resource article
     [ADDED]  spec/models/article_spec.rb
     [ADDED]  app/models/article.rb
     [ADDED]  spec/requests/articles_spec.rb
     [ADDED]  app/controllers/articles.rb
     [ADDED]  app/views/articles/index.html.erb
     [ADDED]  app/views/articles/show.html.erb
     [ADDED]  app/views/articles/edit.html.erb
     [ADDED]  app/views/articles/new.html.erb
     [ADDED]  app/helpers/articles_helper.rb</code>
</pre>
</div>
<p>When we edit the articles controller, we&#8217;ll see that it looks exactly like a generated resource that one would get in a Merb application. In fact that&#8217;s probably the whole point since we want our slices to be full MVC stacks to implement our subsystem features. But if we tried to use this resource right now, we&#8217;ll find that our slice just won&#8217;t work. The main reason is how we define a slice&#8217;s controller versus a controller in a full Merb application. The whole issue is about namespacing.</p>
<div class="CodeRay">
<pre><code>$ vi app/controllers/articles.rb</code></pre>
</div>
<p>We need to change the following class declaration:</p>
<div class="CodeRay">
<pre>
<code>class Articles &lt; Application</code>
</pre>
</div>
<p>to</p>
<div class="CodeRay">
<pre>
<code>class Myslice::Articles &lt; Myslice::Application</code>
</pre>
</div>
<p>This article class needs to inherit from the slice&#8217;s application class instead of whatever Merb app it is installed in. And the Articles class needs to be in the Myslice namespace so the slice router rules will actually be able to find the class.</p>
<p>The rest of the controller looks good. It&#8217;s got all the default DataMapper access code for its methods. <b>NOTE</b>: If one did not call &#8220;use_orm :datamapper&#8221; in the slice&#8217;s init.rb file, then all this ORM access code will be omitted; one would have a plain class whose methods just called `render`.</p>
<p>As a next step, one would generally verify that merb-gen would have updated the config/router.rb file with this new resource. <b>WARNING!</b> The config/router.rb file is NOT where routes are configured for slices. Everything is done in lib/myslice.rb, or whatever it is named in your real slice. In fact, this is also where we&#8217;ll find other configuration options.</p>
<div class="CodeRay">
<pre><code>$ vi lib/myslice.rb</code></pre>
</div>
<p>Let&#8217;s go ahead and update our slice meta data. Replace the following code with your own stuff.</p>
<div class="CodeRay">
<pre>
<code># All Slice code is expected to be namespaced inside a module
  module Myslice

    # Slice metadata
    self.description = "Myslice is a chunky Merb slice!"
    self.version = "0.0.1"
    self.author = "Engine Yard"</code>
</pre>
</div>
<p>I won&#8217;t cover the other slice hooks in this file except for the &#8220;def self.setup_router(scope)&#8221; method. This is where you SHOULD to add your resource. Although the scope.default_routes line will correctly route to your resource, I find it cleaner to explicitly declare the slice&#8217;s routes. Watch the video mentioned above to understand why we setup routes in this hook instead of a slice&#8217;s router.rb file.</p>
<div class="CodeRay">
<pre>
<code>def self.setup_router(scope)
      # Add the following resource line
      scope.resources :articles
      # The lines that follow are the pre-generated ones.

      scope.match('/index(.:format)').to(:controller =&gt; 'main', :action =&gt; 'index').name(:index)
      # the slice is mounted at /myslice - note that it comes before default_routes
      scope.match('/').to(:controller =&gt; 'main', :action =&gt; 'index').name(:home)
      # enable slice-level default routes by default
      scope.default_routes
    end</code>
</pre>
</div>
<p>I personally would delete the &#8220;scope.default_routes&#8221; line because I&#8217;m all about explicitly specifying routes.</p>
<p>At this point, we&#8217;ve got routes and a fixed up controller.</p>
<p>Now we look at the Article model. This model contains the code required to define it as a DataMapper resource.</p>
<div class="CodeRay">
<pre><code>$ cat app/models/article.rb
class Article
  include DataMapper::Resource

  property :id, Serial
end</code>
</pre>
</div>
<p>Again, as with controllers, the DataMapper code would have been omitted without the &#8220;use_orm :datamapper&#8221; in the slice&#8217;s config/init.rb file. We would have had an empty class.</p>
<p>I will skip over how we develop Models for Datamapper in Merb. One should watch the other <a href="http://merbcamp.com/video">MerbCamp videos</a> for that. Let&#8217;s just assume that you&#8217;ve added a few other properties to the Article model class.</p>
<p>Let&#8217;s create the model&#8217;s sqlite3 tables.</p>
<div class="CodeRay">
<pre><code>$ rake db:automigrate
Don't know how to build task 'db:automigrate'</code>
</pre>
</div>
<p>Oops. We don&#8217;t have that kind of default support in our slice&#8217;s rake tasks. But no fear, we&#8217;ll just invoke the auto_migrate! directly:</p>
<div class="CodeRay">
<pre><code>$ echo 'DataMapper.auto_migrate!' | slice -i
Loading init file from /Users/notroot/projects/myslice/config/init.rb
 ~ Connecting to database...
 ~ Loaded slice 'Myslice' ...
 ~ Parent pid: 9145
 ~ Activating slice 'Myslice' ...
DataMapper.auto_migrate!
[Merb::DataMapperSessionStore, Article]</code>
</pre>
</div>
<p>Be sure to use single quotes since the `!&#8217; character is special in bash. But if you want to do it interactively, just start up the slice irb console.</p>
<div class="CodeRay">
<pre><code>$ slice -i</code></pre>
</div>
<p>The only other thing to note is how slice sql tables are named. Our &#8220;articles&#8221; table in the slice&#8217;s development database becomes &#8220;myslice_articles&#8221; in a Merb application&#8217;s database.</p>
<p>And to finally get it all together, we need to start mogrel to serve up the slice&#8230; but NOT using the `merb` command. We use the `slice` binary.</p>
<div class="CodeRay">
<pre><code>$ slice
Loading init file from /Users/notroot/projects/myslice/config/init.rb
 ~ Connecting to database...
 ~ Loaded slice 'Myslice' ...
 ~ Parent pid: 9147
 ~ Activating slice 'Myslice' ...
merb : worker (port 4000) ~ Starting Mongrel at port 4000
merb : worker (port 4000) ~ Successfully bound to port 4000</code>
</pre>
</div>
<p>And off to the browser you go; and off to developing your slice.<br />
Example) http://localhost:4000/articles</p>
<p>So what&#8217;s next once I finish developing my slice?</p>
<p>Install your slice directly into your gem repository.</p>
<div class="CodeRay">
<pre><code>$ sudo rake install</code></pre>
</div>
<p>Add your slice to the application&#8217;s list of dependencies.</p>
<div class="CodeRay">
<pre><code>$ cd ~/projects/myapp
$ vi config/dependencies.rb</code></pre>
</div>
<div class="CodeRay">
<pre>
<code># Add your slice dependency to the bottom of the file.
dependency "myslice", "0.0.1"</code>
</pre>
</div>
<p>Install the Slice into your Merb Application.</p>
<div class="CodeRay">
<pre><code>$ rake -T slices
$ rake slices:myslice:install</code></pre>
</div>
<p>And go ahead and add your slice to your application&#8217;s router.rb file.</p>
<div class="CodeRay">
<pre><code>$ vi config/router.rb</code></pre>
</div>
<div class="CodeRay">
<pre>
<code># Find the following method call and add your slice.
Merb::Router.prepare do
  # This mounts your slice to the default http://example.com/myslice/
  # "namespace". See Merb's rubydocs for more info about options.
  slice(:myslice)

  # other stuff omitted.
end</code>
</pre>
</div>
<p>Now, you can run `merb` to start up mongrel for your application and<br />
hit away under the /myslice url path namespace.</p>
<p>Example) http://localhost:4000/myslice/articles</p>
<p>Go forth and slice!</p>
]]></content:encoded>
			<wfw:commentRss>http://badpopcorn.com/blog/2008/11/02/how-to-quickstart-merb-slice-development/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
<enclosure url="http://merbcamp.com/video/neighman1.mp4" length="288077128" type="video/quicktime" />
		</item>
		<item>
		<title>Installing Mysql with MacPorts for Rails on Leopard</title>
		<link>http://badpopcorn.com/blog/2008/09/19/installing-mysql-with-macports-for-rails-on-leopard/</link>
		<comments>http://badpopcorn.com/blog/2008/09/19/installing-mysql-with-macports-for-rails-on-leopard/#comments</comments>
		<pubDate>Sat, 20 Sep 2008 04:09:04 +0000</pubDate>
		<dc:creator>Moe</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://badpopcorn.com/blog/?p=948</guid>
		<description><![CDATA[I just spent a hour going through this so you won&#8217;t have to. I thought I installed mysql using mac ports but I kept getting this error.


Errno::ENOENT (No such file or directory - /tmp/mysql.sock):


 Below are the three steps you need to get Mysql running on Leopard for MacPorts.


sudo port install mysql5 +server
sudo launchctl load [...]]]></description>
			<content:encoded><![CDATA[<p>I just spent a hour going through this so you won&#8217;t have to. I thought I installed mysql using mac ports but I kept getting this error.</p>
<div class="CodeRay">
<pre>
<code>Errno::ENOENT (No such file or directory - /tmp/mysql.sock):</code>
</pre>
</div>
<p> Below are the three steps you need to get Mysql running on Leopard for MacPorts.</p>
<div class="CodeRay">
<pre>
<code>sudo port install mysql5 +server
sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
sudo ln -s /opt/local/var/run/mysql5/mysqld.sock /tmp/mysql.sock</code>
</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://badpopcorn.com/blog/2008/09/19/installing-mysql-with-macports-for-rails-on-leopard/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Authentication and Authorization in Rails</title>
		<link>http://badpopcorn.com/blog/2008/09/16/authentication-and-authorization-in-rails/</link>
		<comments>http://badpopcorn.com/blog/2008/09/16/authentication-and-authorization-in-rails/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 23:46:58 +0000</pubDate>
		<dc:creator>bill</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://badpopcorn.com/blog/?p=947</guid>
		<description><![CDATA[Some say Rails is &#8220;missing&#8221; a lot of things you might expect to find in full-featured web development framework, but it doesn&#8217;t matter &#8211; what&#8217;s it&#8217;s NOT missing is a plugin system which allows you to add any functionality you need by pulling a few bits of code from other authors into your site. What [...]]]></description>
			<content:encoded><![CDATA[<p>Some say <a href="http://www.rubyonrails.com">Rails</a> is &#8220;missing&#8221; a lot of things you might expect to find in full-featured web development framework, but it doesn&#8217;t matter &#8211; what&#8217;s it&#8217;s NOT missing is a plugin system which allows you to add any functionality you need by pulling a few bits of code from other authors into your site. What I&#8217;ll be using in this example are the <a href="http://github.com/technoweenie/restful-authentication/tree/master">restful-authentication</a> plugin for authentication and the <a href="http://github.com/timcharper/role_requirement/tree/master">role_requirement</a> plugin for authorization. Both of these are hosted on <a href="http://github.com/">github</a>, which hosts loads of Rails plugins along with other open projects. As the name implies, they use git for their repositories, so you should install git to grab these plugins.</p>
<p>
<h1>Setting up authentication</h1>
</p>
<p>First, you&#8217;ll need to set up authentication. In the vendor/plugins folder of your project, run:</p>
<div class="CodeRay">
<pre><code>git clone git://github.com/technoweenie/restful-authentication.git restful_authentication</code></pre>
</div>
<p>This will grab a copy of the restful_authentication plugin; you don&#8217;t need to mess with any of the code in the plugin itself. go back to your project&#8217;s root and run:</p>
<div class="CodeRay">
<pre><code>script/generate authenticated user sessions
rake db:migrate</code></pre>
</div>
<p>This will set up the user model for you and insert the users table in your database. You can add arguments to the generate script such as &#8211;include-activation &#8211;aasm to enable activation emails but we&#8217;re not going to cover all of that right now.</p>
<p>Now, you&#8217;ll have two new controllers in your application, sessions_controller.rb and users_controller.rb. Go to each of these files and remove or comment out the line that says &#8216;include AuthenticatedSystem&#8217;, and copy this line to the top of the application controller instead, right at the beginning of the class definition:</p>
<div class="CodeRay">
<pre><code>class ApplicationController &lt; ActionController::Base
  include AuthenticatedSystem</code></pre>
</div>
<p>and so on. The generate script should have also added these lines to routes.rb:</p>
<div class="CodeRay">
<pre><code>map.logout '/logout', :controller =&gt; 'sessions', :action =&gt; 'destroy'
  map.login '/login', :controller =&gt; 'sessions', :action =&gt; 'new'
  map.register '/register', :controller =&gt; 'users', :action =&gt; 'create'
  map.signup '/signup', :controller =&gt; 'users', :action =&gt; 'new'
  map.resources :users

  map.resource :session</code></pre>
</div>
<p>These give you some prettier URLs rather than, for example, /users/new to sign up and /sessions/new to login. Generally you want the first thing a user sees to be the login page, so if you want to you can make that the default by adding</p>
<div class="CodeRay">
<pre><code>map.root :controller =&gt; "sessions", :action =&gt; "new"</code></pre>
</div>
<p>to your routes. You&#8217;ll also need to remove or rename index.html in the public folder. </p>
<p>At this point you have a basic authentication system, which is great considering how easy it is to set up, but alone it&#8217;s pretty useless. You&#8217;ll notice, no matter if you&#8217;re logged in or not, you still have full access to your app. So why did we even bother? Because now, you can add authorization to lock down actions based on roles you set up.</p>
<p>
<h1>Setting up authorization</h1>
</p>
<p>There are a few different ways to do this; if you want a very, <b>very</b> granular authorization system you can install <a href="http://github.com/bilson/padlock_authorization/tree/master">padlock authorization</a> which allows you to set roles per each object in your application. We decided this was probably overkill for our latest project, but I may touch on it in a later blog if we decide to use it after all. We&#8217;ll be using the aforementioned role_requirement plugin.</p>
<p>Back to github with us! Head back to your /vendor/plugins folder and run:</p>
<div class="CodeRay">
<pre><code>git clone git://github.com/timcharper/role_requirement.git role_requirement</code></pre>
</div>
<p>Go back up to your application&#8217;s root, and run:</p>
<div class="CodeRay">
<pre><code>script/generate roles Role User
rake db:migrate</code></pre>
</div>
<p>Now, you&#8217;ll have to make some manual database changes. You need to add one or more roles to the roles table, and if you have any users, assign them initial roles, if you want them to have roles, in the roles_users table. You can, of course, just add a new controller and view to make all this changeable from your application, but you&#8217;ll probably still be setting up one admin user by hand to start things off when you go live.</p>
<p>Now you can go about editing your controllers to make each one accept and reject your roles. For example, say I have a simple model with with a TV show, which can have one starting date. To allow only administrators to make changes, you can set up your Shows controllers like so:</p>
<div class="CodeRay">
<pre><code>class ShowsController &lt; ApplicationController
  require_role "ADMIN", :for_all_except =&gt; [:index, :show]
  def index
    @shows = Show.find(:all)</code></pre>
</div>
<p>(The rest is standard Rails boilerplate)</p>
<p>A later post will probably deal with setting/changing roles within your application.</p>
]]></content:encoded>
			<wfw:commentRss>http://badpopcorn.com/blog/2008/09/16/authentication-and-authorization-in-rails/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Rails Observer Field</title>
		<link>http://badpopcorn.com/blog/2008/09/11/rails-observer-field/</link>
		<comments>http://badpopcorn.com/blog/2008/09/11/rails-observer-field/#comments</comments>
		<pubDate>Thu, 11 Sep 2008 20:16:48 +0000</pubDate>
		<dc:creator>Moe</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://badpopcorn.com/blog/?p=946</guid>
		<description><![CDATA[The Rails observer field is an easy way to add an ajax select menu to your site. Below is an example to get you going.
Problem
Let&#8217;s say you have a list of users on your site and want to filter them by a group. In the example we will be using gym members.
View


&#60;%= select "gym", :gym_id, [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href ="http://www.rubyonrails.com">Rails</a> observer field is an easy way to add an ajax select menu to your site. Below is an example to get you going.</p>
<p><strong>Problem</strong></p>
<p>Let&#8217;s say you have a list of users on your site and want to filter them by a group. In the example we will be using gym members.</p>
<p><strong>View</strong></p>
<div class="CodeRay">
<pre>
<code>&lt;%= select "gym", :gym_id, Gym.find(:all).collect { |p| [p.name, p.id]}, {:selected =&gt; params[:gym]}  %&gt;
&lt;%= observe_field "gym_gym_id", :url =&gt; { :action =&gt; :members_by_gym , 
                                          :controller =&gt; :members},
                                          :update =&gt; "member_list", 
                                          :with =&gt; "'gym='+ escape($('gym_gym_id').value)" %&gt;
&lt;div id="member_list"&gt;
   &lt;% for member in @members %&gt;
      &lt;%= link_to member.name, member %&gt;&lt;br/&gt;
    &lt;% end %&gt;
&lt;/div&gt;</code>
</pre>
</div>
<p>The first thing we do is build the select form by using the <a href="http://badpopcorn.com/blog/2008/09/02/rails-select-helper/">Rails Select Helper.</a> The id of the select tag is generated by rails, &#8220;gym_gym_id&#8221;. We need to pass the id name as the first parameter to the observe field. The url parameter is pretty straightforward. The update parameter of the observer field designates which element will be updated. The &#8220;:with&#8221; parameter adds a parameter to the form named &#8220;gym&#8221;. </p>
<p><strong>Post</strong></p>
<div class="CodeRay">
<pre>
<code>Parameters: {"contoller"=&gt;"members", "action"=&gt;"members_by_gym", "gym"=&gt;"1", "controller"=&gt;"members"}</code>
</pre>
</div>
<p>The &#8220;gym&#8221; parameter posted here is from the observer_field tag.</p>
<p><strong>Controller</strong></p>
<div class="CodeRay">
<pre>
<code>def members_by_gym
     @members = Member.find_all_by_gym_id(params[:gym])
     render :layout =&gt; false
  end</code>
</pre>
</div>
<p>It&#8217;s important that you add the render <code>:layout =&gt; false</code> or bad things will happen. We still have one more thing to do. We have our controller being hit via ajax when the select menu changes but we need to make the html that will be updated on the page. In the view/members folder we need to make a new page called members_by_gym.html.erb</p>
<p><strong>members_by_gym.html.erb</strong></p>
<div class="CodeRay">
<pre>
<code>&lt;% for member in @members %&gt;
     &lt;%= link_to member.name, member %&gt;&lt;br/&gt;
&lt;% end %&gt;</code>
</pre>
</div>
<p>This code will be interpreted and update the &#8220;member_list&#8221; element on the page. That&#8217;s it. Rails Ajax magic.</p>
]]></content:encoded>
			<wfw:commentRss>http://badpopcorn.com/blog/2008/09/11/rails-observer-field/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Google Charts in Rails, gchartrb</title>
		<link>http://badpopcorn.com/blog/2008/09/08/rails-google-charts-gchartrb/</link>
		<comments>http://badpopcorn.com/blog/2008/09/08/rails-google-charts-gchartrb/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 17:00:19 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Google]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://badpopcorn.com/blog/?p=943</guid>
		<description><![CDATA[This week I was playing around with Google Charts, a wonderful API for creating charts via a URL. While I developed a helper class to create charts more easily in Rails, I did a little research and realized the RubyGem gchartrb has a great API for creating these charts. Here&#8217;s a quick tutorial on using [...]]]></description>
			<content:encoded><![CDATA[<p>This week I was playing around with <a href="http://code.google.com/apis/chart/">Google Charts</a>, a wonderful API for creating charts via a URL. While I developed a helper class to create charts more easily in Rails, I did a little research and realized the RubyGem <a href="http://code.google.com/p/gchartrb/">gchartrb</a> has a great API for creating these charts. Here&#8217;s a quick tutorial on using gchartrb from the ground up.</p>
<p><strong>Installing gchartrb</strong><br />
To install gchartrb, simply use the command <code>gem install gchartrb</code> from your command prompt. Check out these <a href="http://code.google.com/p/gchartrb/">instructions</a> for additional help and to access the gchartrb packages (Downloadable <a href="http://code.google.com/p/gchartrb/downloads/list">here</a>).</p>
<p><strong>Quick Examples</strong><br />
Now that we have our gem installed, we can create graphs right away. First, remember to require the gem at the top of your code. Notice how the gchartrb gem is actually called &#8220;google_chart&#8221;:</p>
<div class="CodeRay">
<pre><code>1 require 'rubygems'
2 require 'google_chart'</code></pre>
</div>
<p>There are a couple ways that you can create a chart. You can instantiate the graph and then add attributes to it:</p>
<div class="CodeRay">
<pre><code>1 lc = GoogleChart::LineChart.new("400x200", "My Results", false)</code></pre>
</div>
<p>Alternatively, you can create the graph and add the attributes within a block:</p>
<div class="CodeRay">
<pre><code>1 GoogleChart::LineChart.new("400x200", "My Results", false) do |lc|
2 # Put lc data here
3 # ...
4 puts lc.to_url
5 end</code></pre>
</div>
<p>I personally prefer the latter method, as the creation of the graph and its attributes are kept together in a modular fashion.</p>
<p>Here are some examples of line, bar, and pie charts:</p>
<p><strong>Line Chart</strong></p>
<div class="CodeRay">
<pre><code>1 lc = GoogleChart::LineChart.new("400x200", "My Results", false)
2 lc.data "Line green", [3,5,1,9,0,2], '00ff00'
3 lc.data "Line red", [2,4,0,6,9,3], 'ff0000'
4 lc.axis :y, :range =&amp;gt; [0,10], :font_size =&amp;gt; 10, :alignment =&amp;gt; :center
5 lc.show_legend = false
6 lc.shape_marker :circle, :color =&amp;gt; '0000ff', :data_set_index =&amp;gt; 0, :data_point_index =&amp;gt; -1, :pixel_size =&amp;gt; 10
7 puts lc.to_url</code></pre>
</div>
<p><img src="http://chart.apis.google.com/chart?chxt=y&#038;cht=lc&#038;chd=s:UhG9AN,NbAo9U&#038;chs=400x200&#038;chxr=0,0,10&#038;chco=00ff00,ff0000&#038;chtt=My+Results&#038;chxs=0,10,0&#038;chm=o,0000ff,0,-1,10" /></p>
<p>Notice that each lc.data call will create a new data line for you. The second parameter accepts a integer or float array, which can be anything you like. Lets say you have a <code>WeighIn</code> model that has <code>weight</code> as an attribute and belongs to some <code>Person</code>. If a person weighs himself/herself once a month for a year, then we can collect that data and show your trend via a line graph. Collecting that data and using it would look like this:</p>
<div class="CodeRay">
<pre><code># In the controller Person
def show
@person = Person.find(params[:id])
weigh_ins = @person.weigh_ins.collect(&amp;amp;:weight)

lc = GoogleChart::LineChart.new("#{width}x200", "My Results", false)
lc.data "Weigh ins", weigh_ins, green
# etc, etc...
puts lc.to_url # Or @line_graph = lc.to_url
end</code></pre>
</div>
<p>Another useful function, <code>lc.axis</code>, can be used to specify your own data range labels, but it is not required to create a chart (default ranges and labels will be used). The last function, <code>lc.to_url</code>, takes the information specified for the line chart and puts it into the Google charts url.</p>
<p><strong>Bar Chart</strong></p>
<div class="CodeRay">
<pre><code>1 bar_1_data = [350,110,700]
2 bar_2_data = [200,800,50]
3 color_1 = 'c53711'
4 color_2 = '0000ff'
5 names_array = ["Bob","Stella","Foo"]
6 GoogleChart::BarChart.new("600x300", "Horizontal Bar Graph", :horizontal, false) do |bc|
7 bc.data "FirstResultBar", bar_1_data, color_1
8 bc.data "SecondResultBar", bar_2_data, color_2
9 bc.axis :y, :labels =&amp;gt; names_array, :font_size =&amp;gt; 10
10 bc.axis :x, :range =&amp;gt; [0,1000]
11 bc.show_legend = false
12 bc.stacked = true
13 bc.data_encoding = :extended
14 bc.shape_marker :circle, :color =&amp;gt; '00ff00', :data_set_index =&amp;gt; 0, :data_point_index =&amp;gt; -1, :pixel_size =&amp;gt; 10
15 puts bc.to_url
16 end</code></pre>
</div>
<p><img src = "http://chart.apis.google.com/chart?chxt=y,x&#038;chxl=0:|Bob|Stella|Foo&#038;cht=bhg&#038;chd=e:b.Iz3.,P...D.&#038;chs=450x220&#038;chxr=1,0,1000&#038;chco=c53711,0000ff&#038;chtt=Horizontal+Bar+Graph&#038;chxs=0,10&#038;chm=o,00ff00,0,-1,10"/></p>
<p>An interesting attribute to pay attention to is <code>bc.stacked</code>, which gives you the ability to stack bar data, or to create separate bars together. When <code>bc.stacked = false</code>, the chart above is what is shown.</p>
<p>If <code>bc.stacked = true</code>, the same data will look like this:</p>
<p><img src = "http://chart.apis.google.com/chart?chxt=y,x&#038;chxl=0:|Bob|Stella|Foo&#038;cht=bhs&#038;chd=e:YnHvxO,OE4QDh&#038;chs=450x130&#038;chxr=1,0,1000&#038;chco=c53711,0000ff&#038;chtt=Horizontal+Bar+Graph&#038;chxs=0,10&#038;chm=o,00ff00,0,-1,10"/></p>
<p>Also, if you wanted to use this code in one of your controllers, and then display the graph in a template (view) file, you would set the graph&#8217;s url in the controller: <code>@graph = bc.to_url</code> Then within the view, you can simple show the graph as an image like this: </p>
<div class="CodeRay">
<pre><code>&lt;!-- From within the view file --&gt; 
&lt;img src="&lt;%= @graph %&gt;"/&gt;</code>
</pre>
</div>
<p><strong>Pie Chart</strong></p>
<div class="CodeRay">
<pre><code>1 GoogleChart::PieChart.new('320x200', "Pie Chart",false) do |pc| 
2 pc.data "Apples", 40 
3 pc.data "Banana", 20 
4 pc.data "Peach", 30 
5 pc.data "Orange", 60 
6 puts "\nPie Chart" 
7 puts pc.to_url 
8 # Pie Chart with no labels 
9 pc.show_labels = false 
10 puts "\nPie Chart (with no labels)" 
11 puts pc.to_url 
12 end</code></pre>
</div>
<p><img src = "http://chart.apis.google.com/chart?chl=Apples|Banana|Peach|Orange&#038;cht=p&#038;chd=s:oUe9&#038;chs=320x200&#038;chtt=Pie+Chart"/></p>
<p><img src = "http://chart.apis.google.com/chart?cht=p&#038;chd=s:oUe9&#038;chs=320x200&#038;chtt=Pie+Chart"/></p>
<p>This last example was taken from <a href="http://gchartrb.rubyforge.org/files/README_txt.html"> gchartrb&#8217;s readme</a> file, which has an example for each kind of chart you can create (including Venn diagrams, Scatter charts, XY Line charts, etc). Notice that the data does not need to be within 0 to 100 (like percents on the pie chart) &#8211; the data will be added together and scaled appropriately by gchartrb. If you want to make the chart 3d, either set the 3rd parameter on <code>PieChart.new</code> to <code>true</code>, or you can set the attribute manually: <code>pc.is_3d = true</code></p>
<p><strong>Useful parameters</strong></p>
<p>Google charts, by default, works with the data range between 0 and 100. Most of the time, your values will not work with this same scale. Instead of converting your data to the default scale, you can specify the data range with the <code>:range</code> parameter in <a href="http://gchartrb.rubyforge.org/classes/GoogleChart/Base.html#M000017"><code>bc.axis</code></a>. So if you data was between 1 and 12 on the x_axis (like months in a year), then you could specify that range like so:<br />
<code>lc.axis :x, :range =&amp;gt; [1,12], :labels =&amp;gt; month_names_array</code></p>
<p>If you&#8217;re working with large data sets/numbers, then you may have chart data that is slightly off the mark. The default encoding that gchartrb uses is called <a href="http://code.google.com/apis/chart/#simple">Simple Encoding</a>, which only has 62 points of resolution. We can easily expand this resolution (to 4096 points of resolution) by using <a href="http://code.google.com/apis/chart/#extended">Extended encoding</a>, which a chart can select by changing the <code>data_encoding</code> attribute with <code>:extended</code>. For a line graph <code>lc</code>, it would look like this: <code>lc.data_encoding = :extended</code>. See <a href="http://code.google.com/apis/chart/#chart_data">Chart data</a> for an explanation for all the encodings Google charts uses.</p>
<p><strong>gchartrb subtlities</strong></p>
<p>While gchartrb provides a great API for create charts in Rails, there are some subtle details about gcharts that you should be aware of. First of all, all charts derive from the base class <a href="http://gchartrb.rubyforge.org/classes/GoogleChart/Base.html">GoogleChart::Base</a> &#8211; check it out to see all the methods and attributes that apply to all charts. Secondly, gchartrb encodes all of the data values in <code>some_chart.data</code> from numeric form into a string representation of the data. This conversion doesn&#8217;t effect the final output of your chart, and helps make the chart url shorter in length. Again, if your working with large values, make sure the use the extended coding by using <code>lc.data_encoding :extended</code>.</p>
<p><strong>Other Resources</strong><br />
Now that you have a taste of Google Charts with gchartrb, check out the API documentation <a href="http://gchartrb.rubyforge.org/">here</a>. If gchartrb doesn&#8217;t suit your tastes, you can go directly to the <a href="http://code.google.com/apis/chart/">Google Charts API</a> &#8211; it is thorough and self-explanatory. Ajaxian shows a number of quick examples of Google Charts <a href="http://ajaxian.com/archives/use-the-google-chart-api-to-create-charts-for-your-web-applications">here</a>, and Matthew Bass gives a solid introduction to Google Charts and a few examples with gchartrb <a href="http://www.infoq.com/articles/bass-google-charts-gchartrb">here</a>. Lastly, here is a great link for <a href="http://www.collegeathome.com/blog/2008/06/05/50-cool-things-you-can-do-with-google-charts-api/">50 Cool Things You Can Do with Google Charts</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://badpopcorn.com/blog/2008/09/08/rails-google-charts-gchartrb/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
