JavaScript Classes: A primer focusing on encapsulation

Preface on target audience:
This post is intended for someone who is mostly comfortable with JavaScript and is familiar, at a basic level, with object-oriented-programming principles. If you can create functions and arrays in JavaScript, and know what “this” means in the context of classes, then that should be adequate.

For several months, I had been searching for a good guide on writing JavaScript classes; something that started very basically and used lots of examples. What I found were guides on writing plugins in jQuery, doing inheritance with JavaScript, overly technical articles about the underlying framework behind JavaScript’s prototype method (lots of discussion about the prototype property — which was confusing for a hot minute since there is also a framework named Prototype.)

I know they’re out there, but I’ve had a hard time finding them. All I really wanted was a guide on basic encapsulation to make my code cleaner, but so many of the guides focused instead on the Inheritance aspect (which is arguably flashier / sexier).

With the caveat that I would never claim to be a JavaScript expert, and also that I have only just recently figured this out in detail, I’d like to share my findings and process. My hope is that this can be a stepping stone for some others out there like me. I’m open to feedback on this, but want the focus to be on both encapsulation and keeping it basic.

Continue reading

Authenticating against WordPress in Rails 3

So I’m working on this Rails app for Melissa. It’s kind of a secret, but it’s pretty neat. She demoed it at a webinar and it was received well. :)

One problem I’ve encountered is that her main website that manages subscriptions and users is in WordPress. Users will register with that main website and may or may not have access to the Rails application, separately (depending on their subscription level). What I needed was a method to handle User Authentication in Rails that slaves to the subscription / account information held in WordPress. Ideally, this should all keep with the DRY principle to make it nice and Rails-y.

I wanted to avoid using any messy and potentially unsecure session cookie sharing, and I also wanted to avoid doing any database shadowing. I suspected that since all the data is up on databases on the same server, I should be able to just query it out, right?

Right! Continue reading

Scraping Twitpics with PHP [Coding]

Update! My twitpic scraper (as well as search API calls) have been integrated into NCSU’s Tweetgator. Check it out on Github!

A couple months ago, IU East re-vamped its twitter wall. We incorporated a codebase originally developed by NCSU, and then I extended it by adding inline hashtag searching and a twitpic scraper.

At the time I wrote it, I could not find any other existing Twitpic scraper – Twitpic doesn’t have a formal API (or at least, it didn’t then; I don’t think it does at the time of this writing, either).

Effectively what this script does (see after the jump) is to browse the Twitpic site, parse out the image IDs, and then re-create the Twitpic images. It is somewhat rudimentary in that it does not cache nor does is it actually download the images — anyone reading this may feel free to extend the code into something like that. Continue reading

Syndicating your shared Google Reader items

I’m an avid user of Google Reader. I follow a couple dozen different websites, check it daily (and sometimes quasi-daily), and have a very elaborate system of tags.

One thing that I did a year or two ago was to take my public broadcast feed of my shared items and integrate it into my main website directly. The goal was to have the homepage of my website always have fresh content by linking it to my shared Reader items (I share anywhere from 3-8 items per day, on average).

Originally, my solution was a bit of a hack: I used Magpie RSS reader to consume the feeds, then did some serious PHP acrobatics to parse out the Google Reader feed — what I failed to understand, at the time, was the fundamental differences between RSS and Atom feeds (subtle, yet significant). All in all, I think it took somewhere from 100-150 lines of code to effectively snatch the feed and parse it out; and it wasn’t perfect, either — frequentle the title and source would get mashed together. But for that particular time, it was fine.

The other night, though, I decided to try and fix it up proper. I discovered that the Zend Framework has some native Atom Feed classes and thought this would be a good place to start. (Details, including my PHP source code, after the jump)

Continue reading