Doing something different depending on file extension (MIME type) in Sinatra
This looks like a solution: sinatra-respond_to
Posted: March 31st, 2010 | Author: jay | Filed under: Code | Tags: extension, mime type, respond_to, sinatra | No Comments »This looks like a solution: sinatra-respond_to
Posted: March 31st, 2010 | Author: jay | Filed under: Code | Tags: extension, mime type, respond_to, sinatra | No Comments »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 |
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: jay | Filed under: Code | Tags: ajax, javascript, livepipe, prototype, prototype.js | No Comments »Title pretty much says it all.
Posted: March 28th, 2010 | Author: jay | Filed under: Code | Tags: blog, function | No Comments »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: jay | Filed under: Code | Tags: autotest, test, testing | No Comments »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 |
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: jay | Filed under: Code | Tags: bug, class, dumb, test | No Comments »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 |
working on storing files to Amazon’s S3 web service. Here’s what I’ve come up with so far:
1 2 3 4 5 6 7 8 9 10 11 12 | require 'aws/s3' def aws_connect @aws_connect ||= AWS::S3::Base.establish_connection!( :access_key_id=>AWS_KEY, :secret_access_key=>AWS_SECRET ) end def aws_upload(file,bucket=AWS_BUCKET) aws_connect AWS::S3::S3Object.store(file,open(file),bucket) end |
then a:
1 | aws_upload('test.txt') |
Will upload the the file to the Amazon bucket. Pretty simple.
Posted: March 22nd, 2010 | Author: jay | Filed under: Code | Tags: amazon, aws, aws/s3, bucket, gem, ruby, s3, s3object, upload | No Comments »1 2 | # create the consumer... consumer ||= OAuth::Consumer.new(KEY, SECRET, {:site => SITE, :authorize_path => PATH }) |
1 2 3 4 5 | # create the request token... rt=consumer.get_request_token({ :oauth_callback => OAUTH_CALLBACK_URL }) # save the request token and secret in the session... session[:r_token]=rt.token session[:r_secret]=rt.secret |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | # use session values to create the request token... rt=OAuth::RequestToken.new(consumer, session[:r_token], session[:r_secret]) # grab the user data from the OAuth provider... access_token=rt.get_access_token({:oauth_verifier=>params[:oauth_verifier]}) oauth_user_json=access_token.get(VERIFY_PATH).body oauth_user=JSON.parse(oauth_user_json) # create or find the the user (using twitter.com for the email address - could use some work)... u=TwitterUser.first_or_create(:email=>"#{oauth_user['screen_name']}@twitter.com") u.username=oauth_user['screen_name'] u.save! u.oauth_tokens.all.destroy u.oauth_tokens.new(:user_access_token=>access_token.to_yaml) u.save! # set the session user for future use... session[:user]=u.id ... # and when you need access to the OAuth provider again, use the access_token stored in the User model u=User.first(:id=>session[:user]) access_token=YAML::load(u.oauth_tokens.first.user_access_token) verify=access_token.get(OAUTH_PROVIDERS["https://twitter.com"][:verify_path]).body |