Sha256: 684d27742bca151d3ab3a00a1f2bcfdf2a0657d0a876fec9487b6775fca68f1a
Contents?: true
Size: 1.44 KB
Versions: 1
Compression:
Stored size: 1.44 KB
Contents
require "uri" require "nokogiri" module Lita module Handlers class WebTitle < Handler config :ignore_patterns, types: [String, Array] URI_PROTOCOLS = %w( http https ) route(URI.regexp(URI_PROTOCOLS), :parse_uri_request, help: { "URL" => "Responds with the title of the web page at URL" }) def parse_uri_request(request) requestUri = URI::extract(request.message.body, URI_PROTOCOLS).first if config.ignore_patterns then if config.ignore_patterns.kind_of?(String) then Array(config.ignore_patterns) end config.ignore_patterns.each do |pattern| return if requestUri.match(%r{#{pattern}}) end end result = parse_uri(requestUri) request.reply(result.delete("\n").strip) unless result.nil? end def parse_uri(uriString) httpRequest = http.get(uriString) if httpRequest.status == 200 then return unless httpRequest.headers['Content-Type'] =~ %r{text/x?html} page = Nokogiri::HTML(httpRequest.body) page.css("title").first.text elsif [300, 301, 302, 303].include? httpRequest.status then parse_uri httpRequest.headers["Location"] else nil end rescue Exception => msg log.error("lita-web-title: Exception attempting to load URL: #{msg}") nil end end Lita.register_handler(WebTitle) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
lita-web-title-1.0.6 | lib/lita/handlers/web_title.rb |