Archive for the ‘programming’ Category

CSS frameworks & Compass

Friday, May 1st, 2009

I have been looking harder at CSS frameworks recently because I am always looking for ways to improve my craft.
I have been playing with typogridphy a little recently after reading giles post on using it for a miniapp.

More recently I have read about where typogridphy comes from. I had heard about blueprint, quite a while ago, but I still thought that building things myself was the best way to go. Now, I am tired of dealing with CSS annoyances so frameworks look more appealing. Typogridphy was built on top of 960 which is a somewhat opinionated CSS framework.(saying CSS framework sounds funny to me). 960 gives you a bunch of reasonable default classes and such to make getting started easier.

This is pretty cool. But, there are some drawbacks. The problem is they create semantically void classes and ids that you need to use for your stuff to work.

Even though I want my hand to be held a bit, I also want my markup to make sense.

What to do?

I have been using sass for styling for a while now, and it has made dealing with CSS easier by making nesting more convenient and added things like variables which CSS does not have. It has been pretty awesome.

Also it has mixins, which are sort of like bags of properties that can be inserted into classes, etc. Check it out.

=heading_text
  :font
    :size 120%
    :weight bold
h1
  +heading_text

These mixins allow you to do all sorts of interesting things, like get around the having to use semantically meaningless classes etc for stuff to work. Chris Eppstein wrote Compass, a CSS meta-framework. The idea is to port major CSS frameworks to sass as mixins and then create a way to access them without having all the code hanging out in your app tree.

It redefines what would be classes, ids etc as sass mixins instead, so you can add them to your semantic class names. Which in is what I want to be able to do anyway.

Check it out.

Sprinkle and Passenger Stack

Tuesday, April 21st, 2009

I started using sprinkle with the passenger-stack recently. So far, it has been easy to get started with and fairly intuitive. I chose it rather than puppet or some of the other declarative server configuration DSL because it doesn’t need to install anything extra on the server to work.

This is great for the kinds of things I have been working on, because they are fairly small, and don’t need many servers.
I think if I were managing more things, I might choose puppet, because the overhead would be justified.

One of the things I like about sprinkle, is the DSL is pretty straight forward, eg the package definition for passenger looks like this:

package :passenger, :provides => :appserver do
  description 'Phusion Passenger (mod_rails)'
  version '2.1.3'
  gem 'passenger' do
    post :install, 'echo -en "\n\n\n\n" | sudo passenger-install-apache2-module'
 
    # Create the passenger conf file
    post :install, 'mkdir -p /etc/apache2/extras'
    post :install, 'touch /etc/apache2/extras/passenger.conf'
    post :install, 'echo "Include /etc/apache2/extras/passenger.conf"|sudo tee -a /etc/apache2/apache2.conf'
 
    [%Q(LoadModule passenger_module /usr/local/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-#{version}/ext/apache2/mod_passenger.so),
    %Q(PassengerRoot /usr/local/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-#{version}),
    %q(PassengerRuby /usr/local/bin/ruby),
    %q(RailsEnv production)].each do |line|
      post :install, "echo '#{line}' |sudo tee -a /etc/apache2/extras/passenger.conf"
    end
 
    # Restart apache to note changes
    post :install, '/etc/init.d/apache2 restart'
  end

I had some problems, like the version of passenger in the passenger-stack master branch on github is a point release behind the phusion’s, which is annoying because when passenger updates itself to 2.2.0, the configs are not updated and apache tells you it can’t find the mod_passenger.so file.

Also, it puts the passenger config in a slightly unusual place(/etc/apache/extras), for an apache module, as well as appending stuff to /etc/apache2/apache2.conf. This prevents it from idempotency, because if you run it twice, it will add additional lines to the config files. So, in the vein of scratching my own itch and what have you–you know, that open source thing–I rewrote it.

package :passenger, :provides => :appserver do
  description 'Phusion Passenger (mod_rails)'
  version '2.2.0'
  gem 'passenger' do
    post :install, 'echo -en "\n\n\n\n" | sudo passenger-install-apache2-module'
 
    # Create the passenger conf file
    loading = %Q(LoadModule passenger_module /usr/local/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-#{version}/ext/apache2/mod_passenger.so)
    conf = %Q(PassengerRoot /usr/local/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-#{version}
PassengerRuby /usr/local/bin/ruby
RailsEnv production)
 
    post :install, "echo '#{conf}' >> /etc/apache2/mods-available/passenger.conf"
    post :install, "echo '#{loading}' >> /etc/apache2/mods-available/passenger.load"
    post :install, 'a2enmod passenger'
    # Restart apache to note changes
    post :install, '/etc/init.d/apache2 restart'
  end
 
  verify do
    has_file "/etc/apache2/mods-enabled/passenger.load"
    has_file "/usr/local/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-#{version}/ext/apache2/mod_passenger.so"
    has_directory "/usr/local/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-#{version}"
  end
 
  requires :apache, :apache2_prefork_dev, :ruby_enterprise
end

Now, it takes advantage of the debian convention of keeping module loading and configuration files go in /etc/apache2/mods-available that are symlinked into /etc/apache2/mods-enabled by the a2enmod utility. This is immediately obviously awesome to anyone who has contemplated the horror of trying to parse the apache main config to see if the stuff they want to add is already there and needs updating.

I also added some verifiers so that it won’t rerun if there were no errors. Pretty cool, no?

Ruby is awesome–lazy scripting

Monday, April 20th, 2009

Sometimes I find myself doing tedious things like trying to grab the dollar amounts from text copied from the web. More and more often I turn to irb for these sort of things. For example, a few minutes ago, when I wanted to analyze my spending habits I copied the data into a file and manipulated it.

I grabbed the lines from the file

irb(main):001:0> lines = File.readlines 'transactions'


And selected those with dollar signs (with a little effort–sometimes I forget whether it is =~ or ~=).

irb(main):002:0> monies = lines.select {|l| l ~=  /\$(.*)/}
SyntaxError: compile error
(irb):2: syntax error, unexpected '='
monies = lines.select {|l| l ~=  /\$(.*)/}
                               ^
	from (irb):2
	from :0
irb(main):003:0> monies = lines.select {|l| l =~  /\$(.*)/}

Then I grabbed the numbers using map.

irb(main):008:0> nums =monies.map {|m| m.sub( /\$(.*)/,$1).to_f}

From there, I could do all sorts of things.

I could sum numbers.

 total =nums.inject(0){|sum,i|sum+i}

I could get the average.

total/nums.length

I could even see how much of the total was due to transactions under $20.

nums.reject {|i|i>20}.inject{|s,i|s+i}

In short, I like ruby.

Ruby in Practice is Good

Thursday, April 16th, 2009

I have been reading Ruby in Practice over the past week or so as you know. Currently, I am on page 114 which talks about using active resource to consume RESTful webservices( you could use RESTClient, but thats another story) and has a box about the ever awesome BlankSlate class.

I haven’t really learned many “whoah, ruby does what?? Sweet” things from it, most of those are behind me. Now, I get more “Whoah, X project really abstracts that annoying thing into a nice interface.” kind of feelings, which are more satisfying in some ways and less in others. I guess what I am saying is that I have grown to know ruby much better over the past six months or so using it at work every day, than the past, uh, five? years. Which is sweet.

But back to the book( I ramble when I feel braindead):

The things I have learned from reading this book remind me of ruby itself. It has glued together various bits of ruby that I know, in the same way you can use ruby to glue together other technologies. Most of my ruby smarts have been picked up through hacking at things and reading blog posts by knowledgeable people, but that sometimes leaves some gaps in my knowledge. This book has helped me to tie some of the things I have learned together(w00t Hebbian Learning).

For instance, spec tasks

While I know that rspec has a set of extensions for rake, I had not used them outside rails, in fact in the project I was working on I just wrote the following.

task :spec do
  sh "spec --colour --reverse #{FileList['spec/**/*_spec.rb']}"
end

The problem with this is that when any of the specs fail, I get a huge trace from the ran shell script that I have to scroll past before I can see the failed specs.

But, if I included the spec tasks I could do stuff more like this.

require 'spec/rake/spectask'
Spec::Rake::SpecTask.new('spec') do |task|
  task.spec_files =FileList['spec/**/*_spec.rb']
  task.spec_opts = ['--colour','--reverse']
end

Now things work like they do when you generate the tasks from within rails. I had read the lib/tasks/rspec.rake file when I was looking over the generated code in one of my rails apps, but it was a little too complicated to grok easily.

The other things I am looking for are cool projects that I should be more aware of. Sometimes, they are only mentioned but that doesn’t mean they are not awesome. Some that I would like to play with include:

  • Chronic a date string parser that tries to be able to handle stuff like: “next tuesday” and “summer”
  • FasterCSV I saw this at Mountain West Ruby Conf and made a mental note to read it at some point(so much for mental notes)
  • Heckle runs tests. Breaks your code. Sees if the tests fail like they should.

Later.

Merb in Action: Ch 1 Thoughts

Wednesday, April 8th, 2009

Last night I read the first chapter of what will become Rails 3 in Action. So far, I really am enjoying it.

One of the things that frustrates me the most in my own projects is this feeling that things need to be big and get abstracted before they can be useful. I guess it might be a hold over from being a student and wanting to over achieve, or maybe just the classic problem of the hobbyist who tinkers but never manages to really build anything.

It is a gumption trap, to use a Zen and the Art of Motorcycle Maintenance-ism; a thought that sort of stalls my work on a project.

“How will this be useful to anybody?”

“How can I get this to a usable state?”

I think I am getting better at this problem, but sometimes I feel like I have this irresistible urge to over-architect things that gets me into trouble.

What this has to do with Merb in Action?

Well, the thing I thought was really cool was where the authors linked to the pastie that started it all, clocking in at ~120 lines.

I look at that and I think two things.

  1. I usually over think things
  2. I can do this.

The beginning of merb is completely grokable. It reminds me that something doesn’t need to be fully designed. It just needs to be useful enough.

or you could just say:

YAGNI

You Ain’t Gonna Need It.

YAGNI is part of the reason why I have been trying to use BDD in a more disciplined way. By using the Feature->Spec->Implementation workflow, there is less temptation to add neat features on the backend that are never used. I have worked on a number of projects where I ended up writing more code than I needed, than was used.

Writing from the outside in helps to manage that some. After all “no code is faster than no code.”

New Books

Tuesday, April 7th, 2009

I just ordered Ruby in Practice and Rails 3 in Action from Manning(they had a one day only 45% off promo).

The Rails 3 book won’t actually be coming out for a while, but in what I think is an ingenious publishing move, people who preorder can look at drafts of chapters as the book is worked on. It reminds me a little of how Lawrence Lessig is doing the next edition of his books. Except of course totally different.

The thing that makes them similar is the added participation of future readers. It isn’t that this is a new idea. People have been soliciting comments on publications for as long as they have been around. Technology just changes the timescale and the scale of the projects.

Anyway, cool.

Right now, I have Ruby in Practice and through the fourth chapter of Merb–Rails 3 in Action. More later.

Sweet.

Mountain West Ruby Conf Day 2

Monday, March 16th, 2009

After yesterday, I have a lot of grokking to do. And, some projects to start.

The talks, like the day before were great. And having the opportunity to talk with so many people about what they do with ruby was enlightening.

I wanted to write up my thoughts about yesterday, but they haven’t really congealed yet.

Short summary:

  • Ruby is awesome.(of course)
  • I need to be more aware of my metaworkflow
  • Rhodes looks really slick.
  • Adhearsion would be fun to build something with(eg a podcast engine)
  • Destroy is a funny word to users(rails)
  • Of the bicycle gears of software development,cucumber is the outmost one
  • Suite.add(test) if test.value > test.cost
  • be extremely pedantic when you first start trying to use a new methodology–even if it sucks(in the ‘man, why all this typing’ sort of suckage you know makes you want to be lazy).
  • don’t confuse concise with terse.
  • maybe I should try to make a theramin sim with the wiimote
  • energy not time is the most precious resource

Silly Simple Twitter Search App with Javascript

Friday, March 13th, 2009

I’ve been meaning to spend some time playing with twitter’s API. So, I decided to build a really simple js app on top of the search api.

Essentially, I want to show a list of statuses from a search, say ‘baroquebobcat’ and do it in the least amount of effort possible.

There are more complicated ways to do it, but I wanted to see how easy it was.

First, I thought I would do what the API docs suggest and poke around with curl.

Since all I want is to do a search, like you would using a browser, why not try that, only with json, for extra awesomeness.

$ curl 'http://search.twitter.com/search.json?q=baroquebobcat'

Running that gets me a long string of json:

{"results":[{"text":"Talkin' 'bout sandwiches", ...

Breaking that down with structurally looks like

{
  "results": [
    { "text":"Talkin' 'bout sandwiches",
      "to_user_id":null,
      "from_user":"baroquebobcat",
      "id":1307603293,
      "from_user_id":125785,
      "iso_language_code":"en",
      "source":"web<\/a>",
      "profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/79555083\/face02_normal.jpg",
      "created_at":"Tue, 10 Mar 2009 22:04:12 +0000"
    },
    ...
  ],
  "since_id":0,
  "max_id":1307851898,
  "refresh_url":"?since_id=1307851898&q=baroquebobcat",
  "results_per_page":15,
  "next_page":"?page=2&max_id=1307851898&q=baroquebobcat",
  "completed_in":0.033281,
  "page":1,
  "query":"baroquebobcat"
}

Results contains the statuses that match the search, the other 1st level attributes are metadata about the search.

For my silly simple js app, all I really need is a subset of this vertiable bevy of infos.

I am not even going to bother with the search metadata and am going to throw away most of the statuses data too.

For now, I only care about the text, and the user who tweeted it.

{
  "results": [
    { "text":"Talkin' 'bout sandwiches",
      "from_user":"baroquebobcat"
    },
    ...
  ]
}

So, I have this list and a url to get stuff. How does that go into the javascript?

we need a callback. Because of all that cross site scripting etc stuff, you can’t do ajax requests for data like this
(well you can as long as you proxy it through your host somehow like
this guy ).
So, the way to get all this stuff so it can be executable is to add a callback parameter to the request. It wraps the json with a function call so you can use it in a script tag.

$ curl 'http://search.twitter.com/search.json?q=baroquebobcat&callback=awesome'
awesome({"results":[{"text":"Talkin' 'bout sandwiches", ...})

so in html file can do

and awesome will be executed with the response from the search.


cut to the chase

So, we need a callback.

because we lack imagination, lets call it twitter_callback.


  Super Silly Twitter App
  
  
  
  
  

Pretty cool, huh?

What that does is define a call back that just writes each status in the search with the user’s name: their tweet on each line.

Actually, that is a bit boring.

All we have is the list, and it’s not very portable but it demonstrates how it works.

You could make it more robust by using a javascript function to insert the script tag, thereby allowing you to dynamically set the query.

There are problems with using js to do all the work here. It can be slower and it also can be a security risk–if you don’t trust twitter.

Laters.

Dreamhost Passenger Sinatra or more yak hair

Friday, January 30th, 2009

In my ongoing adventures with Sinatra and shared hosting, I have discovered a number of new ways to shave a yak.

My preblog project warmup has been taking shape over the past week or so as a microapp that scrapes my library account and gives me a convenient way to find out whether I have any books overdue or any holds that are ready for pick up. The actual application was easy. Use scrubyt + modifications(firewatir is not a good dependency on a server) to login, grab my checked out books, etc and store them. Then, serve that info up on a simple web page.

The Problem: dependency hell + funny runtime environments.

Initially, I had bmizerany’s sinatra-0.9.0.4 vendored, because I wanted to be one of the cool kids and grab things off github. It worked beautifully, ran nicely, specs passed–on my machine. On the server, it kept giving me this error

Exception NameError in Passenger::Rack::ApplicationSpawner (uninitialized constant Rack::MethodOverride)

After some searching, pain etc, I discovered that the problem was that the rack on dreamhost is 0.4 and sinatra0.9.x.x uses rack 0.9+. So, I tried installing the new rack locally and using the GEM_PATH env to pick it up. Unsurprisingly, this failed. I tried a number of different tactics like using the apache setEnv directive and using ruby’s ENV hash. None of it worked.
Eventually, I gave up on getting 0.9.x.x running and replaced my vendored copy with 0.3.3

$ sudo gem install -v 0.3.3 sinatra
$ cd /path/to/app/vendor
$ rm -rf sinatra
$ gem unpack -v 0.3.3 sinatra
$ git add *
...
$ git commit -a -m 'downgraded sinatra'
cap deploy

I had to change my config.ru to be sensical to sinatra as it was, but it worked.

Somehow that doesn’t sound as annoying as I found it. I spent several hours working this out, and now it works more or less perfectly. Passenger can’t run the scraper because scrubyt also requires hoe >=1.5.0 which dreamhost doesn’t have in their shared gems(theirs is 1.2.1). It can’t see my local gems, but I set up a cron job which should run as me and thus be able to see it.

Blogging is hard

Sunday, January 11th, 2009

The problem I have is pretty typical, I imagine. You start with the goal of writing something witty and hopefully interesting, and you sit in front of the screen and wait for ideas. The problem with this approach is that there is no context. The solution space is too big and you find yourself stuck, looking at a continuum of possibilities that is truly challenging.

When that happens, I write about writing.

I have never felt that I was particularly good at writing. Except for a brief period around middle school where I wanted to write science fiction. I started a story, wrote something like 11 pages, but I became bored with it, or maybe just felt I was at the point where I needed more structure.

It is always hard to come up with new things to say. It takes time or a spark, but once it starts and you get in the flow, it just comes. Programming works like that for me sometimes too. It is like writers block. I can see the problem, but it is just a cloud–an abstract set of possible needs and uses for the application. And, when the problem space is ill defined, so is the solution space.

Part of the problem is my knowledge of my abilities with regards to building software. I know that I can build almost anything( we build compositions of thought stuff), given time and a good plan. But, being able to build anything is different from building the right thing. I get caught up in the good that it is nigh impossible to build the good enough.

I figure I am not the first to face this issue, after all isn’t that the point of YAGNI, and things like BDD. Divide and Conquer yes, but first and formost start!

So this is a problem I have had with writing for longer than coding, and I deal with it in the same sort of way. Just start. At some point I find that I know enough, or I have the first word(or leaf of a synatx tree as it may be) and I just start working.

This not knowing enough to narrow the options is a problem that stops me from doing as much testing on my code as I would like to. I want to use BDD, etc, but I have a tough time discerning where I should use acceptance tests vs specs. But, I am dealing with this, by reading more docs and by reading more code. I don’t really like the tutorials that I see about testing for the most part because they don’t have any real meat. So, I have been surfing github looking for projects tests and reading them to get a feel for how others use them.

As a side effect, I am reading more code and becoming more familiar with others styles, which is never a bad idea.