Module: AutoHTTP

Included in:
LazyDomain
Defined in:
lib/lazy_domain.rb

Overview

Add http:// if a protocol isn’t already there.

Constant Summary collapse

TP_REGEX =

Looks for any character before ‘://’

%r{[\w]+(\b:\/\/)}
BADURL_REGEX =

Looks if there is are non word characters before domain but no letters. Example is ‘://google.com’ or ‘#$/google.com’, etc.

/\A(\W+)/

Instance Method Summary collapse

Instance Method Details

#autohttp(str) ⇒ Object

Add ‘http://’ if something is not already present.



33
34
35
36
37
# File 'lib/lazy_domain.rb', line 33

def autohttp(str)
  strip_bad(str) if bad_protocol?(str)
  str.prepend('http://') unless hastp?(str)
  str
end

#bad_protocol?(str) ⇒ Boolean

Boolean, returns true if anything besides ‘*://’ is given

Returns:

  • (Boolean)


22
23
24
# File 'lib/lazy_domain.rb', line 22

def bad_protocol?(str)
  !(str =~ BADURL_REGEX).nil?
end

#hastp?(str) ⇒ Boolean

Returns true if there is any character before ‘://” in a url. ie. ’something://google.com’ or ‘xyz://google.com’

Returns false if somehting like ‘://google.com’ or ‘google.com’.

Returns:

  • (Boolean)


17
18
19
# File 'lib/lazy_domain.rb', line 17

def hastp?(str)
  !(str =~ TP_REGEX).nil?
end

#strip_bad(str) ⇒ Object

Strip out the bad characters. If there is nothing bad to strip, returns the original string.



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

def strip_bad(str)
  str.gsub!(BADURL_REGEX, '') || str
end