2013-02-05 14:16:51 -05:00
|
|
|
require 'open-uri'
|
|
|
|
|
|
|
|
module Oneboxer
|
|
|
|
|
|
|
|
class BaseOnebox
|
|
|
|
|
|
|
|
class << self
|
|
|
|
attr_accessor :regexp
|
|
|
|
attr_accessor :favicon_file
|
|
|
|
|
2013-02-06 00:22:11 -05:00
|
|
|
def matcher(regexp=nil,&blk)
|
|
|
|
self.regexp = regexp || blk
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def favicon(favicon_file)
|
|
|
|
self.favicon_file = "favicons/#{favicon_file}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_whitespace(s)
|
|
|
|
s.gsub /\n/, ''
|
|
|
|
end
|
|
|
|
|
|
|
|
def image_html(url, title, page_url)
|
|
|
|
"<a href='#{page_url}' target='_blank'><img src='#{url}' alt='#{title}'></a>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def replace_tags_with_spaces(s)
|
|
|
|
s.gsub /<[^>]+>/, ' '
|
|
|
|
end
|
|
|
|
|
|
|
|
def uriencode(val)
|
2013-09-30 13:09:57 -04:00
|
|
|
URI.escape(val, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-10-09 14:43:59 -04:00
|
|
|
# Replace any occurence of a HTTP or HTTPS URL in the string with the protocol-agnostic variant
|
|
|
|
def replace_agnostic(var)
|
|
|
|
var.gsub! /https?:\/\//, '//' if var.is_a? String
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(url, opts={})
|
2013-02-25 11:42:20 -05:00
|
|
|
@url = url
|
2013-02-05 14:16:51 -05:00
|
|
|
@opts = opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def translate_url
|
|
|
|
@url
|
|
|
|
end
|
|
|
|
|
2013-03-01 18:46:55 -05:00
|
|
|
def nice_host
|
|
|
|
host = URI.parse(@url).host
|
|
|
|
host.nil? ? '' : host.gsub('www.', '')
|
|
|
|
rescue URI::InvalidURIError
|
|
|
|
'' # In case there is a problem with the URL, we just won't set the host
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|