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.
