Class: URI::Generic

Inherits:
Object
  • Object
show all
Defined in:
lib/extensions/uri.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.domain_for(url) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/extensions/uri.rb', line 28

def self.domain_for(url)
  uri = URI.parse(url)
  uri.host
rescue => e
  m = url.match(/http:\/\/([^\/]+)/)
  m ? m[1] : nil
end

.friendly_domain_for(url) ⇒ Object



36
37
38
# File 'lib/extensions/uri.rb', line 36

def self.friendly_domain_for(url)
  domain_for(url).to_s.gsub(/^www\./, '')
end

Instance Method Details

#domain_keyObject

Get a friendly name for media_entity. www.flickr.com => flickr flickr.com => flickr iknow.co.jp => iknow www.iknow.co.jp => iknow monkey.iknow.co.jp => iknow news.bbc.co.uk => bbc



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/extensions/uri.rb', line 13

def domain_key
  name = nil
  host = @host
  if host
    host = host.split('.')
    if (host[host.size-2].to_s+host[host.size-1].to_s).size > 4
      offset = (host.size - 2)
    else
      offset = (host.size - 3)
    end
    name = (offset < 0) ? host.first : host[offset]
  end
  name ? name.underscore : nil
end