Source

atlassian-jruby-example-plugin / src / main / ruby / rurl.rb

Full commit
require 'rubygems'
require 'open-uri'
require 'hpricot'
require 'java'

class RurlResponse
  include Java::com.atlassian.plugins.polyglot.jrubyexample.servlet.HttpResponseInformation
  attr_accessor :http_headers, :body_lines, :title, :headlines
  def initialize(http_headers, body_lines, title, headlines)
    @http_headers, @body_lines, @title, @headlines = http_headers, body_lines, title, headlines
  end
end

class Rurl
  def curl(url)
    open(url) do |f|
      # Raw body as array
      body = f.each_line.to_a

      # Extract a few elements
      doc = Hpricot(body.join)
      headlines = (doc/:h1).concat((doc/:h2)).map {|e| e.innerText }.compact.map {|text| text.strip }
      title = (doc/'//head/title').text

      RurlResponse.new(f.meta, body, title, headlines)
    end
  end
end