New Hawtnesses

Rails,Ruby — squarism @ 12:04 pm

Wow, how can anyone keep up?

Zeus

Video of Zeus in action. Is it the new spork?

Super fast Rails boot times, but not just booting. Everything! Like DRB on steroids.

FishFish shell

`brew install fishfish`. The new ZSH? A fork of the Fish shell. Featured in the above video link. Be sure to uninstall fish through homebrew if you have already. It’s not the same thing.

YADR


YADR – for me, the new codegram/vimfiles? Impressive documentation, will really change my muscle memory.

Prezto

Included with YADR, looks to be a replacement for oh-my-zsh (my fav). Will have to try it out and update this.

Replace Guard with Autotest if using Minitest

Rails — squarism @ 11:20 pm


I love Guard. I’ve posted about it and used it a lot. But it’s got some problems right now with all the downstream gems and addons that I like.

Of course, a more elegant solution is to not pre-fork and use Gary Bernhardt’s (and others’) advice of extracting domain objects from rails and running tests over that. This is fine within plain old ruby objects but sometimes you need to load rake or factory_girl and that requires rails. So let’s assume you can’t isolate your tests into lib/ or extras/ and you need fast tests while loading your entire Rails environment. That’s where Guard and Autotest come in.

The newest Guard installs Listen which doesn’t work. So I had to back down a version of listen. Added gem ‘listen’, ’0.4.7′ to my Gemfile to get rid of polling warning. 0.5.0 is latest but it has a problem right now (bug opened 2 days ago as of today, grr). All that is unrelated to the real problem.

Spork-minitest doesn’t work with guard. The issue is that spork-minitest doesn’t support any options like -r or -e that the guard is passing.

So let’s switch to autotest. But first, let’s look at my current configuration and I’ll show you what I had to change. This is what my Guardfile looked like with Guard on a Rails project:

Delete that whole Guardfile and let’s switch to autotest with spork and minitest.

This is what my Gemfile looked like with Guard:

This is what my Gemfile looks like when switched to Autotest:

It works great! As I said in the Gemfile comments:

  • Start `spork` in one terminal
  • Run `bundle exec autotest -cf` in another terminal


-c is for avoiding re-running rake test on red/green
-f is for fast start to avoid running rake test on startup

Save a file like products_controller_test.rb and watch it run. Save the app/controllers/products_controller.rb and watch products_controller_test.rb run.

The only problem is that it reacts a little bit slow because of a 1 second default sleep in Autotest. So this is my entire .autotest file which includes a hack to reduce that 1 second to 0 seconds. No impact to CPU and it reacts as fast as Guard did.

I plan on doing a pull request to address this problem. So watch for that.

Let’s Play with Capistrano

Rails,Ruby — squarism @ 11:05 pm

In which I try to learn capistrano for the first time. Pushed a simple Rails 3 app to a mac dev box running rbenv, zsh, nginx and passenger. It’s LONG and edited only for length. So strap in and observe a completely blind learning session.

Final deploy.rb file here:
gist.github.com/3410415

Let’s Play with Capistrano from Chris Dillon on Vimeo.

Memcached with Rails 3

Rails,Ruby — squarism @ 6:13 pm

I never had a reason to play with memcached and it was on my list of things to learn. Below I will demonstrate an example app very quickly and simply. I’m not doing anything more complicated than simple primitive storing. If you are going to store any objects in memcached, dalli gem will take care of this for you (ymmv).

First let’s create a rails app and an rvm gemset to play in.

If you don’t have rails in your global gemset, you can install rails now. I used the 3.1 RC here just to test out 3.1 and memcached at the same time.

Dalli is written by Mike Perham (awesome guy) and simply wires up Rails.cache to be our memcached server. This is convenient and more standard than creating your own global variable or other configuration.

Next, let’s install memcached if we haven’t already:

You can also start memcached as a service under Mac using the homebrew instructions. I like to leave things in the foreground when I’m first setting them up or grok’ing.

Ok, now we can create our dummy app.

Our dummy rails app is going to have a slow controller action in it. In this case, it could be a row count of a huge database. In your case it could be a slow network call or a result of an expensive SQL operation.

First, edit your Gemfile:

Edit config/environments/development.rb

Rails c should work at this point and this line:

Now generate a controller:

Create a route in routes.rb:

Create a simple view in app/views/posts/index.json.erb:

Now when you hit the page it should be slow the first time but fast the second time. After 3 seconds, it’s slow again.

So you can see that memcached returned the cached object and avoided the sleep call. In the real world, this would equate to lower page rendering time or better application performance.

How to write a Ruby and Rails 3 REST API

Rails,Ruby — squarism @ 11:18 pm

Background

I’ve always wondered how I’d go about publishing a real REST API on the web to do something. In this example, we’ll create an employee manager app-thing. It’s not particularly interesting but it shows what “API” means. In another bit, we’ll create an “API” meaning a library to interact with this web service.

Caveats

If you are just getting started with Rails and Ruby, you might find that this tutorial is really long and includes a lot of syntax. I love Rails to death but many people say it has a “large surface area”. That means that it’s hard to learn and the API is broad and vast.

As an alternative, I suggest taking a look at Grape and Sinatra if you are finding Rails to be a little too heavy. However, make sure you read up on what features you will lose when going thinner. It’s not always clear and you might find things like autoreloading were assumed in Rails but now you have to get a plugin for Sinatra (or Grape). BTW, I think Grape by Intridea is the better Web API framework at least vs Sinatra. For pure APIs, it may be better suited for the job than stripping down Rails.

The rails app

Ok enough caveats and intro. First, create a new rails app. I’m going to assume you have RVM installed and know how to create gemsets.
rails new rest_api
cd rest_api

update:This was last tested with 3.2.12.

Database setup

In this example we are going to use Sqlite3 but you can easily substitute MySQL or some other database here. To keep this post on topic and short, we’ll use the sqlite3 default for spiking.

(more…)

Arduino Cat Faucet with Mongodb and Rails

Arduino,Noteworthy,Rails,Ruby — squarism @ 11:01 pm

I built a robot arm for my cat during a month-long geekcation. :) Here are some shots of the web interface. The graph shows the percentage of the day that she drinks.

Final hardware rig

Background

My cat likes to drink fresh and cold water directly from the faucet. We get up and turn on the faucet only to leave it running after she’s jumped down. It’s not really a big problem for us but I saw a fun problem that I could work on. As much as this seems like a weird and freakish oddity, it’s a potential start of a smarthome sensor network that may provide some utility. I also saw an opportunity to learn various things such as MongoDB, mechanical construction with Microrax, Rails3 and more development on Arduino with an Xbee module.

(more…)

Rails behind Enterprise SSO

Rails,Systems — squarism @ 8:12 pm

This is a quick write-up without a lot of detail. We hacked together a quick rails app to do provisioning in the style of OIM behind an OAM SSO webgate. The complete guide and detail would be tens of pages so I’ll just give a quick overview for the strategy.

The Goal

  • Ldap authentication
  • No db
  • Sso protected
  • Weblogic deployment

Develop app steps and failures

We used activeldap for the LDAP pieces and defined our user model to narrowly search for a particular objectclass and attributes. Tried to use Authlogic. Fail. Acts as authenticated fail. Devise fail. Ended up using filters and activeldap. Integrating the gems and activeldap was actually kind of hard. A lot of the security gems assume you’ve got activerecord users and depend a lot on the validation helpers etc. So some of the authN gems didn’t work for us. We also had to hack a bit on the activeldap validations. Password policy was non-trivial. I just rolled my own like so:


def validate_password(password)
// initialize a password score
// call password rule methods like:
// check_special_characters
// check_length
// check_uppercase
end

The score from each check method is added to a total score. If the score is greater than zero then your password fails the checks. Each check, a hash for flash[:error] is used so that a precise error message is possible. It works ok except the flash error display is for some reason not ordered correctly.

All configuration constants are stored as YML as an app_config.yml file. For example, the LDAP server, port, password policy rules etc.

For the SSO config, we just detect header as HTTP_REMOTE_USER even though OAM is creating REMOTE_USER. Quick and easy. You have to append the “HTTP_” for the name. It’s a naming convention thing that you can’t do anything about. If your OAM header variable is UNICORNS, then you have to use HTTP_UNICORNS.

We used formtastic for the forms. This was a bit problematic with it trying to detect the activeldap model instead of activerecord.

Testing while developing

Ok so how do you test integration? Are you going to SSO enable your dev laptop? That’s way too hard. You can hardcode the credentials for a while but then eventually you’re going to want to test. I got around this by using a firefox plugin called modify headers. It’s pretty straight forward except for the small detail that you have to keep it open while hitting pages. I thought it would run in the background but it doesn’t. Just keep the modify headers firefox plugin open and it’ll let you create an auth cookie. Don’t worry, this isn’t a security hole. OAM in production won’t let you do this. It’s just used for development.

Warble

install warbler with gem install warbler
Generate default config warble config
Install jruby-openssl because activeldap requires it
Edit config/warble.rb to include jruby-openssl note that you don’t have to have jruby installed or anything.

The rest of the steps are not rails related. Deploy war to Weblogic as usual. Set up a Proxy webgate back to Weblogic for /app (you can’t protecte Weblogic directly with OAM). Protect /app with an OAM policy. If your firefox header test worked then when you turn that off and hit it behind OAM it will work the same. I was able to identify and trust the REMOTE_USER header coming in.

Bam, you’ve got a rails app working in a big scary enterprise SSO environment. The best part about all of this was how fast it went. Compared to JSP/Java EE dev, it was a breeze. The only big multi-day hangups we had was with activeldap. Many gems and auth models really expect you do have your user in the DB. Unfortunately, putting users in the DB creates a silo. Fine for small shops, not so good if you’re using Active Directory, OID, OpenLDAP or Fedora DS (389) for a centralized login.

Alphabet sequences with factorygirl

Rails,Ruby — squarism @ 1:37 pm


I was trying to use this in a factorygirl class to create test data with names like Product A, Product B etc. This isn’t very sophisticated. If you want some smarter dummy data generation with factorygirl, check out this post at the PMA media group.

Anyway, here’s the pure ruby part if you fire up irb:

i = 0
hash = Hash.new
"a".upto("z") do |letter|
hash[i] = letter
i+=1
end

Gives you:
hash.sort
=> [[0, "a"], [1, "b"], [2, "c"], [3, "d"], [4, "e"], [5, "f"], [6, "g"], [7, "h"], [8, "i"], [9, "j"], [10, "k"], [11, "l"], [12, "m"], [13, "n"], [14, "o"], [15, "p"], [16, "q"], [17, "r"], [18, "s"], [19, "t"], [20, "u"], [21, "v"], [22, "w"], [23, "x"], [24, "y"], [25, "z"]]

Or even cleaner:
hash.sort.each {|k,v| print "#{v} "}
a b c d e f g h i j k l m n o p q r s t u v w x y z

It has an off-by-one problem for readability. You actually need to change the i=0 to i=1 for factory girl because the factorygirl sequence starts on 1. Here’s my factorygirl product class:

# let's make an alphabet.
i = 1
hash = Hash.new
"A".upto("Z") do |letter|
hash[i] = letter
i+=1
end

Factory.define :product do |p|
p.sequence(:name) { |n| "Product #{hash[n]}" }
p.price 19.95
p.platform "Example Platform"
end

If you need more than 26 products, just change the upto(“Z”) to “ZZ” or whatever. It’ll scale as far as you want. If you need infinite scaling, don’t use letters. :)

To test, in the console:
script/console test
> 40.times { Factory(:product) }

And this will generate some madness as depicted in the screenshot to the right.

Watchr Unit Tests + Growl + Doomguy

Rails — squarism @ 11:57 am


I read this post about setting up autotest with growl. The little doomguy was a nice effect and got a literal lol out of me (llol?). However, the post was from 2007 and apparently autotest has some problems with 1.9. I’m not running 1.9 but on the eve of rails3, I probably will be. Ok, what’s cooler than autotest? Apparently not much, three hits on google.

Install a gem
I found Watchr. It’s not too hard to set up. sudo gem install watchr to get the gem. If you’re on *nix, grab the filesystem event gem: sudo gem install rev. Now we have watchr up but we need a config.

Create the watchr config
The docs tell you to edit a config/watchr.rb file. Mine came from the examples on their wiki. It worked out of the box. It’s posted here. It’s a basic rails testing config that I haven’t had to edit. It’s very readable if you need to futz with it.

Create a test case
Ok so watchr just runs `rake test` which you can do in your rails app root. But perhaps you don’t have any test cases created. The generators will create them for you. But assuming you don’t have one, create a file named [rails app root]/test/functional/hello_test.rb.

Obviously this is a crappy assertion.

Get Growlnotify
Obviously, you’ll need growl installed. But you also need the growlnotify binary. It’s good to have anyway (I use it in Automator). I didn’t have it by default on two of my machines so I assume that the normal Growl install doesn’t install it. It’s on the Growl 1.2 dmg under extras. You just have to copy it to /usr/local/bin. Download the growl dmg, open the dmg. You don’t need to reinstall growl.
sudo cp /Volumes/Growl-1.2/Extras/growlnotify/growlnotify /usr/local/bin

You can test it with:
growlnotify -t "Title" -m "Message"

Make doomguy happen
Create a directory for the growl icons: mkdir ~/.watchr_images.
Copy these two doomguy faces to ~/.watchr_images/. On mac, hit Shift+Cmd+G and go to the hidden folder.

Now we should have two pngs in our hidden folder.

The watchr.rb file references these images. If you want to put it somewhere else (like maybe under ~/Pictures), you can change the .rb.

Ok Go!
Launch with: watchr config/watchr.rb from your rails app root. Watchr should be in your $PATH because you installed the gem. You should have a blank screen now where watchr is waiting.

When you save a file, watchr will fire. Depending on the file you saved, a different test will run. If you save a controller, for example, the functional test for that controller will fire. If you save a model object, a unit test for that model object will fire. If you want to force a whole test suite to run hit Ctrl+\ in the watchr window.

When things pass:

When things fail:

The growl display setting is “Music Video”. I have it popping up on my 3rd monitor which is less distracting but still visible. However if you put it on your main monitor, the overlay won’t interfere with mouse clicks, which is nice. It makes for a killer setup where I don’t have to test my app with a browser or with contrived/non-automated tests. Now I just need to write more tests.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2013 SQUARISM | powered by WordPress with Barecity