Becoming A Proud Nintendo Wii Owner

Nintendo Wii

I was finally lucky enough to get my hands on a Nintendo Wii. Some people at work had a line on some that they needed for a giveaway and I was able to get the last one at a local Walmart. I’m still stumped as to why on earth these things are still so hard to get, but they are. I ended up paying about $325 for the Wii Console and a second Wiimote and Nunchuck. The first night, I think I played Wii sports for about 6 hours. Man was I sore the next morning. Funny how you try to play the sports like you would in real life. Reminds me of Morpheus asking Neo, “Do you think that’s air you’re breathing?”

Now I have the new Zelda game, which is fun, but hard. It also takes a lot of time to play, which I don’t have a lot of. We’re also borrowing a copy of Mario Party 8, which is kind of silly but is fun to play with friends. I can imagine its hilarious as part of a party or something. I’d like to get a couple more “group” type games like Super Smash Brothers Brawl or Mario Galaxy. I think the game prices are fairly reasonable, but I don’t want to get too far ahead of myself.

Anyway, if you’re lucky enough to find one in a store, I’d definitely get one. Its worth the money!

Sorting Geographical Based Search Results In Ruby on Rails

Ruby on Rails

I’ve spent the last few months on a Ruby on Rails project for a client. I’m integrating a lot of different applications into it, creating quite the “mashup”. One part of the project requires the ability to search the system for results that fit within a radius of a given postal code. So to do this, I need some sort of searching algorithm or application and a geocoding application for zip code relationships. The search results need to be able to be sorted by the different attributes on the results.

So this meant I needed several pieces. One was a searching module. I found ferret and the acts_as_ferret Rails plugin to do full text searching. From what I could gather online, this was one of the best solutions out there. I want to be able to display distances between zip codes, which I can do using GeoKit. So I find myself off and running.

I was able to do everything successfully from getting the right results back to calculating distances (Side note. If you need a start with acts_as_ferret, there’s a good article here at Rails Envy). However, because the distances between zip codes are calculated and not part of the ferret index, I can’t sort by results. Uh oh…

The solution was, in my opinion, a hack, but it works. What I did was let acts_as_ferret handle sorting for everything except distances (it couldn’t do it anyway, so fine). After I get my results back, I decided, well, I guess I can sort them again, right? So, let’s do this:

@total, search_results = MyModel.full_text_search(@search_term, 
  {:sort => s},
  {:include => [:zip_code],
    :conditions => conditions})

This gets me my search results. What about distances? Well, this can be done, even though its an issue performance wise:

for sr in search_results
   sr.destination_distance = round_to(sr.zip_code.distance_to(@search_zip_code), 2)
end

So now each result knows what its distance is from the searched upon zip code. Now what about sorting?

if params[:sort] == "distance"
    search_results = search_results.sort
    @total = search_results.length
elsif params[:sort] == "distance_reverse"
    search_results = search_results.sort
    search_results = search_results.reverse
    @total = search_results.length
end

So now you’re thinking, ok, but how do you know how to sort MyModel? Easy, I decided I’d override <=> for the MyModel class so that a MyModel was less than, greater than, or equal to another MyModel based on distance. So I did this:

def <=>(item)
    if self.destination_distance < item.destination_distance
      return -1
    elsif self.destination_distance > item.destination_distance
      return 1
    else
      return 0
    end
end

So you can see with the example above, I can sort by just calling sort. To reverse the sort, just call reverse after sorting.

So there you go, sorting by distance values. There are definitely drawbacks with this method. First, you have to iterate over all of the search results to set the distance on them. Second, what if I need to sort by some other calculated value? Since I overroad <=> for distance, I can’t really do it for another value. But for now, this works. Maybe I, or someone else, can come up with a better solution.

Uploading and Resizing Images in Ruby on Rails

ImageMagick

In my previous article on using ImageMagick and Mini-Magick to manipulate images in Ruby on Rails, I talked about how to install all of the goodies you’d need to work with images in Rails. I thought I’d expand on this a little bit more and give an example on how I used this cool stuff to upload images and resize them in my Rails application.

You’ll need to set up some HTML code to upload the file to the server. Something like this will suffice:

<% form_tag :action => 'upload', :multipart => true, :id => 'upload_form' do -%>
     <input style="margin-left: 5px;" type="file" id="imageone_file" name="imageone[file]" />
<% end -%>

Now you’ll want to build a model (based on ActiveRecord or not) to save your image for you. For my use, I did based my image model on an ActiveRecord class since I wanted to at least store the file name of the image in my database. But doing that is up to you. Anyway, on to saving the image. In your class, you want to grab the data for the image file in the posted form and save it to the file system. Something like this will suffice:

def image_save(file)
    @file = file
    @content_type = file.content_type.chomp
    @original_filename = base_part_of(file.original_filename)
    @extension = @original_filename[@original_filename.rindex(".") .. @original_filename.length].strip.chomp
    
    self.file_name = "#{epoch_time()}#{@extension}"
    
    is_saved = false
    begin
      if self.file
        if self.content_type =~ /^image/
          # Make the directory for the id of the listing if it doesn't exist
          Dir.mkdir("#{RAILS_ROOT}/public/images/originals/") unless File.exists?("#{RAILS_ROOT}/public/images/originals/")
          
          # What's the new file name?
          
          # Create the temporary file
          File.open("#{RAILS_ROOT}/public/images/originals/#{self.file_name}", "wb") do |f|
            f.write(@file.read)
            f.close
          end
          
          # Crop the image to the sizes we need
          crop()
          
          is_saved = true
        end
      end
    rescue
    end
    
    return is_saved
end

So what are we doing here? First, we grab the content type of the file, its original file name, and the extension of the file. We save this information out to attributes defined on the model itself, i.e.

attr_accessor :file, :content_type, :original_filename, :extension

Then we check to make sure the directory where we want to save the original file exists, and if not, create it. Then we save the file itself. Once we have the file saved, you’ll notice I call a method called crop(). This is my method that resizes the original image and saves the resized images to the file system. How do I do that? Check this out:

  def crop()
    image = MiniMagick::Image.from_file("#{RAILS_ROOT}/public/images/originals/#{self.file_name}")
    if !image.nil?
      # Resize to 360x360
      image.resize "360x360"
      image.write("#{RAILS_ROOT}/public/images/360x360/#{self.file_name}")

      # Resize to 240x240
      image.resize "240x240"
      image.write("#{RAILS_ROOT}/public/images/240x240/#{self.file_name}")

      # Resize to 120x120
      image.resize "120x120"
      image.write("#{RAILS_ROOT}/public/images/120x120/#{self.file_name}")

      # Resize to 80x80
      image.resize "80x80"
      image.write("#{RAILS_ROOT}/public/images/80x80/#{self.file_name}")
      
      # Resize to 40x40
      image.resize "40x40"
      image.write("#{RAILS_ROOT}/public/images/40x40/#{self.file_name}")
    end
  end

As you can tell, I needed several different image sizes. You start with the lowest size and work your way down. Not doing this gets you funky sized images. MiniMagick makes it really easy to just open the file and set the new size for the image and then just write it out to where you want it. Nice!

Ruby on Rails Image Manipulation with ImageMagick and Mini-Magick on OS X

I’m working on a Ruby on Rails project that requires users to be able to upload images. At the same time, we want to resize images so that they appear as we want them to on site. To do this, I found that the best solution would be to use the Ruby Gem Mini-Magick which uses ImageMagick. The benefits of using Mini-Magick is that its lighter weight that just using RMagick. So away I went…

I develop my Rails applications on OS X, so I thought I might have most of the stuff already that I’d need since XCode was installed. I had ImageMagick installed from a previous PHP project I had worked on, so I figured I’d just install the Ruby Gem Mini-Magick.. I did and hooked it up to my application. When running it though, it didn’t work. Bummer. I kept getting cryptic errors like:

ImageMagick command (identify "/tmp/minimagick99445-0.jpg") failed: Error Given 256

Great, what does that mean? So I did some sleuthing on Google and didn’t have much luck for about an hour or so. Then I came across this post at MattKing.org. How could I have missed this? A no brainer that you need to have the image libraries installed before compiling ImageMagick (and thus mini-magick).

So with out further ado, here are the steps you need to perform to get this to work!

Step One – Download Image Libraries

Before compiling and installing ImageMagick, you need to have all of the image libraries installed for the image types you want to manipulate. In my case, I wanted to support JPG, GIF, PNG, and TIFF. So I had to install the following libraries:

Once you have those downloaded, you can go on to Step 2

Step Two – Install Image Libraries

Now that you have all of the image libraries downloaded, its time to install them. Extract them using tar and install them!

JPEG

tar xzvf jpegsrc.v6b.tar.gz
cd jpeg-6b
./configure
make
sudo make install

LibPNG

tar xzvf libpng-1.2.24.tar.gz
cd libpng-1.2.24
./configure
make
sudo make install

GifLib

tar xzvf giflib-4.1.4.tar.gz
cd giflib-4.1.4
.configure
make
sudo make install

LibTiff

tar xzvf tiff-3.8.2.tar.gz
cd tiff-3.8.2
.configure
make
sudo make install

Step Three – Download & Install ImageMagick

With all of the image libraries installed, you can download ImageMagick. I installed mine to the /usr/ directory, but I think /usr/local/ would work as well.

tar xzvf ImageMagick-6.3.8-9.tar.gz
cd ImageMagick-6.3.8
./configure --prefix=/usr/
make
sudo make install

Step Four – Install ImageMagick

You’re almost there! Now that you have all of the image libraries installed and ImageMagick installed, you can go ahead and install the Ruby gem Mini-Magick

sudo gem install mini_magick

One note that I found very helpful on Matt King’s blog post was to check to make sure the configure script for ImageMagick could find the image libraries you wanted to be able to use. Check the output at the end of the configure script and make sure yes is present next to each image library you want to use.

You should be ready to rock now! To use mini-magick, insert the following lines into your environment.rb file:

require 'rubygems'
gem 'mini_magick'
require 'mini_magick'

Now that you’re all set up, you can load up a file and do whatever you want to it using Mini-Magick. Perhaps something like this!

filename = "path to your file"
fileout = "path to output file"
image = MiniMagick::Image.from_file(filename)
image.resize "120x120"
image.write(fileout)

There you go! If something doesn’t work, the first thing I’d check is to make sure all of the libraries and ImageMagick were installed properly. Also check file permissions in case for some reason the directory you’re trying to save your file is locked down so the web server user can’t write to it.

One more side note before I finish. I was able to use these exact steps to install ImageMagick and Mini-Magick on my Debian server which I’m using for my public test environment. So, that cool thing about using these steps installing everything from source allows you to duplicate on other *NIX systems.

OS X Printing Black & White Only HP Photosmart 7200 Series

HP Photoshop 7200 Series

I have a HP Photosmart 7200 Series Ink Jet printer. It’s convenient that OS X has what seems to be every printer driver under the sun installed as part of its system so you can plug in almost every printer and have it work (except a stupid all-in-one Dell printer my fiancee has). So more times than not, as long as all of the cords are plugged in, my printer works the way I want it to. That was until today.

The color cartridge in my printer is shot. Hardly any color comes out on the paper at all. Since I’m cheap, I haven’t gone out to the store to purchase a replacement yet. I needed to print a gift certificate from a web site for dinner tonight, but it wouldn’t print properly since the color cartridge was trying to print the parts of the document I really needed (a certificate # to be specific).

So, I did some Googling to find out how to just print in black & white. You’d think this would be simple since OS X is so easy to use, right? Not quite. It turns out, to print in black & white you have to do the following:

  • Save the page as PDF
  • Open in Preview
  • CMD+Print to open the Print Dialog
  • Click Paper Type/Quality
  • Click Color Options
  • Select the Black Printer Cartridge Only radio button

Now, I understand this might be a little out of the norm, but I think it should be way easier to do this from the browser than going through all these steps. Maybe I missed something along the way, but this was the only way I could get black only printing to work. Hopefully this is helpful for someone else out there who’s having the same issue.

OS X Leopard 10.5.2 Is Available

OS X Leopard 10.5.2

I just checked Software Update on my MacBook Pro and noticed that Leopard 10.5.2 is available (there’s also a post here on Digg). That made sense since it appeared the entire world was after it since it took about 5 minutes for Software Update to do its thing and check for all the available updates.

The download comes in at a heafty 180MB. Not quite as big as I had heard in the various article I’d read about the developer seed downloads. Lets hope it fixes some of the lingering issues from the initial update. My biggest issue is that the Synergy screen sharing software doesn’t work (maybe some network issue) and with the desktop updating and some quirks with spaces. Anyway, my update is downloading as I write this and I’m anxious to have it installed to see what’s in here.

Escalate Retail’s Ecometry Support For Endicia (USPS)

Endicia

Escalate Retail

We recently wanted to get a better handle on shipping packages for YumDrop via USPS. Ecometry has support for USPS, but only using what they call an Indicia Permit stamp. The problem with this is the minimums the USPS requires you to meet in order to use this. You have to ship at least 50lbs or 200 pieces to even qualify to ship this way. You also have to go through a lengthy verification process and drop the packages off yourself. Not ideal.

So some reps from USPS came down to visit and suggested we use Endicia to ship via USPS. Its a low cost, easy to use solution. It’s a monthly subscription program starting at $9.95 and topping out at $34.95 per month. There are no meters to rent and it can run on any computer. All of the software is included with the monthly fee.

Cool! Next step, Ecometry integration. Well, no surprise, there isn’t one from Ecometry directly. So I called our USPS rep and talked to him about solutions. He said he’d call Endicia and see what we could do. Turns out Endicia has an import/export product called Endicia Galaxy that can pull data into Endicia and push it back out to Ecometry. Even better, you can use a bar code scanner to basically scan the barcode on a collate and it will auto-populate the shipping address fields in the Endicia DAZzle postage printing software.

It all basically works through and ODBC connection that you set up as part of the Endicia Galaxy installation program. Galaxy can pull the order and shipment information straight from Ecometry when you scan the package so that it can print the label automatically for you. Really cool!

The one gotcha here is that Galaxy can’t read from multiple tables, which is how Ecometry stores its data. What you can do is set up a SQL View to pull the data from. I found this document in the Ecometry Google Group thread on how to set this stuff up. It’s from Scott Lohmann of republicoftea.com, so I want to give him credit there. You can basically follow this to the T, however…

I did encounter one problem. Scott’s example view SQL code only pulls information from the CUSTOMER’s table. The issue here is if your order has a different ship-to address than bill-to address, then the wrong address gets printed on the label. Laura Smith from swell.com pointed me in the right direction. After you ship confirm a package, it will show up in the MANIFESTENTRIES table. In here will be all of the information regarding the package and where its going. So you can pull the information right from there! You can download a copy of the SQL code for this view here.

Now I’m left with a couple of issues to sort out. First, I want to post the actual shipping cost and any tracking information back to Ecometry. I think you can do this through a SQL view as well, I just haven’t figured it out. Also, it looks like ESM keeps a connection to the shipping scale open, so Galaxy can’t pull the package weight in directly. I have to enter it manually. And lastly, when you scan the package barcode, you have to remove the last digit. Its not part of the order number, but some flag Ecometry puts on the end there. A little annoying, but workable.

All in all, I’d say this works great. Way better than doing it by hand through usps.com or printing postage from a postage meter. You can just generate the labels as you ship confirm packages.

Setting Up Ecometry Shipping Methods

Setting up Ecometry shipping methods is something that trips me up every time I have to do it. Honestly, I think the problem is that there are just too many steps that aren’t clear enough. If you miss one thing, you’re hosed. Too bad Ecometry can’t (or isn’t able to) fix this obvious usability issue for whatever reason. They have more documentation than they used to via their support portal, but it still isn’t as good as it could be (it looks like a glorified README and not indexed by Google, thus not available on the world wide web, where information belongs).

So I’m going to do the best thing I can, and go over exactly how to do it here. This way I will have it documented and anyone else out there in Ecometry land can use it for reference. For the purpose of this example, I’m setting up a miscellaneous (MX) ship method. These same steps can really be followed for any other ship method group, i.e. UPS (UP), FedEx (FX), USPS (PS), etc. Anyway, here goes…

You’ll probably want to just do all of your data entry for the shipping stuff in VisualLink. Gather all of the company/division pairs that you want to set up. Remember, you’ll want to include company 00 and division 00 in your setup as well, just for defaults. Ecometry suggests this in case “something goes wrong” and a ship method cannot be found for your company/division pair. So, if I’m setting up a new MX ship method, say 07, we’d have the following for a company/division 03/31:

Company Division Ship Method
00 00 07
03 00 07
03 31 07

With this data in visual link, activate Warehouse Management under Warehousing and Shipping. Go into Ship Methods Entry and enter the information for the company/division/ship method combinations like you see in the table above. Once you’re done, we can move on to the next step.

As a side note, in our setup, I used ship method 07 for USPS Express Mail so we could integrate Endicia with one of our companies (more on that in a future post).

Now you can go into Shipper Data Entry. This is the tricky part. You need to set up the shipper data for each company/division/warehouse/ship method group (you’ll also have to include company division 00/00 as fall back defaults). If you already have miscellaneous ship methods set up, this part is probably done already, as it was for me. If not, you need to provide any shipping account, manifest, or server information required to set up the shipping method. So it would be best to have that all in front of you before you begin.

One other point of note is that even if you don’t use warehouse 01, you need to set it up for each ship method. We’re using warehouse 03 in our example here. So we’d have to set up warehouse 01 and warehouse 03.

Anyway, if I were to have to set this up, I would have used the following table of values to set up my shipper data:

Company Division Warehouse Ship Method Group
00 00 01 MX
03 00 01 MX
03 31 01 MX
00 00 03 MX
03 00 03 MX
03 31 03 MX

For a miscellaneous ship method group, you can just enter 1 for the Carrier ID and Manifest Numbers. For FedEx, you’d want to enter all of your FedEx account and server information (sometimes you have to enter P in the Carrier ID field to get the right fields to come up.). I’m sure UPS and other carriers are similar to FedEx.

Now, assuming you have all of that set up right, its time to set up Postage and Handling routines. Get our of Warehouse and Shipping and go back to the VisualLink start screen. Under Advertising and Sales, you want to get into Advertising Control. Once there, get into P & H Management. I’ve always wondered why this stuff is here in this module, but I think it has to do with the fact that P&H routines are tied to advertising offer codes.

You’re in P&H Management, so type in your company name. Its time to set up P&H routines, set up shipping zones, and zone charges.

At the P&H Management screen, type in your company then hit enter. Then active P&H Routines at the bottom of the screen.

P&H Routine Ship Method
00 07
07 07

where 07 is actually the P&H routine I already had set up for my company, but I needed to add my new ship method, 07. Save your changes and go back to the main P&H Management screen.

Now you need to set up Sec Ct. Zone values. Click on that at the bottom of the screen. For this ship method, 07, I need to set up values of 001 Z9Z 01, where the last 01 is the zone. Basically every location will be in zone 01 for this ship method. It took a while for these values to save in Visual Link, so be patient if you think that the program has hung up on you.

Now that you’re back at P&H Management again, you need to set up the Zone Charges for the ship method. Even if you don’t use weight zone charges, they need set up for the ship method to work. In my case, I had to set up 4 sets:

P&H Routine Ship Method Zone Page
00 07 01 00
00 07 01 01
07 07 01 00
07 07 01 01

Basically the table above means I’m setting up the weight zone charges for Page 0 & 1 for Zone 1 for my ship method 07 and P&H routines 00 and 07. Sound confusing? It is.

In my case, I just entered 0 for the first few values of each set because I wasn’t using weight zone charges. If you are, you’ll want to put actual charges in for those values.

That’s about it! Test out your ship methods. If you get errors, go back and check your work. If you get “Invalid Ship Method”, you probably forgot to set up a ship method properly or the proper P&H routine for that ship method.

Any questions or comments, fee free to do so. If you think I have something wrong here, let me know so I can fix it!