Module: MobME::Infrastructure::Utilities::CoreExtensions::URL

Included in:
String
Defined in:
lib/mobme_support/core_ext/string/url.rb

Overview

String extension, which allows URL validation

Instance Method Summary collapse

Instance Method Details

#urlString?

Validates and converts a URL to standard format (if applicable)

Examples:

Validate a URL without scheme

"google.com".url
"http://google.com"

Validate a URL with server IP

"https://123.234.123.234/path/to/item".url
"https://123.234.123.234/path/to/item"

Validate a file:// URL

"file:///absolute/path/to/file".url
"file:///absolute/path/to/file"

Validate a scp:// URL

"scp://user@server:/path/to/resource".url
"scp://user@server:/path/to/resource"


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mobme_support/core_ext/string/url.rb', line 22

def url
  possible_url = self.strip
  reg = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
  first_match = reg.match(possible_url) ? possible_url : nil
  unless first_match
    reg = /^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
    second_match = reg.match(possible_url) ? "http://" + possible_url : nil
    unless second_match
      reg = /^(http|https):\/\/[12]?[0-9]?[0-9]\.[12]?[0-9]?[0-9]\.[12]?[0-9]?[0-9]\.[12]?[0-9]?[0-9](\/.*)?$/ix
      third_match = reg.match(possible_url) ? possible_url : nil
      unless third_match
        reg = /^[12]?[0-9]?[0-9]\.[12]?[0-9]?[0-9]\.[12]?[0-9]?[0-9]\.[12]?[0-9]?[0-9](\/.*)?$/ix
        fourth_match = reg.match(possible_url) ? "http://" + possible_url : nil
        unless fourth_match
          reg = /^file:\/\/(\/[a-z0-9_-]+)*\/([a-z0-9_-]+)+(\.([a-z0-9]+)+)?$/ix
          fifth_match = reg.match(possible_url) ? possible_url : nil
          unless fifth_match
            reg = /^scp:\/\/[a-z0-9_-]+@[a-z0-9_-]+:(\/[a-z0-9_-]+)*\/([a-z0-9_-]+)+(\.([a-z0-9]+)+)?$/ix
            sixth_match = reg.match(possible_url) ? possible_url : nil
            unless sixth_match
              reg = /^scp:\/\/[a-z0-9_-]+@[12]?[0-9]?[0-9]\.[12]?[0-9]?[0-9]\.[12]?[0-9]?[0-9]\.[12]?[0-9]?[0-9]:(\/[a-z0-9_-]+)*\/([a-z0-9_-]+)+(\.([a-z0-9]+)+)?$/ix
              seventh_match = reg.match(possible_url) ? possible_url : nil
              unless seventh_match
                reg = /^scp:\/\/[a-z0-9_-]+@[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}:(\/[a-z0-9_-]+)*\/([a-z0-9_-]+)+(\.([a-z0-9]+)+)?$/ix
                reg.match(possible_url) ? possible_url : nil
              else
                seventh_match
              end
            else
              sixth_match
            end
          else
            fifth_match
          end
        else
          fourth_match
        end
      else
        third_match
      end
    else
      second_match
    end
  else
    first_match
  end
end

#url?Boolean

Validates a URL

Examples:

URL without scheme

"google.com".url?
true

URL with invalid scheme

"foobar://123.234.123.234/path/to/item".url
false

file:// path with invalid characters

"file:///p@th/w!th/!nval!d/ch@r@cters".url
false


82
83
84
# File 'lib/mobme_support/core_ext/string/url.rb', line 82

def url?
  url ? true : false
end