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:

// replace #ticker_url with whatever your input element is called
$("#ticker_url").keyup(function() {
        type: "POST",
        url: "/url_lookup/",
        data: {url: $('#ticker_url').val()},
        dataType: 'json',
        success: function(data) {

I then set up my config/routes.rb file to send POST requests to /url_lookup/:

match '/url_lookup/' => 'application#url_lookup', :as => :url_lookup, :via => :post
def url_lookup
    page =[:url])
    respond_to do |format|
      format.json { render :json => [
            title: page.title,
            description: page.description
        ] }

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


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.

This Post Has 2 Comments

  1. How did you set up your config/routes.rb file to send POST requests to /url_lookup/ to that controller method?

    1. Hey Matt, thanks for asking. Just updated the post with the line from my routes file:match '/url_lookup/' => 'application#url_lookup', :as => :url_lookup, :via => :post

Leave a Reply

Close Menu