You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
768 B

require 'sinatra'
require 'mechanize'
# Find a description from <meta> tags
def meta_description(page)
description_meta_tag =
page.search("meta[property='og:description']")[0] ||
page.search("meta[property='twitter:description']")[0] ||
page.search("meta[name='description']")[0]
description_meta_tag&.attribute("content")&.value
end
# Truncate the text of the first <article>
def article_description(page)
article = page.search("article")[0]
article.text[0..500] if article
end
get '/summary' do
url = params[:url]
mech = Mechanize.new
page = mech.get(url)
title = page.title
description = meta_description(page) || article_description(page)
content_type :json
{url: url, title: title, description: description}.to_json
end