rss

FFMPEG get video file duration: Ruby

1

Category : Ruby on Rails

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def get_video_file_duration(inputFilename)
            command = "ffmpeg -i "
                             + inputFilename.to_s + " 
                             2>&1 | grep 'Duration' 
                             | cut -d ' ' -f 4 | sed s/,//"
                             output = `#{command}`
 
          if output =~ /([\d][\d]):([\d][\d]):([\d][\d]).([\d]+)/
               duration = (($1.to_i * 60 + $2.to_i) * 60 + $3.to_i) * 10 + $4.to_i
          end
 
          #return duration.to_s
          return "#{$2}:#{$3}"
 end

 

  • Share/Bookmark

My Google Knol articles

6

Category : General, Ruby on Rails

Ruby on Rails implementation of OpenId

Ruby on Rails implementation for Paypal | Download Code

Ruby on Rails implementation for Authorize.net

  • Share/Bookmark

How to Create a File Upload Progress Bar in Rails, Passenger, Prototype and Low Pro

Category : Ruby on Rails


How to Create a File Upload Progress Bar in Rails, Passenger, Prototype and Low Pro from Erik Andrejko on Vimeo.

  • Share/Bookmark

Rails flash messages helper

2

Category : Ruby on Rails

display_flash(:error) # to display a specific flash message
display_flash # to display all flash messages

1
2
3
4
5
6
7
8
9
10
11
12
13
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  FLASH_TYPES = [:error, :warning, :success, :message]
  def display_flash(type = nil)
    html = ""
    if type.nil?
      FLASH_TYPES.each { |name| html << display_flash(name) }
    else
      return flash[type].blank? ? "" : "#{flash[type]}"
    end
    html
  end
end
  • Share/Bookmark

Beauty of Maths

1

Category : Ruby on Rails

1 x 8 + 1 = 9
12 x 8 + 2 = 98
123 x 8 + 3 = 987
1234 x 8 + 4 = 9876
12345 x 8 + 5 = 98765
123456 x 8 + 6 = 987654
1234567 x 8 + 7 = 9876543
12345678 x 8 + 8 = 98765432
123456789 x 8 + 9 = 987654321

1 x 9 + 2 = 11
12 x 9 + 3 = 111
123 x 9 + 4 = 1111
1234 x 9 + 5 = 11111
12345 x 9 + 6 = 111111
123456 x 9 + 7 = 1111111
1234567 x 9 + 8 = 11111111
12345678 x 9 + 9 = 111111111
123456789 x 9 +10= 1111111111

9 x 9 + 7 = 88
98 x 9 + 6 = 888
987 x 9 + 5 = 8888
9876 x 9 + 4 = 88888
98765 x 9 + 3 = 888888
987654 x 9 + 2 = 8888888
9876543 x 9 + 1 = 88888888
98765432 x 9 + 0 = 888888888

  • Share/Bookmark

Installing mod_rails(Passenger)

Category : Ruby on Rails

Mod_rails has come to rescue people form tedious rails application development. With mod_rails the work has become a lot easier, you do not have to worry about different things just few steps and your application is up and running, ofcourse there are options to modify the deployment to your needs.

There are few prerequisites for mod_rails to work, first you need Apache and secondly you need Ruby and Rubygems.

Step 1: We need to update rubygems
sudo gem update

Step 2: We need to install mod_rails(passenger)
sudo gem install passenger

Step 3: Now we need to install Apache2 headers
sudo aptitude install apache2-prefork-dev

Step 4: Now we need to install Apache2 module
sudo passenger-install-apache2-module

There will be a screen which takes you further. If there are any dependencies that are needed then it will be asked for on this screen.

After successful installation there is just one thing left, we need to copy some lines to the Apache2 main config file , open the file “apache2.conf” in a text editor and then paste these lines at the end of the file:
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-1.0.4/ext/apache2/mod_passenger.so
RailsSpawnServer /usr/lib/ruby/gems/1.8/gems/passenger-1.0.4/bin/passenger-spawn-server
RailsRuby /usr/bin/ruby1.8

Restart Apache
sudo service apache2 restart

Done!!

  • Share/Bookmark

Compile & install Nginx web server

Category : Ruby on Rails

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 # See:
# - http://en.wikipedia.org/wiki/Nginx
# - http://wiki.codemongers.com/NginxGettingStarted
# - http://wiki.codemongers.com/NginxInstallOptions
# - http://wiki.codemongers.com/NginxCommandLine
# - http://wiki.codemongers.com/NginxConfiguration
 
export PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin"
 
mkdir -p ~/Desktop/Nginx
cd ~/Desktop/Nginx
 
curl -L -O http://sysoev.ru/nginx/nginx-0.7.2.tar.gz
tar -xzf nginx-0.7.2.tar.gz
 
curl -L -O http://downloads.sourceforge.net/pcre/pcre-7.7.tar.gz
tar -xzf pcre-7.7.tar.gz
 
cd ~/Desktop/Nginx/nginx-0.7.2
./configure --help
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/sbin --with-debug --with-http_ssl_module --with-pcre=../pcre-7.7
make
sudo make install
 
which nginx
otool -L /usr/local/sbin/nginx
open /usr/local/nginx
open -e /usr/local/nginx/conf/nginx.conf
 
nginx -v
nginx -V
sudo nginx # start server
sudo nginx -t
 
open http://localhost:80
open http://localhost:80/50x.html
 
sudo nano /usr/local/nginx/conf/nginx.conf # editing ... server { listen 8080; ...
 
# reload configuration
sudo kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
 
# stop server
sudo kill -15 $(ps -auxxx | egrep "[n]ginx.*master" | awk '{ print $2 }') 2>/dev/null
  • Share/Bookmark

Create a Hash from two Array objects

Category : Ruby on Rails

Requires Ruby 1.8.7 or higher.

1
2
3
4
5
6
keys = [1,2,[3,4]]
[1, 2, [3, 4]]
values = ['a','b',['c','d']]
["a", "b", ["c", "d"]]
Hash[ * keys.zip(values).flatten(1) ]
{1=>"a", [3, 4] => ["c", "d"], 2 => "b"}
  • Share/Bookmark

File Upload Helpers

Category : Ruby on Rails

Handy little helpers to check if a file was provided to a controller, and to save it.

1
2
3
4
5
6
7
8
9
10
11
 def file_provided?
   [StringIO, Tempfile].include?(@file.class) and @file.size.nonzero?
 end 
 
 def save_file
   if @file.is_a? Tempfile
     FileUtils.cp(@file.path, path)
   elsif @file.is_a? StringIO
     File.open(path, "wb") { |disk_file| disk_file << @file.read }
   end
 end
  • Share/Bookmark

Renaming your .rhtml to .erb on EdgeRails

Category : Ruby on Rails

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
## just slap this in your rake file or in lib/tasks
 
namespace 'views' do
  desc 'Renames all your rhtml views to erb'
  task 'rename' do
    Dir.glob('app/views/**/*.rhtml').each do |file|
      puts `svn mv #{file} #{file.gsub(/\.rhtml$/, '.erb')}`
    end
  end
 end
 
 ## or if you have rhtml and rxml you could probably use the following (untested)
 
 namespace 'views' do
   desc 'Renames all your rhtml views to erb'
   task 'rename' do
     Dir.glob('app/views/**/*.rhtml').each do |file|
       puts `svn mv #{file} #{file.gsub(/\.(rhtml|rxml)$/, '.erb')}`
     end
   end
 end
  • Share/Bookmark

Amit Yadav is Digg proof thanks to caching by WP Super Cache