url_format 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e6b078f50cf4d767034c1f5e38ee163e4d49dd9a
4
- data.tar.gz: 336ec031f947da651cc04ef7c3ca66c8d3e9f9b1
3
+ metadata.gz: 1d5dfa1ccbb5f619c64cb28afa566501f4211ffe
4
+ data.tar.gz: 91d6d09f0c8c612b824a789b76c6a09bb00f553d
5
5
  SHA512:
6
- metadata.gz: 9b03cd6d1d2b9e252649cd870ab0c70f64f61edc9a320953a9750511832fbb1a4f585d2a1358eecb622ffe37af34b88b1035cb7b3a2566dbee775c5f168dd6e4
7
- data.tar.gz: 7ca4d0efaa33b74096d0e019843c80bc50d29dd1c3bdd6a02a929255c42d2df6ecf3af899b0f503d0a146f00f75d830c458b36b5e61f6374aa4f6145381c3c6f
6
+ metadata.gz: d7548a7be7112d4dd292d9c2e06a25961162f2585d53362c70db57e1de8a0c8783ac4445761de62c035a1fd222aa5afc173591b2f5e468b4b4e0f95af368e8bd
7
+ data.tar.gz: bdb879d6b050ce9e6c6c898e5e321caa0e5a1c99a4bf61a446713f8b6b5006bdadf2aff59bcae7d1c340a027df1b9b3a06c5f707152dd55ba31ea3b2c5669262
data/lib/url_format.rb CHANGED
@@ -3,4 +3,10 @@ require "url_format/url_format_validator"
3
3
  require "uri"
4
4
 
5
5
  module UrlFormat
6
+
7
+ def self.get_domain(url)
8
+ host = URI.parse(url).host.downcase
9
+ host.start_with?('www.') ? host[4..-1] : host
10
+ rescue URI::InvalidURIError
11
+ end
6
12
  end
@@ -1,9 +1,20 @@
1
1
  class UrlFormatValidator < ActiveModel::EachValidator
2
2
  def validate_each(record, attribute, value)
3
- unless URI.parse(value).kind_of?(URI::HTTP) && value =~ /\.\w{2,6}/
3
+ format_value(value)
4
+ unless URI.parse(value).kind_of?(URI::HTTP) && value =~ url_regexp
4
5
  record.errors[attribute] << (options[:message] || "is invalid")
5
6
  end
6
7
  rescue URI::InvalidURIError
7
8
  record.errors[attribute] << (options[:message] || "is invalid")
8
9
  end
10
+
11
+ def format_value(value)
12
+ value = "http://#{value}" unless value =~ /\Ahttps?:\/\//
13
+ end
14
+
15
+ # Thanks to Dean Perry and Ryan Bates
16
+ # https://github.com/deanperry/url_formatter/blob/master/lib/url_formatter.rb
17
+ def url_regexp
18
+ /\Ahttps?:\/\/([\A\s:@]+:[\A\s:@]*@)?[-[[:alnum:]]]+(\.[-[[:alnum:]]]+)+\.?(:\d{1,5})?([\/?]\S*)?\z/iux
19
+ end
9
20
  end
@@ -1,3 +1,3 @@
1
1
  module UrlFormat
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: url_format
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Otander