Thursday, December 31, 2009

Http Here!

Announcing httphere, a tiny ruby command line utility that simply serves files for what they're worth, from the directory where you run the command.

It's really simple to use, just change directory to the one you want to serve files from, and run "httphere". It tries to default to port 80, but you'll probably need to be sudo to do port 80. Or you can run on any port by adding the -p PORT option:

cd ~/mywebsite
httphere -p2244
# open web browser to http://localhost:2244/index.html,
# - assuming there's an index.html right here. :)
What do you need it for? Well, most of the time you may not need it, but you might as well install it because there's no telling when you'll want it. When you'd rather let your browser use HTTP to look at an html file on your filesystem instead of using file://... seriously, file:// will mess with your resource urls that start with a slash (/). Use httphere and you won't have that problem.

Install

sudo gem install httphere

Enjoy!

Friday, October 16, 2009

Integrating with QuickBooks from the Browser is easy now.

Just a short note...

Bet you never even thought of doing things with QuickBooks from the browser! Jon Hoyt recently approached me about working together to create a program for browsing inventory in QuickBooks from other computers within the same network. At first we were thinking about making a fully-bundled program that communicated with QuickBooks over the network through my QuickBooks Connector, and we'd work on it together one evening a week and release it just before Christmas. But we soon realized that instead of a bundled program, I already have all the tools ready to create a simple HTML + Javascript webpage that does everything we want. That brought our expected release time from Christmas all the way back to... October 27th, only a week and a half away!

The tools involved to build a webpage that communicates with QuickBooks:
1) An HTML webpage
2) Cross-domain Ajax, I've created a couple of JS libs for managing communication with QuickBooks
3) The QuickBooks Browser Connector, which is soon to be released (look for it in about a month, or if enough people really want it, could be sooner)

I'll be blogging again about it when it's ready for use! First version will be FREE, we're thinking of adding features for $1 each. :)

Tuesday, September 15, 2009

Action prerequisites in Rails

Have you ever had a series of pages (controller actions/views) that must be seen in order, because each page sets up prerequisite information for the next? Perhaps like a wizard, a series of pages with forms on them that bring you eventually to a page that does something with that data.

I've come up with a really simple and elegant solution for managing prerequisites in Rails. Since it doesn't take much code, it's easier to just plop it in your project instead of as a plugin (at least in my opinion)...


Here's how it works: Using an around_filter, I can provide a way to jump out of the action when checking the prerequisites. To specify (and check) a prerequisite, simply give it a name: "prerequisite :login". To check the prerequisite, it will call the "login?" method. If that returns true, everything's fine and we'll continue on our merry way into the action, or to the next prerequisite (if you have several). If false, it throws a :prerequisite_unmet, which means it exits the current method and continues on in our catcher method. Next order of business is simply to redirect to the :login action.

A better mategem

I've just built a nicer "mategem" command, which will open up a rubygem's root directory in textmate. It allows you to simply open the latest gem version using the same reference you use in your project to activate the gem.

You know how you can activate a gem by running the gem command (e.g. "gem 'rails'"), or also by simply requiring a library file that is provided by that gem in the root of its library (like "require 'action_controller'" will activate the 'actionpack' gem)? Well, you can use the same two ways to reference a gem when you run mategem:

  • "mategem rails" will open rails
  • "mategem initializer" will also open rails
  • "mategem action_controller" will open the actionpack gem
  • "mategem sinatra" will of course open the sinatra gem
Here is the code for the command. Basically how it works: Tries to reference it as a gem name, if that works, take the last-added load path as the gem path. If that doesn't work, try to have `gem which` find it as a gem-provided library. If that works, we have the path given to us. Parse out where the gem's root folder is, and open it up in mate!


To install, create this file, make it executable, and put it somewhere in your PATH. I would highly advise creating a ~/.bin folder and putting that in your PATH.

Monday, August 24, 2009

My version of Rubygems

Just recently, I decided not to use rubygems in my project. Instead, I just install all the gems I need into a "gems" directory in the project directory:

gem install some-gem -i gems
Then, before too much happens in ruby, add /lib to your path:
$:.unshift(File.expand_path("#{Dir.pwd}/lib"))
Then, create lib/rubygems.rb with this SINGLE line of code:
Dir.glob('gems/gems/*/lib').each {|gemdir| $: << gemdir}
. There you are. All set. All the rubygems you've installed into your project get pre-loaded and ready to go. If they happen to require 'rubygems', too bad so sad, they can't have it. They get your little replacement rubygems instead of the real one. So be careful if you use a gem that actually needs the "real" rubygems, but most don't. They just might be written poorly. If they use the 'gem' command (which in your case is irrelevant if you have all the correct gems installed into the project), you can add this to your lib/rubygems.rb:
def gem; return true; end
Problems solved. No more rubygems. You won't have to be afraid of accidentally upgrading gems without preparing this project first; and your code will actually load faster too. Yay!