Using the MetaInspector gem in Ruby on Rails

I needed an AJAX call to take a user-provided URL from a form, scrape the HTML and retrieve the title and meta description for that URL, and update the corresponding title and description fields on the form–much like Facebook does when you type in a URL and it automatically grabs the information about that page. After searching around, I found the MetaInspector gem, installed it, and included it in my Ruby on Rails Gemfile.

Once installed, it’s pretty easy to make a web request and parse through the result. First, the AJAX piece looks like this:

$("#ticker_url").keyup(function() {
    $.ajax({
        type: "POST",
        url: "/url_lookup/",
        data: {url: $('#ticker_url').val()},
        dataType: 'json',
        success: function(data) {
            $('#ticker_title').val(data[0].title)
            $('#ticker_content').val(data[0].description)
        }
    });
});

I then set up my config/routes.rb file to send POST requests to /url_lookup/ to a controller method that looks like this:

def url_lookup
    page = MetaInspector.new(params[:url])
    respond_to do |format|
      format.json { render :json => [
            title: page.title,
            description: page.description
        ] }
    end
  end

You’ll notice how easy it was to grab the elements from the MetaInspector object:

page.title
page.description

Check out the documentation for more examples on how to access scraped data.

I didn’t grab the image as part of this example (like Facebook does), but a simple page.image will get you what you need.

Comments

comments

Job Happiness

“The problem is that what we think matters most in our jobs often does not align with what will really make us happy. Even worse, we don’t notice that gap until it’s too late.”

-Clayton Christensen, How Will You Measure Your Life?

Comments

comments

Amazon Affiliate Experiment

I’m running an experiment this shopping season–a Twitter account (and potentially a website) dedicated to posting deals I find on Amazon. For lack of a better name, I’m calling it SpencerDeals.

Why am I doing this? If you’re not familiar with the Amazon Affiliate program, it’s a way to refer people to make purchases on Amazon and then get a cut of the profits. I don’t post affiliate links very often, so I haven’t made very much money over the past year. We’re talking $20 max. But this holiday season, I plan to inundate the #BlackFriday hashtag (and others) with affiliate links, in hopes that clicks will lead to conversion will lead to money for Christmas presents.

When I find really good deals, I usually post them to Facebook. But I don’t want to be that guy who just posts annoying deals–which is why I’m creating a separate Twitter account for the purpose. For those who like my style and selections, they can follow SpencerDeals for my curated list of favorite deals. That way I can keep my other feeds clear of junk that not everybody wants.

If this takes off, I’ll probably create a website where I can post these deals, and automate the tweets from there. I’d like to see what the Amazon Associates and WordPress APIs can offer–it would be awesome if I can build a Chrome plugin or something that will automatically post an Amazon product to my blog with one click from the product page. If you know of anything already available, let me know in the comments. The goal is to keep it personally curated, which I think is the big pull for a deal site like this–not every deal on the planet, just the stuff I’m interested in. And hopefully there are enough other people interested in the same things that it’ll take off.

Come join the experiment! Start following SpencerDeals on Twitter!

Comments

comments