Doing something different depending on file extension (MIME type) in Sinatra

This looks like a solution: sinatra-respond_to

Posted: March 31st, 2010 | Author: | Filed under: Code | Tags: , , , | No Comments »

Using Rack middleware to parse JSON

In attempting to AJAX-ize the site, I had the desire to handle JSON as if it were form post data. Queue a Rack middleware solution. rack-contrib contains a bunch of common middleware extensions, one being the horribly named PostBodyContentTypeParser. To get this working I added:

1
require 'rack/contrib'

with all of the rest of the required files.

Added:

1
use Rack::PostBodyContentTypeParser

to my application class

And went about over testing it like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def test_json_creates_params_hash
  params_hash={"user"=>{"username"=>"testuser","email"=>"test@test.com","password"=>"pass1","password_confirmation"=>"pass1"}}
  post '/test_json', params_hash
  assert !last_request.params.blank?
  assert_equal params_hash, last_request.params
  assert last_response.ok?
  json_string="{\"user\":{\"password_confirmation\":\"pass1\",\"username\":\"testuser\",\"password\":\"pass1\",\"email\":\"test@test.com\"}}"
  post '/test_json', JSON(json_string)
  assert !last_request.params.blank?
  assert_equal params_hash, last_request.params
  assert last_response.ok?
  post '/test_json', json_string, "CONTENT_TYPE"=>"application/json"
  assert_equal last_request.env["CONTENT_TYPE"], "application/json"
  assert !last_request.params.blank?
  assert_equal params_hash, last_request.params
  assert last_response.ok?
end
Posted: March 30th, 2010 | Author: | Filed under: Code | Tags: , , , , , , , | 1 Comment »

AJAX-izing

Working on making the site more app-like. In doing so, found the Prototype.js-based library, LivePipe. The documentation is poor, but what little I’ve seen, I like.

Now, I’m trying to figure out how much I should “gracefully degrade” or not.

Posted: March 29th, 2010 | Author: | Filed under: Code | Tags: , , , , | No Comments »

Close to perfection

Posted: March 29th, 2010 | Author: | Filed under: Fun | Tags: , , | No Comments »

Blogging system mostly in place

Title pretty much says it all.

Posted: March 28th, 2010 | Author: | Filed under: Code | Tags: , | No Comments »

Put a basic teaser page up

Check it out

Posted: March 28th, 2010 | Author: | Filed under: Fun | Tags: | No Comments »

Battling autotest

Looking for a reason that autotest is skipping some of my test files….

UPDATE: Found the culprit. In my .autotest file I had some exclusions which were matching on the log in blog

1
2
3
4
5
Autotest.add_hook :initialize do |autotest|
  %w{.git .svn .hg .DS_Store db log tmp vendor ._* .sqlite3}.each do |exception| 
    autotest.add_exception(exception) 
  end
end

I think I’ll just comment the exception line out for now as I don’t think it’s necessary.

Posted: March 27th, 2010 | Author: | Filed under: Code | Tags: , , | No Comments »

Rack::Test and request / response objects

Rack::Test uses last_response and last_request objects instead of Rack’s typical request and response objects. This is probably normally fine, but when you are testing functionality that requires accessing the Rack’s normal objects, they aren’t there. I found (in the comments section of this post) that you can fix this by overriding them in your test_helper.rb:

1
2
3
4
5
6
7
8
9
10
11
12
module Test::Unit
  class TestCase
    include Rack::Test::Methods
    ...
 
    def request(*args)
      args.empty? ? last_request : rack_test_session.request(*args)
    end
 
    ...
  end
end
Posted: March 26th, 2010 | Author: | Filed under: Code | Tags: , , , , , , , , , | No Comments »

When tests are acting strange and you can’t figure out what’s wrong…

Check that you didn’t copy / paste / didn’t change the test class definition line.

example:

My BlogPost class was mis-titled…

1
2
class FundastacheSiteTest < Test::Unit::TestCase
...

There went an hour…

Posted: March 25th, 2010 | Author: | Filed under: Code | Tags: , , , | No Comments »

Including common routines in test_helper.rb

I should have done this earlier. To do a number of tests, I have to repeat the same actions like create a user, log a user in, etc. I was putting these actions at the top of each test file, but have wised up and moved them to the test_helper.rb file. Here’s how that looks now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
module Test::Unit
  class TestCase
    ...
 
    def create_user(user_hash={})
      post '/user/signup', {:user=>{:username=>"testuser",:email=>"test@test.com",:password=>"pass1",:password_confirmation=>"pass1"}.merge(user_hash)}
    end
 
    def login(user_hash={})
      post "/user/login", :user=>{:username_or_email=>'testuser',:password=>'pass1'}.merge(user_hash)
    end
 
  end
end
Posted: March 24th, 2010 | Author: | Filed under: Code | Tags: , , , , | 1 Comment »