<?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>Erik A. Hanson &#187; Ruby</title>
	<atom:link href="http://www.eahanson.com/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.eahanson.com</link>
	<description>My weblog</description>
	<lastBuildDate>Mon, 24 May 2010 04:49:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>SeleniumQuery</title>
		<link>http://www.eahanson.com/2010/05/23/seleniumquery/</link>
		<comments>http://www.eahanson.com/2010/05/23/seleniumquery/#comments</comments>
		<pubDate>Mon, 24 May 2010 04:49:46 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Selenium]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/?p=125</guid>
		<description><![CDATA[I wrote a tiny library to make Selenium testing in Ruby a little easier by helping you easily build a jQuery string that can be executed in the browser during a test. This: # this is ruby jquery = SeleniumQuery.new(selenium) id = jquery.find("td:nth(5).user:visible").closest("tr").attr!("id") executes this in the browser: // this is Javascript selenium .browserbot .getCurrentWindow() [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a tiny library to make Selenium testing in Ruby a little easier by helping you easily build a jQuery string that can be executed in the browser during a test. </p>
<p>This:</p>
<pre>
# this is ruby
jquery = SeleniumQuery.new(selenium)
id = jquery.find("td:nth(5).user:visible").closest("tr").attr!("id")
</pre>
<p>executes this in the browser:</p>
<pre>
// this is Javascript
selenium
  .browserbot
  .getCurrentWindow()
  .jQuery(selenium.browserbot.getCurrentWindow().document)
  .find("td:nth(5).user:visible")
  .closest("tr")
  .attr("id");
</pre>
<p>Check it out here: <a href="http://github.com/eahanson/seleniumquery">http://github.com/eahanson/seleniumquery</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2010/05/23/seleniumquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Ruby Client for Angular</title>
		<link>http://www.eahanson.com/2009/11/19/a-ruby-client-for-angular/</link>
		<comments>http://www.eahanson.com/2009/11/19/a-ruby-client-for-angular/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 04:29:08 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[My Software]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/?p=120</guid>
		<description><![CDATA[Here&#8217;s a little Ruby client for storing data in Angular, a system for creating simple database-backed web apps extremely easily. #!/usr/bin/ruby require 'rubygems' require 'httpclient' require 'json' class Angular def initialize(username, password, library) @http = HTTPClient.new @library = library post("/login", :email => username, :password => password) end def libraries get("/data") end def store(database, document, data) [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a little Ruby client for storing data in <a href="http://www.getangular.com">Angular</a>, a system for creating simple database-backed web apps extremely easily. </p>
<pre>
#!/usr/bin/ruby

require 'rubygems'
require 'httpclient'
require 'json'

class Angular
  def initialize(username, password, library)
    @http = HTTPClient.new
    @library = library
    post("/login", :email => username, :password => password)
  end

  def libraries
    get("/data")
  end

  def store(database, document, data)
    post("/data/#{database}/#{document}", data.to_json)
  end

  private

  def post(path, params)
    request(:post, path, params)
  end

  def get(path, params=nil)
    request(:get, path, params)
  end

  def request(method, path, params)
    uri = (path.index("http") == 0) ? path : ("http://#{@library}.getangular.com" + path)
    response = @http.send(method, uri, params)
    if response.code == 302
      get(response.header["Location"][0])
    elsif response.code == 200
      parsed = parse(response)
      status_code = parsed['$status_code']
      raise "#{uri.to_s} responded with #{status_code}" if status_code &#038;&#038; status_code != 200
      parsed
    else
      raise "#{uri.to_s} responded with #{response.code}"
    end
  end

  def parse(response)
    begin
      JSON.parse(response.body.content)
    rescue
      {}
    end
  end
end
</pre>
<p>Also <a href="http://github.com/eahanson/angular-ruby">available on GitHub</a> for your forking pleasure.</p>
<p>Maybe in the future I&#8217;ll write up a blog post about how I&#8217;m using Angular.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2009/11/19/a-ruby-client-for-angular/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing File Uploads To A Rack Server</title>
		<link>http://www.eahanson.com/2009/03/28/testing-file-uploads-to-a-rack-server/</link>
		<comments>http://www.eahanson.com/2009/03/28/testing-file-uploads-to-a-rack-server/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 19:28:46 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/?p=78</guid>
		<description><![CDATA[I wrote a web app on top of Rack that includes the ability to accept file uploads. It took some time to figure out how to successfully test the file upload using Rack::Test. The trick was to encode the file exactly correctly, and then post it with the correct headers. I wrote a simple post_file [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a web app on top of <a href="http://rack.rubyforge.org/">Rack</a> that includes the ability to accept file uploads. It took some time to figure out how to successfully test the file upload using <a href="http://www.brynary.com/2009/3/5/rack-test-released-a-simple-testing-api-for-rack-based-frameworks-and-apps">Rack::Test</a>. </p>
<p>The trick was to encode the file exactly correctly, and then post it with the correct headers. I wrote a simple <code>post_file</code> method to do this.</p>
<pre>
  def post_file(url, param_name, file_path, content_type)
    file_name = File.basename(file_path)
    boundary = "AaB03x"
    data = "--#{boundary}\r\n" +
           "Content-Disposition: form-data; name=\"#{param_name}\"; filename=\"#{file_name}\"\r\n" +
           "Content-Type: #{content_type}\r\n" +
           "\r\n" +
           "#{File.read(file_path)}\r\n" +
           "--#{boundary}--\r\n" +
           "\r\n"
    post(url, {}, {
      "CONTENT_TYPE" => "multipart/form-data, boundary=\"#{boundary}\"",
      "CONTENT_LENGTH" => data.length, :input => data })
  end
</pre>
<p>I call it from my test like this:</p>
<pre>
...
post_file("/my_app/upload", "uploaded_image", "../test-images/originals/loki.jpg", "image/jpeg")
...
</pre>
<p>and my app reads it like this:</p>
<pre>
...
request = Rack::Request.new(env)
...
uploaded_image = request.params["uploaded_image"][:tempfile]
...
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2009/03/28/testing-file-uploads-to-a-rack-server/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
