A cute little trick with Ruby hashes and .try()

Recently I encountered a situation where incoming JSON was converted to a hash, and certain keys were not always present. Naturally I want to safely grab the values that were present without barfing on the nils.

    hsh = {foo: bar: [2]}
    bad_hsh = {foo: nil}
    should_be_2 = hsh[:foo][:bar].first # works
    uhoh = fail[:foo][:bar].first # NilClass error

At first began looking for a way to use .try() to get at nested hash values (since we’re not on Ruby 2.3 yet and can’t use the lonely operator). But the normal try syntax didn’t work:

    uhoh = fail[:foo].try([:bar]).try(:first) # does not work

But then I remembered that [] is actually the name of the method, and what’s in it is the first parameter, so I tried this:

    win = fail[:foo].try([], :bar).try(:first) # It works!

This is not fantastic code, I know there are better ways to do this, and I can’t wait to upgrade to Ruby 2.3, but I do find this interesting.

WordPress Multisite Tips

You can’t start a multi-site install from scratch, you have to install the base site first, then turn on multi-site. If you turn it on right out of the box, you’ll get stuck with a database error and probably spend an hour messing with MySQL permissions before you figure it out. Don’t ask how I know 😉

Also, for running WordPress locally, it took a while to get auto-update working (it’s easier to use than than update manually on the local machine).

To do that I had to enable FTP on this machine, which I kind of hate to do because it’s a security hole. But to do that, execute this command:

sudo launchctl load -w /System/Library/LaunchDaemons/ftp.plist

When wordpress asks for FTP info, you can use ‘localhost’ as the hostname

permissions updates may have been a factor, but I’m not sure, but mostly it just worked

The big revelation is the steps for multi-site setup. The wordpress docs are weak in this area. I really wish they would create two different documents, one for subdomain, one for subfolder, because it’s a lot of work to mentally filter out one or the other as you’re reading the document, and it’s easy to miss details.

Of particular note is that first, you turn on wp_allow_multisite, then  the Network settings item appears in your admin, then you enable the network, THEN you add the code it recommentds, includeing the MULTISITE=true item

More than I ever wanted to know about cookies

Here is a list of things I learned working on a project a while back, I wrote them down because they’re so screwy I know I’d force myself to forget them:

  • Cookie size causes both the browser and the server to barf. Total size, not just individual cookie.
  • Deleting a cookie is tricky, sometimes IE won’t delete session cookie without a restart.
  • When deleting, use ‘0’ as expiration, the full “Thu, 01 Jan 1970 00:00:00 GMT” fails in IE, it sets it to 2070 for some reason.
  • If you have two cookies, one with a subdomain, one without, the one without will “win”.
  • it can be hard to mess with a higher scoped cookie from a subdomain. Avoid it if you can
  • And here’s the kicker: Sometimes the buffer space on your load balancer can be too small for the total size of cookies that your browser is sending, even if Apache can handle larger cookies. In that situation you’ll get an ugly 500 error page. This buffer space may only become an issue if there are rules on the LB that require it to inspect the headers.

Just the other day I saw this article on a related note: Let’s Break The Internet

Image Uploads to AWS S3 using Rails 3, Paperclip and PLUpload

For the Invest Your Heart project, I’ve been working on getting image uploads perfected. That’s easy enough with the Paperclip gem. But storing uploads on your Heroku dyno isn’t a good idea, so I wanted to use S3. Oh and throw in upload previews, resizing and a progress bar.

I managed to get it all working, though it took a lot because there are so many moving parts. I’m using the PLUpload code to provide uploads and progress var interface and Paperclip for resizing. And I’m uploading directly to S3, to avoid timeouts on Heroku when large files are uploaded. After the initial upload, Paperclip actually downloads the image, processes it, and uploads the different resized versions back to S3.

I couldn’t have done it without these two incredibly helpful sites: The Rails-S3-Plupload demo project, and Swarut’s post. Thanks guys! I would never have gotten it working without you.

Swarut also turned me on to the Gon gem which provides a simple way to send data from your controllers and JS variables. Very handy for when your app doesn’t need something like Backbone.js

StartupBus Status Update: Invest Your Heart

I’ve been so busy hacking and hustling that I haven;t had much any time to update here, but after a day and a half, we’ve made great progress.

We have a four person team building Invest Your Heart.

InvestYourHeart is a collaborative social change investment platform for people who believe:

What’s in your heart is just as valuable as what’s in your wallet.
InvestYourHeart creates an opportunity for heartful, conscious, intentional giving to causes you care about.  Each time you invest, you’ll be invited to share what’s in your heart while you’re making the investment by creating a “heartspace”.  These “heartspaces” will be visually displayed on the landing page, and contributors will be able to offer them as virtual gifts to friends and family by designating a contribution in their honor.

We’re all in this together.
Instead of initiating separate, independent fundraising & social action campaigns for each project on the site, instead, we’ll mount themed campaigns in which like-minded nonprofits may participate as affiliates.   By collaborating common campaigns, we can achieve critical mass that attracts more visibility and resources to all participating projects.

Funding is just the beginning.
So often, crowdfunding sites encourage attention to a project ony until it’s been funded.  At InvestYourHeart, we know funding is only the beginning, and we know people who have invested their hearts in your efforts are interested in more than seeing a fundraising goal met.  We provide unique opportunities to interact online with the people & communities that are transformed through our collaborative investments and provide ongoing updates so you can see the love you’ve manifested in the world, not just the money you’ve helped to raise.

As expected, we have a Facebook Page and twitter account set up.

All aboard the StartupBus

I’ve decided to break out of some ruts and join the StartupBus (@startupbusfl), a 3 day moving hackathon on a bus to Austin, TX for South by Southwest Interactive (SxSWi).

I’ll be posting a whole lot more about it as I go along, so I’ll start here with just the list of things that I’m packing to bring along (aside form the obvious tech):

  1. Sketchpad
  2. Dry-erase markers
  3. Post-its
  4. Headphones
  5. My Ideas To Pitch
  6. Luna Bars
  7. MyPressi Portable Espresso Machine
  8. Upgraded Coffee
  9. 100GB of music
  10. Pre-built Rails application skeleton (I’ll tell you about that in another post. soon!)



(photo credit teddy.is)

Mini Upgrade

image by 'smee' at Apple InsiderThanks to this tutorial, I was able to upgrade my Mac Mini media server to 3GB of RAM. That allowed me to upgrade it to Mac OS X Lion as well (it only had 1GB before, and Lion requires 2GB). That’s made quite an improvement in file sharing responsiveness. If you recall, even on Snow Leopard, it could take 30 seconds to connect to a server. Initially, I had tried to use the DIMMs left over form my MacBook Pro upgrade (to 8GB), but they were too advanced. Luckily, I had an old white MacBook in the closet with a bad motherboard which happened to have matching DIMMs, so I was able to complete the upgrade.

Pow and WordPress on the same machine

While developing the InnerLightTools application on my local machine, I’m using the Pow web/rack server. It’s a great way to run rails apps locally. At the same time I’m working on WordPress development locally as well, and for that I need Apache. By default Pow runs on port 80, just like Apache, so I couldn’t do both at the same time. Initially I set Apache to run on a different port, but discovered the WordPress Multisite won’t run on ports other than 80 and 443. So, I found the Pow configuration page contained information about setting Pow to run on an alternate port. It’s pretty simple, just add

export POW_DST_PORT=<any port num>

to your ~/.powconfig file and restart. My Pow is finicky, so I often have to reinstall it to get things to work properly. Fortunatley that’s trivial with the Powder gem.

Lion, Rails 3.1 and WordPress Clients

I haven’t gotten much done on Inner Light Tools on the last few months. My day job had taken over all of my free time, at the same time, I was waiting for Rails 3.1 to finish up. I’d begun converting the code over with one of the early Release Candidates, but when I updated my MacBook to Lion, things broke. At the same time I’d tried renaming the Xcode directory, and then make and gcc stopped working. Even when I moved the folder back they wouldn’t work.

Because I couldn’t compile any native code, the upgrade to rails 3.1 final would’t work. I couldn’t get Xcode to reinstall from the App Store either. So I reinstalled it on another machine, and made a copy of the installer before it was finished (when it gets deleted). With that I was able to reinstall, and everything started working again, thankfully.

Now that I had a final version of Rails. I could get on with the code. Over the weekend I spent some time deleting parts of the app. Originally I’d planned to allow users to build customized intake forms, but dealing with all the complexity that brought was driving me nuts, and taking forever to code. So I’ve cut it out, and it’s not just a simple model.

I may go back to that idea in the future, if my users actually want that kind of thing. I’ll ask them first.

Also, I’m trying out WordPress clients, trying to decide between MarsEdit and MacJournal. I’ll let you know which one wins.