Cups & HP-Lib on raspberry Pi to create a new paperless life

I tend to hoard paper. It’s not something I like doing, but I have a hard time differentiating the important bills from the non important, so I tend to keep everything which usually piles up until it falls over and I spend a day going through it, throwing out most things.

There must be a solution to push all of these documents in a digital format to one of the many services online (read dropbox/google drive). Although I’m aware doxie exists, about this time I bought a raspberry Pi, and have had a ball making LED’s flash, but I couldn’t help thinking that there must be a better use for it.

So, I think you all know where I’m going with this. My plan:

Raspberry Pi to be connected to my HP coloursmart c7280 all-in-one device (of which the printer no longer works, so it’s only use is now a scanner).
I’ll wire up (via breadboard) a number of switches & buttons.

6 switch, dip Switch will determine document type (it’ll light a specific LED – I’ll have printed labels next to the LED’s)

The Button will initiate the process which will:

  • Start the scan
  • Rename the document (This part is still needs to be solved)
  • Upload the document
  • Clean up

Capybara, RSpec, Swinger and a dash of awesome

When trying to get Capybara-rspec working I went googling first, to see how others have done it… People suck and generally over complicate the simplest tasks… Heres how I got it all working!

# Gemfile
 
  gem 'rspec-rails'
  gem 'capybara'
  gem 'swinger'

Add rspec rails, capybara and swinger, then install your gems and generate rspec skeleton files

bundle install
rails g rspec:install

In your spec/spec_helper.rb file require your gems.. a quick note, I force spec to use “test” env, why wouldn’t it do this by default? I’m sure theres an answer, but it doesn’t feel write, and everything works just fine like this!

# spec/spec_helper.rb
 
ENV["RAILS_ENV"] = 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'swinger'
 
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
 
RSpec.configure do |config|
  config.mock_with :rspec
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
end

I really like the idea of my integration tests living in a directory called “spec/integration”, so I create it, and put some tests in there.

# /spec/integration/user_integration.rb
require "spec_helper"
 
describe 'A user session' do
 
  it 'should allow a user to login', :driver => :selenium do
    visit '/'
    click_link 'Login'
    fill_in 'Email', :with => "user@markyg.com"
    fill_in 'Password', :with => "password"
    click_button 'Sign in'
    find('Signed in successfully')
  end
end

A quick note, swinger allows us to add :driver => :selenium, or :driver => :culerity to our specs, this is how we can easily switch drivers.. if you leave this out, it’ll use rack.

Raw Output Of Html In Rails3

As of rails3 the <%=h tag is now applied by default to all of your <%= @content

So for instance in a message:

  ## Controller
  @message = "Unfortunately an error occurred: Sorry, this product is no longer available, if you think you received this in error, please <a href='/contact/us'>contact us</a>"
 
  ## View
  = @message

would result in

Unfortunately an error occurred: Sorry, this product is no longer available, if you think you received this in error, please <a href='/contact/us'>contact us</a>

To remove htmlentities being sanitized you can use the raw() method helper

  ## Controller
  @message = "Unfortunately an error occurred: Sorry, this product is no longer available, if you think you received this in error, please <a href='/contact/us'>contact us</a>"
 
  ## View
  =raw @message

would result in


Unfortunately an error occurred: Sorry, this product is no longer available, if you think you received this in error, please contact us

Be careful how you use this, if @message was set from user input, than they could inject some nasty javascript or flash in your page.

Rails3: Undefined Method Includes Values

I can across an error in Rails3 today when I was trying to implement a scope.

scope :latest, order("created_at asc").first

When trying to run that Scope I’d get

undefined method 'includes_values' for <#Product:rarrarrar>

It had me scratching my head since the trace showed me nothing. So I went through the activerecord codebase, and sure enough it was something stupid that I did.

The scope should return an ActiveRecord object, and not a result set. So simply removing the .first off my scope resulting in a working scope

 scope :latest, order("created_at asc")

Its a simple fix, but googling came up with nothing. So I hope this helps someone else who comes across this error.

How To: Install Node Js And Roll Out Your First Project

So after NodeKnockout I have to say I learnt a lot about the simplicity that comes with developing a nodeJs app! A few people have asked me about how easy it is to roll out their own app, and I’ve walked a few people though it, and finally decided it warranted a blog post!

You’ll be walked through how to set up:

Quick Note

I’m doing this on a mac, but it should be the same on any posix system (linux, bsd, etc) (sorry windows, but if you want to be a serious developer, and .Net isn’t doing it for you, then change your OS)

So lets get this started!

Install NodeJS

Visit the NodeJS website and download the latest node package. It’ll come in tar.gz format. At the time of writing this, its node-v0.2.0.tar.gz. So I’ll pull it down, and install it!

On the console do:

  wget http://nodejs.org/dist/node-v0.2.0.tar.gz
  tar xvf node-v0.2.0.tar.gz
  cd node-v0.2.0
  ./configure
  make
  sudo make install

The sudo is required as node installs /usr/local/lib/

Once thats done, check to make sure you have it installed

  node --version => 0.2.0

Install NPM

Visit the NPM site. There is a link to the github repo that has some extra info not on the site. I recommend visiting it and reading the readme..

After you’ve done that, to install:

  curl http://npmjs.org/install.sh | sudo sh

And then check if its installed

  npm --version => 0.1.27-11

You might have a later version, again this was the latest at time of writing.

Express

Express is a sinatra like framework that wraps nodejs. It looks pretty sexy! It uses Jade by default, which is a HAML like templating language. Very sexy indeed.

  sudo npm install express
  sudo npm install jade

Rolling out a new nodejs app!

Express has a few generators, which makes life easier for all involved.

  express my_new_project

Go through the project and have a look at the directories/files. The important one there is the app.js file. This file is used to run your new project!

To run your node server run

  node app.js

You can then point the browser at http://localhost:3000 and bingo!

Where to find packages?

There is a good list of hosted Addons & Modules. Or just a bit of googling will come up with a few extras.

In further articles I’ll write a little bit more about how to modify the app, add extra actions, etc.

Node.Js Knockout Retrospective & Dns Bot App

It was a crazy 48 hours! My team members @coenhyde and @nathanhoad and myself had little knowledge of node.js and really didn’t know how to even start a project. But we investigated a bunch of frameworks and decided on:

ExpressJS as our main framework.
Jade as our template language (which was integrated into express)
Less for our stylesheets
MongoDB for our database server

It all started with a mad rush to understand what the hell was happening within node, and more importantly how we could implement our idea! The event based architecture that node is built around made for an interesting adventure since our whole team is mainly PHP and Ruby on Rails developers.

The first day was spent finding our feet, given the massive amount of nodeJs plugins and frameworks, but by the end of the day we had a good idea of the scope of the technology and more importantly a good idea of how much code we could cut within the remaining 24 hours.

We decided to ditch our original idea and move more towards a simple tool that every technologist has been craving! We called it DNS Bot App and its primary purpose is to assist you in the propagation of a domain.

It works by pinging a variety of root servers all around the globe and building a picture of where your domain is resolving. for this first iteration it will show you A records, but stay tuned for more to come!

All in all, Node Knockout was in my opinion a complete success, the goal of the competition was to get a few more people involved in Node and more importantly build some buzz around it. Personally my goals going into the competition was simply to learn a new language, and although I’m nowhere near an expert, I really feel I’ve crossed that first hurdle that exists when learning a new language!

So if you think that DNS Bot App may be useful for you now or in the future, please show some support and Vote for us!

So a big shoutout to the node.js team, node knockout crew and of course my team members. It was a cool beer filled and sleep deprived ride, but one I’m looking forward to doing again!

Wordcountr.com

I just wrote a simple rails app to count words in a block of text!

You can find it at http://wordcountr.com/. I built it mainly to play around with rails3 but also I hated the fact there wasn’t an easy way to count words in a document.

So I’ve created this. I hope to implement some Natural Language Processing in the future to give more details about the block of text a user submits!

Rails3 With Cucumber, Rspec

Initially I had a few problems getting rspec and cucumber (especially gherkin) working with rails3 beta and ruby 1.9.2! Mainly these problems I ran into seemed to deal with bundler.

So easy enough, to get cucumber and rspec working, add the following to your Gemfile

group :test do
  gem 'webrat'
  gem 'rspec'
  gem 'capybara'
  gem 'database_cleaner'
  gem 'cucumber-rails'
  gem 'cucumber'
  gem 'rspec-rails'
  gem 'spork'
  gem 'launchy'
end

then run

bundle install

chances are the first time you try to run your tests (especially if you’re using rvm) you’ll have segfaults displayed. To fix this manually remove the natively compiled gets and reinstall them using the gem command.

i.e.

gem uninstall gherkin nokogiri 
gem install gherkin -v=2.1.5
gem install nokogiri -v=1.4.3.1

Do this for each of your segfaulting gems and things should start to work!

Hudson Ci Server Running Selenium/Webdriver Cucumber In Headless Mode Xvfb

I was determined to get our CI server running cucumber features in headless mode. I ran into a few webdriver problems which resulting in a small monkey patch (capybara patch being written and submitted) for capybara.

This is how I got it running on a fedora 12 server (and some tips on getting it working on an ubuntu server).

Install Xvfb

On fedora

sudo yum install xorg-x11-server-Xvfb

on ubuntu

sudo apt-get install xvfb

Running Xvfb

To run Xvfb, in console simply type, then set a environment variable to bind all X requests from the command line to go to the new virtual deskspace

  Xvfb -ac :99
  export DISPLAY=:99

This will launch a new virtual display bound to display 99.

Install X11VNC to see whats happening!!

This is very cool. You can actually set up a vnc server and bind it to the virtual display to see what is happening!!!

On Fedora

   sudo yum install x11vnc

On ubuntu

 sudo apt-get install x11vnc>

To run

Simple run the following command! And point your VNC client to the installation server!

 x11vnc -display :99

Test it by going to the command line and typing something like xterm or firefox!

Hudson

Install the hudson gem

The hudson gem is in the gems repo, but for docmentation go http://github.com/cowboyd/hudson.rb. I generally do this as the user “hudson”.

  gem install term-ansicolor
  gem install yajl-ruby
  gem install httparty -v=0.5.2
  gem install builder -v=2.1.2
  gem install thor -v=0.13.6
  gem install hpricot 
  gem install hudson --pre

Install hudson

On the command line just type

 hudson server

this will install hudson, and save configuration to the ~/.hudson directory

Checkout your application to this box

First thing is to ensure you have set up a git user and email in the global config

  git config --global user.name "hudson"
  git config --global user.email "hudson@myserver.com"

Then clone your app down, install all associated gems and create your testing database

  git clone git@github.com:username/mygitrepo.git
  rake gems:install
  rake db:create:all
  RAILS_ENV=test rake db:schema:load

Test cucumber/webdriver runs

Type cucumber in your apps directory

Create a new Hudson Project

Thanks to the hudson gem, you can set up the inital project skeleton via the command line (isn’t technology great) Here we can use the hudson create command and tell hudson to use the current directory, and that hudson is installed on localhost:3001

  cd #{to_your_rails_app}
  hudson create . --host localhost --port 3001

Set up the hudson project

Log into the project, if its on your local computer, point your browser to http://localhost:3001 and click on your project, also skip this next section.

If you’re installing hudson on a remote server you’ll need to ssh port forward since hudson at this point is only listening on localhost.

  ssh -L 8123:localhost:3001 hudson@mytestserver.com

Then open a browser and point it to http://localhost:8123 and click on your project

Set up the build

  1. Remove all defined build sets.
  2. Add a new build step “Execute Shell” with the command “cp config/database.tests.yml config/database.yml”
  3. Add a new build step “Exceute Shell” with the command “RAILS_ENV=test rake gems:install”
  4. Add a new build step “Execute Shell” with the command “RAILS_ENV=test rake db:migrate”
  5. Add a new build step “Execute shell” with the command “spec spec” # if you have specs
  6. Add a new build step “Execute shell” with the command “DISPLAY=:99 cucumber” # with cucumber

Press the save button.

If webdriver doesn’t append to your virtual display

If webdriver doesn’t append to your Xvfb then you can put this script in /features/support/capybara_webdriver_patch.rb

# Added profile support for capybara, so we can run our tests in headless mode
class Capybara::Driver::Selenium &lt; Capybara::Driver::Base
  def self.driver
    unless @driver
      profile = Selenium::WebDriver::Firefox::Profile.new
      profile.load_no_focus_lib = false
      @driver = Selenium::WebDriver.for :firefox, :profile =&gt; profile
      at_exit do
        @driver.quit
      end
    end
    @driver
  end
end

Some after thoughts

Some extras you should set up before you think your finished. They’re outside of the scope of this document but there are plenty of docs out there on the interwebs who can give you all the information you need to rock and roll.

  • At the moment its open to everyone, go to global configuration and set up ACL’s
  • Your builds aren’t very continuous yet, you can schedule builds periodically by going into the project configuration area, or I believe you can tie it into githubs callback mechanisms

Observers In Rails

In an effort to reduce the clutter of before and after callbacks in your Rails models an Observer can be used. In a simple use case, you may want to give the user 500 credits in your online store when they signup.

Without an observer your User model would look something similar to

class User < ActiveRecord::Base
   def before_create
     self.credits = 500
  end
end

Which is fine, if thats all that is in your model. But once your model starts to grow its generally a good idea to keep the callbacks in a different location! Observers to the rescue! With a Rails Observer your code would look similar to

# models/user.rb
class User < ActiveRecord::Base
end
 
# models/user_observer.rb
class UserObserver < ActiveRecord::Observer
   def before_create(user)
     user.credits = 500
  end
end

Now tell Rails that you want your observer to be included

# config/environment.rb 
Rails::Initializer.run do |config|
  config.active_record.observers = :user_observer
end

Rails will automatically understand that this observer is for the user class based on the name (convention over configuration to the rescue). Although you can also have an observer where the model can’t be inferred by the name, in this case you can explicitly define the models the observer should be observing.

For instance, lets say we have an Audit observer, for a number of models.

  class AuditObserver < ActiveRecord::Observer
    observe :user, :item
 
    def after_update(record)
      AuditTrail.new(record, "UPDATED")
    end
  end

This will observe the user model and on an observed update will create a new audit trail record. Sexy huh?

So no excuses, clean your code up, use observers! Its a sexy design pattern which I feel is completely underused!



Nice job!
You now have 30 lives.
Use them wisely, my friend.

Konami Easter Egg by Adrian3.com