Class: Gistgen::URL

Inherits:
Object
  • Object
show all
Defined in:
lib/gistgen/url.rb

Class Method Summary collapse

Class Method Details

.is_image?(url) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/gistgen/url.rb', line 19

def self.is_image?(url)
  url.match(/\.(?:jpg|jpeg|png|gif|tiff|raw|bmp|webp|ai|psd|svg)$/i)
end

.is_multimedia?(url) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/gistgen/url.rb', line 23

def self.is_multimedia?(url)
  url.match(/\.(?:js|css|mp3|swf|wmv|mov|doc|pdf|ppt|xls|xlsx|docx|eps|ps|ttf|xml)$/i)
end

.is_root?(url) ⇒ Boolean

return true if it’s a base url (nothing after first ‘/’) and does not have subdomain

Returns:

  • (Boolean)


28
29
30
# File 'lib/gistgen/url.rb', line 28

def self.is_root?(url)
  url.match(/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?\/$/ix) #and url.split('.').size == 2
end

.is_valid?(url) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/gistgen/url.rb', line 15

def self.is_valid?(url)
  url.match(/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}((:[0-9]{1,5})?\/.*)?$/ix)
end

.standardize(url) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/gistgen/url.rb', line 3

def self.standardize(url)
  protocol = url.split('.')[0].match(/^(.*):\/\//)
  u1 = (!protocol)? "http://#{url}" : url
  #raise error if protocol && protocol[0] != 'http'

  #remove www subdomain if exist
  u2 = u1.gsub(/^(http|https):\/\/www\./ix,'http://')

  #make sure google.com and google.com/ are the same thing
  u3 = (u2.match(/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?$/ix))? "#{u2}/" : u2
end