Sha256: 14a54ee8cbbdb97461efb0f55b261047dc6896e00f3ee1872c06c87ef3663e77

Contents?: true

Size: 826 Bytes

Versions: 1

Compression:

Stored size: 826 Bytes

Contents

module Urlize
	require 'uri'
	require 'cgi'

	def self.read(url)
		parsed_uri = URI.parse(url)
		host = parsed_uri.host.index("www.") ? parsed_uri.host.gsub(/www\./, "") : parsed_uri.host
		query = CGI.parse(parsed_uri.query)

		output = {
			:scheme => parsed_uri.scheme,
			:host => host,
		}
		output[:search_term] = self.humanize(query["q"]) if query.has_key?("q")
		output[:search_term] = self.humanize(query["p"]) if query.has_key?("p")
		output
	end

	def self.domain(url)
		@parsed = self.read(url)
		output = @parsed[:host]
		output << " (#{@parsed[:search_term]})" if @parsed[:search_term]
		output
	end

	protected
		def self.humanize(phrase)
			if phrase.index("%20")
				phrase = phrase.gsub(/%20/, " ")
				phrase = phrase.gsub(/\+/, " ")
			else
			end
			phrase[0]
		end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
Urlize-0.1.3 lib/urlize.rb