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
-
#url ⇒ String?
Validates and converts a URL to standard format (if applicable).
-
#url? ⇒ Boolean
Validates a URL.
Instance Method Details
#url ⇒ String?
Validates and converts a URL to standard format (if applicable)
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
82 83 84 |
# File 'lib/mobme_support/core_ext/string/url.rb', line 82 def url? url ? true : false end |