Ruby Enterprise Edition symlinks

Shell Script (Bash) posted 11 months ago by christian

This will save you from playing with PATH:

   1  sudo ln -fs /opt/ruby-enterprise-1.8.6-20090610 /opt/ruby-enterprise
   2  sudo ln -fs /opt/ruby-enterprise/bin/gem /usr/bin/gem
   3  sudo ln -fs /opt/ruby-enterprise/bin/irb /usr/bin/irb
   4  sudo ln -fs /opt/ruby-enterprise/bin/rake /usr/bin/rake
   5  sudo ln -fs /opt/ruby-enterprise/bin/rails /usr/bin/rails
   6  sudo ln -fs /opt/ruby-enterprise/bin/ruby /usr/bin/ruby

Passsenger.conf is also simplified:

   1  LoadModule passenger_module /opt/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-2.2.5/ext/apache2/mod_passenger.so
   2  PassengerRoot /opt/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-2.2.5
   3  PassengerRuby /opt/ruby-enterprise/bin/ruby

Tagged ree, passenger, ruby, symlinks

How to fix "bad URI(is not URI?)"

Ruby posted about 1 year ago by christian

This URL contains special characters, which Ruby can’t handle:

   1  >> URI.parse 'http://www.yr.no/sted/Finland/Västra_Finland/Askainen/varsel.xml'
   2  URI::InvalidURIError: bad URI(is not URI?): http://www.yr.no/sted/Finland/Västra_Finland/Askainen/varsel.xml
   3  	from /usr/local/lib/ruby/1.8/uri/common.rb:436:in `split'
   4  	from /usr/local/lib/ruby/1.8/uri/common.rb:485:in `parse'
   5  	from (irb):5

Your browser can probably open the URL. To fix this error encode the URL before handing it to the parse method:

   1  URI.parse(URI.encode('http://www.yr.no/sted/Finland/Västra_Finland/Askainen/varsel.xml'))
   2  => #<URI::HTTP:0x18bfb40 URL:http://www.yr.no/sted/Finland/V%C3%A4stra_Finland/Askainen/varsel.xml>

Tagged uri, url, ruby, parse

How to parse geonames.org data with Ruby

Ruby posted about 1 year ago by christian

   1  require 'rubygems'
   2  require 'ostruct'
   3  require 'time'
   4  
   5  class GeoName < OpenStruct
   6  end  
   7  
   8  class GeoNames
   9    class << self
  10      def parse(file)
  11        File.new(file).each_line do |line|
  12          g = GeoName.new
  13          s = line.chomp.split("\t")
  14          g.geonameid = s[0]
  15          g.name = s[1]
  16          g.asciiname = s[2]
  17          g.alternatenames = s[3]
  18          g.latitude = s[4]
  19          g.longitude = s[5]
  20          g.feature_class = s[6]
  21          g.feature_code = s[7]
  22          g.country_code = s[8]
  23          g.cc2 = s[9]
  24          g.admin1 = s[10]
  25          g.admin2 = s[11]
  26          g.admin3 = s[12]
  27          g.admin4 = s[13]
  28          g.population = s[14]
  29          g.elevation = s[15]
  30          g.gtopo30 = s[16]
  31          g.timezone = s[17]
  32          g.modification_date = Time.parse(s[18])
  33        
  34          yield g
  35        end
  36      end
  37    end
  38  end
  39  
  40  file = "geonames/features.txt"
  41  
  42  GeoNames.parse(file) do |place|
  43    p place.geonameid
  44    break
  45  end
  46  

Inspired by this article

Tagged geonames, geocoding, ruby

Select 10 most recent files with Ruby

Ruby posted about 1 year ago by christian

   1  base_dir = '/tmp/'
   2  Dir.entries(base_dir).select{|f| File.file? "#{base_dir}#{f}" }.sort_by { |f| File.mtime("#{base_dir}#{f}") }.reverse[0..9]

Tagged ruby, dir, files

How to parse Apache logs with Ruby

Ruby posted about 1 year ago by christian

Only supports the combined format at the moment…

   1  class ApacheLog
   2    FORMATS = {
   3      :combined => %r{^(\S+) - - \[(\S+ \+\d{4})\] "(\S+ \S+ [^"]+)" (\d{3}) (\d+|-) "(.*?)" "([^"]+)"$}
   4    }
   5    
   6    class << self
   7      def each_line(log_file, log_format = FORMATS[:combined])
   8  
   9        f = File.open(log_file, "r")
  10  
  11        f.each_line do|line|
  12          data = line.scan(log_format).flatten
  13  
  14          if data.empty?
  15            p "Line didn't match pattern: #{line}"
  16  
  17            next
  18          end
  19  
  20          yield data
  21        end
  22      end
  23    end
  24  end
  25  
  26  
  27  log_file   = ARGV[0]
  28  
  29  ApacheLog.each_line(log_file) do |data|
  30    host, date, url_with_method, status, size, referrer, agent = data
  31  end

Tagged apache, statistics, logs, ruby, combined, format