How to perform a file upload (multipart post) with Ruby
You have at least three options:
- The curb gem
- The multipart-post Net:HTTP extension
- Calling curl from Ruby with, for example, Open3.
1 Open3.popen3('curl <and your parameters>') do |input, output, error| 2 # do something 3 end
Dynamic RSpec tests using plain old Ruby
Just happened to stumble upon the following article at caboo.se: Handy dynamic rspec tip.
I immediately found a way of simplifying a test case that involves testing that an ever increasing number of videos can be transcoded:
1 require File.dirname(__FILE__) + '/helper' 2 require File.dirname(__FILE__) + '/../lib/transcoder.rb' 3 4 context "Transcoder" do 5 6 Dir.glob('videos/*').each do |video| 7 it "should support #{video}" do 8 Transcoder.convert(video, "site/#{video}.flv")[:file_size].should > 0 9 end 10 end 11 end
You can also use it to test model validations:
1 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') 2 3 4 describe "Product" do 5 @@valid_product_attributes = { 6 :name => 'WTF', 7 :description => 'LOL', 8 :price => '10.0', 9 :tax => '22.0' } 10 11 12 before(:each) do 13 14 end 15 16 it "should create a new instance given valid attributes" do 17 Product.create!(@@valid_product_attributes) 18 end 19 20 @@valid_product_attributes.each do |name, value| 21 it "should not allow blank #{name}" do 22 lambda do 23 Product.create!(@@valid_product_attributes.except(name)) 24 end.should raise_error(ActiveRecord::RecordInvalid) 25 end 26 end 27 end
How to extract video metadata with mplayer and Ruby
You can easily extract video metadata into a hash with mplayer and Ruby:
1 class Metadata 2 3 def self.get(file) 4 command = "mplayer -vo null -ao null -frames 0 -identify #{file}" 5 metadata = nil 6 7 Open3.popen3(command) do |input, output, error| 8 metadata = output.readlines.grep(/ID_.*/).collect {|name| name[3..name.length].split('=')} 9 metadata = metadata.inject({}) {|hash, (key, value)| hash[key.to_sym] = value.chomp; hash} 10 end 11 12 return metadata 13 end 14 15 end
Then in an RSpec test do:
1 it "should support 3gp" do 2 metadata = Metadata.get("videos/sony_ericsson_k850i.3gp") 3 4 metadata[:DEMUXER].should == 'mov' 5 metadata[:VIDEO_FORMAT].should == 's263' 6 metadata[:VIDEO_WIDTH].should == '176' 7 metadata[:VIDEO_HEIGHT].should == '144' 8 end
Note to self: metadata.demuxer would perhaps be a slightly better syntax.
How to use RSpec and ZenTest in a standalone Ruby project
First install the RSpec and ZenTest gem:
1 $ sudo gem install rspec zentest
Next create the spec folder:
1 $ cd project_folder 2 $ mkdir spec
Save the following to spec/helper.rb:
1 $LOAD_PATH.unshift File.dirname(__FILE__) + '/..' 2 3 require 'rubygems' 4 require 'spec' 5 #require 'spec/rake/spectask' not needed, because ZenTest supports rspec now
Now create spec/transcoder_spec.rb, and add the following test to it:
1 require File.dirname(__FILE__) + '/helper' 2 require File.dirname(__FILE__) + '/../transcoder.rb' 3 4 context "Transcoder" do 5 setup do 6 # Setup your stuff here 7 end 8 9 it "should support 3gp format" do 10 Transcoder.convert("me_and_you.3gp").should == true 11 end 12 end
Let’s not forget the class we’re testing, put this code in lib/transcoder.rb:
1 class Transcoder 2 def initialize 3 end 4 5 def self.convert(file) 6 return true 7 end 8 end
Note that autotest automatically looks for your code in the lib folder.
Now run the test with the zentest command:
1 autotest
Change your files and autotest will run the test again.
Tip: Read Getting started with Autotest – Continuous Testing and Setting up autotest to use Growl on OSX for more information on how to increase your productivity.
Error when installing Mongrel from gem
I received the following error when installing Mongrel from the gem repository:
1 marko@x61s:$ sudo gem install mongrel 2 Updating metadata for 281 gems from http://gems.rubyforge.org 3 complete 4 Building native extensions. This could take a while... 5 ERROR: Error installing mongrel: 6 ERROR: Failed to build gem native extension. 7 8 /usr/bin/ruby1.8 extconf.rb install mongrel 9 extconf.rb:1:in `require': no such file to load -- mkmf (LoadError) 10 from extconf.rb:1
The fix is to install the ruby development package:
1 sudo apt-get install ruby1.8-dev