Monk Seal Software, up and running

By kenglish

For those of you that don’t know, I’m rebranding under Monk Seal Software. New blog to come soon.

categoriaProgramming commentoNo Comments dataOctober 11th, 2012
Read All

Ruby on Rails Class in Hawaii, October 3rd

By kenglish

I’m teaching an Intro Ruby on Rails Class at University of Hawaii’s Pacific New Media. Hope you can make it, here’s the flyer.

Ruby on Rails Class Flyer

categoriaProgramming commentoNo Comments dataSeptember 24th, 2011
Read All

Migrating my heroku/postgres project to Jruby

By kenglish

A little inspired by all the JRuby buzz, I thought I’d take an existing rails app that I have running on heroku and migrate it to Jruby. I wanted to test how easy/hard it would be:

  1. I made a branch of my project:
    git checkout -b jruby-test
  2. Install jruby via rvm
    rvm install jruby-1.6.2
  3. I editted my .rvmc to use Jruby instead of MRI, from:
    rvm use 1.9.2@proj

    to

    rvm use jruby-1.6.2@proj-jruby
  4. I updated my rmvc from :
    rvm use 1.9.2@proj

    to

    rvm use jruby-1.6.2@proj-jruby
  5. I edited my Gemfile. This is where I knew it would get tricky.

    1. Removed:
      gem 'pg'
    2. Added:
      gem 'activerecord-jdbc-adapter'
      gem 'activerecord-jdbcpostgresql-adapter'
      gem 'jdbc-postgres'
    3. Found out hard way had to remove ruby-debug19. DUh, dude :)
    4. Texticle gem does not work. This makes sense because it is plugin to postgres ActiveRecord driver. However, I use this for my minimalistic search functionality.
  6. Ran ‘rake spec’ on the project. My tests for the search feature failed as expected because of the missing Texticle dependency. I had one test that was failing on
    Regular expression. The regular expression looked like this:

    if last_item =~ /sector|leverage_point/

    When examined the code a bit, I found out that last_item was actually a symbol. I guess MRI automatically converts symbols to strings for Regular Expressions and JRuby does not. I changed the line to:

    if last_item.to_s =~ /sector|leverage_point/

    and it worked fine

  7. Ran ‘rails s thin’ and my app was up and running.

Not bad for a JRuby amatuer. Although not having the Texticle gem is a show stopper on this project.
It’s not that Texticle is the best solution for search but we have some code written in it.

categoriaProgramming commento1 Comment dataJuly 22nd, 2011
Read All

Common Accurev Command Line commands and their git/svn counter parts

By kenglish

I’m using Accurev for a new project, thought I’d throw up a blog post of the commands that I use most commonly:

Make a new workspace, similar to ‘git clone’ or ‘svn co’:

accurev mkws -w workspacename -b streamname_from_server -l local_directory

Note: accurev workspaces are similiar to a cloned git repo, kind of but not really…. Are you confused yet?

Update my workspace from the stream, similiarly to a “git pull”:

accurev update

Get status of my workspace, ie, see what’s changed. Same as “git status” or “svn st”:

accurev stat -n

With the ‘accurev stat -n’ command, unlike git, the files shown will be in relation to your current working directory. So if you do:

cd db/migrate
accurev stat -n

You will only see files you have changed in your db/migrate folder. (Why are you changing your migrations???)

In accurev, file not checked into the work space are called “external” files, you see all these with this:

accurev stat -x

Add external file, same as “git add” or “svn add”:

accurev add -x path/to/file
# or add a directory
accurev add -x -d directory

Revert a file, same as “git checkout” or “svn revert”:

accurev purge path/to/file

Promote all kept files to the stream, kind like “git push” or “svn commit” but more like “git push”

accurev promote –c “JIRA-666 comment” –k

I’ll add more as my project progresses…

accurev keep -R -n -c "codenarc fixes" grails-app/domain

categoriaProgramming commento3 Comments dataJuly 12th, 2011
Read All

Override behavior of “rake spec” task

By kenglish

Basically, I have a bunch of folders in my spec directory for a rails project:

spec/acceptance
spec/controllers
spec/fixtures
spec/helpers
spec/javascripts
spec/lib
spec/mailers
spec/models
spec/support

The spec files in my acceptance folder are my capybara/selenium tests and still in experimental phase. I don’t want them to run every time and definitely don’t want the CI server running them. However, when I run ‘rake’ or ‘rake spec’, it runs everything in my ‘spec’ folder. I want it it to run the specs in every directory except acceptance.

I discovered there is not easy way to override a rake task. Redefining the task appends to it which to me is a pretty intuitive default behavior. Apparently, dchemsky knows about this and has opened an issue with rake. I was able to piece together a solution from an old blog post by Jay Fields.

Feel free to comment if you have a better way to do it.

 
require 'rake'
require 'rspec/core/rake_task'
 
class Rake::Task
  def abandon
    @actions.clear
  end
end
Rake::Task[:spec].abandon
 
RSpec::Core::RakeTask.new(:spec) do |t|
  t.pattern = "spec/{models,views,controllers,helpers}/**/*_spec.rb"
end
 
namespace :spec do
  desc "Run the code examples in spec/acceptance"
  RSpec::Core::RakeTask.new(:acceptance => "db:test:prepare") do |t|
    t.pattern = "spec/acceptance/**/*_spec.rb"
  end
end

categoriaProgramming commentoNo Comments dataApril 29th, 2011
Read All

Using Rails 3 helpers and routes in the console or a rake task

By kenglish

To use a Rails 3 helper in the console

If this is your helper:

module ItemsHelper
  def custom_item_display(item)
    item.to_s
  end
end

And you have a route that looks like this:

  resources :items

On the console, you would do this:

ruby-1.9.2-p136 :001 > item = "hello world"
ruby-1.9.2-p136 :002 > helper.extend ItemsHelper
ruby-1.9.2-p136 :003 > helper.custom_item_display(item)
 => "hello world" 
ruby-1.9.2-p136 :004 > include ActionController::UrlWriter
 => Object 
ruby-1.9.2-p136 :005 > items_path
 => "/items"

In the rake task you do this:

namespace :example do
  desc "Do example items"
  task :items => :environment do
    item = "Hello World"
    include ItemsHelper
    puts "ItemsHelper.custom_item_display(item) = #{custom_item_display(item)}"
 
    include ActionController::UrlWriter
    puts "items_path = #{items_path}"
  end
end

Hope that helps.

categoriaProgramming commentoNo Comments dataFebruary 5th, 2011
Read All

Using TinyTDS to connect to SQL Server with Ruby on Rails 3 (for Mac OSX)

By kenglish

Note: please see comments by Ken Collins (metaskills.net) at the bottom of this entry. He mentions a better way to do this without even installing FreeTDS.

You will need FreeTDS to connect to MSSQL Server on Unix-based environments. You MUST use Homebrew to install FreeTDS on the Mac. MacPorts is evil and will not install FreeTDS correctly. Installation of Homebrew is covered excellently in the Homebrew installation docs:
https://github.com/mxcl/homebrew/wiki/installation

Edit the FreeTDS configuration for the FreeTDS library:

brew edit freetds

Replace from “def install” to “end ” with:

def install 
  args = ["--prefix=#{prefix}",
          "--with-tdsver=7.0",
          "--enable-msdblib",
          "--mandir=#{man}"]
 
  system "./configure", *args
  system 'make'
  system 'make install'
end

Install FreeTDS:

brew install freetds

Add an entry to the bottom of your freetds.conf for your MSSQL Server. This file is found at /usr/local/etc/freetds.conf:

[my_sql_server] 
host = <hostip>
port = 1433 
tds version = 7.0

Add the TinyTDS and activerecord-sqlserver-adapter gems to your Rails Gemfile.

  gem 'tiny_tds'
  gem 'activerecord-sqlserver-adapter', :require => false

Run bundler to install the gems:

bundle install

Finally, add the connection to your database.yml file:

development:
  adapter: sqlserver
  mode: dblib
  dataserver: my_sql_server
  database: my_database_name
  username: my_username
  password: xxxxx
  timeout: 5000

A reference for installing UnixODBC (which I don’t recommend), you can follow this tutorial:
MSSQL2005 on Rails on Snow Leopard (the easiest way I know how.)

categoriaProgramming, Tech commento7 Comments dataJanuary 13th, 2011
Read All

Honolulu Hacker’s Guide to Lite Weight Backpacking

By kenglish

Based on some backpacking experience over the years, I’ve come up with this list of ways to reduce the weight of your pack. Read and Learn.

  • Toilet Paper: Anyone who has moved before knows one thing: Books are heavy. Guess what books are made of?…. PAPER! That’s right, the same thing that they use to make toilet paper. I suggest you lighten your toilet paper load. Next time you go to the bath room, make note of how much toilet paper you need. How many times do you wipe? If you average 3-4 wipes, can use only 2 squares per wipe and can estimate you will be going to the bathroom 2 times a day, do some math, figure out how much mountain money you will REALLY need. If you are a guy, congratulations, you can bring less TP. Don’t tell the girls this, they may ask you to carry their tent.
  • Deodorant: Seriously? All or some of your backpacking buddies are gonna smell worse than the slums of Manila. Why are you going out our way to smell good? Leave this item at home.
  • Camera: Skip it. Don’t worry, one of you buddies will bring a camera especially if you are with a big group. One of them may even carry a tripod, a monopod and so much other camera gear that he is going to need a hip replacement by the age of 35. Don’t be that guy. Plus, think about it: if you are taking all the picture, you won’t be in any of them. My wiser older brother once told me: a picture without a person is postcard. If you want pictures of the scenery, just go on flickr and do a search. Chance are pretty good someone else has been there. Make their image your wallpaper. When your co-workers comes, you can brag about what a great trip you had and how much cooler you are than them. Go ahead, gloat away. You’ve got the wallpaper to back it up.
  • Tent: So, you hiked all the way out into the woods so you can sleep INDOORS. Forget that. I have seen someone make a very sustainable shelter out of a rain tarp. You just need to be creative. Cavemen didn’t always make it back to the cave and they didn’t have REI either. What did they do? If the going gets really rough, you can sneak into your buddy’s tent.
  • Water: I don’t think I need to ask this but what human do you know that will deny another person water?… and you can always count on others to carry water.

This only some of the great ideas that will be present in my new book on backpacking. Please e-mail to preorder it.

categoriaProgramming commento1 Comment dataJune 24th, 2010
Read All

Force download of fasta files and aln files with Apache

By kenglish

If you are serving fasta files or alignment files on your server, you may want to force users to download them instead of previewing them in the browser. My application would return the fasta files as Content-Type text/plain. I wanted to force it to application/x-fasta and force download. This is accomplished rather easily in Apache with the following directive:

  <FilesMatch "\.(?i:fasta)$">
    ForceType application/x-fasta
    Header set Content-Disposition attachment
  </FilesMatch>
  <FilesMatch "\.(?i:aln)$">
    ForceType application/x-aln
    Header set Content-Disposition attachment
  </FilesMatch>

You will have to enable the apache module “mod_header” for this to work.

Using Ruby & Hpricot to find lowest mortgage rate in Hawaii

By kenglish

Each week the Honolulu Board of Realtors publishes a report of Hawaii Mortgage Rates. To find the lowest rate for your category is difficult. A non-programming solution would be to copy it into excel, delete all the rows that you need and then sort by the rate column. This takes too much time so I wrote a ruby script that parses this data. This is also a demonstration of how to use the ruby tool Hpricot, an HTML Parser. You will need to install the Hpricot gem for this to work

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
require 'rubygems'
require 'hpricot'
require 'open-uri'
 
#term = '1-YR ARM'
#term = '30-YR Fixed'
term = '15-YR Fixed'
 
doc = Hpricot.parse(open("http://www.hicentral.com/MortgageRates.asp")) 
 
rates = [] 
lender_name = ""
 
(doc/"table"/"table"/"tr").each do | row |
   arr =[]
   (row/"td").each do | cell|  
     arr << cell.inner_html() 
   end
   if arr[1] =~ /15-YR Fixed/
      lender_name = arr[0]
      arr.delete(lender_name) 
      lender_name.sub!('<br />',' - ')
   end
   next unless arr[0] =~ /#{term}/
   lender_data ={} 
   lender_data[:lender_name] = lender_name    
   lender_data[:term] = arr[0] 
   lender_data[:apr] = arr[3].to_f
   rates << lender_data
end
 
5.times do | rank |
  puts "Losest Rate ##{rank+1}"
  row = rates.min{|a,b| a[:apr] <=> b[:apr] } 
  puts "  Name: #{row[:lender_name]}"  
  puts "  Term: #{row[:term]}" 
  puts "  APR: #{row[:apr]}"  
  rates.delete(row)
end

Here’s what’s going on:

  • On line 7 you will notice that I am interested in the 15-Year mortgage rate. You can change this value to get the report for the term you want.
  • On line 9, the program will download the latest rates from the hicentral.com website and parse the page returning an hpricot doc object.
  • From line 14 to 30, the program parses each line in the mortgage rate table. The logic is custom to this table. The table is unusual because the lender name is first cell only on the first line (15-YR Fixed) for each lender. To accommodate this, we match the line that has “15-YR Fixed” in second position and delete the lender name from the array (lines 19-23). We then assign the data to our summary data structure (lines 25-29)
  • Finally, we show the top 5 lowest mortgage rates (lines 32-38). To do this we use the Ruby max method (line 34). We delete the current max element so it is not counted in the next loop iteration (line 38).

Thomas Lecklider has a great tutorial on how to use Hpricot called Using Hpricot to Traverse and Parse HTML.

Your commends are welcome. Give some refactoring advice if you like. I have wordpress plugin for pre tag so to write ruby code just do:

<pre lang="ruby">
puts YEAH
</pre>

categoriaProgramming commento1 Comment dataDecember 14th, 2009
Read All