Class: Loob::UriValidator
Constant Summary collapse
- DEFAULT_HTTP_OK_CODES =
possible error statuses: :invalid_scheme :invalid_host :invalid_content_type :invalid_format :moved_permanently :not_accessible
[ Net::HTTPMovedPermanently, Net::HTTPOK, Net::HTTPCreated, Net::HTTPAccepted, Net::HTTPNonAuthoritativeInformation, Net::HTTPPartialContent, Net::HTTPFound, Net::HTTPTemporaryRedirect, Net::HTTPSeeOther ].freeze
Instance Attribute Summary collapse
-
#content_types ⇒ Object
Returns the value of attribute content_types.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#hosts ⇒ Object
Returns the value of attribute hosts.
-
#port ⇒ Object
Returns the value of attribute port.
-
#response_codes ⇒ Object
Returns the value of attribute response_codes.
-
#schemes ⇒ Object
Returns the value of attribute schemes.
Class Method Summary collapse
- .valid_domain?(str, options = {}) ⇒ Boolean
- .valid_uri?(str, options = {}) ⇒ Boolean
- .valid_url? ⇒ Boolean
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ UriValidator
constructor
A new instance of UriValidator.
- #valid_domain?(str) ⇒ Boolean
- #valid_uri?(str) ⇒ Boolean (also: #valid_url?)
Constructor Details
#initialize(options = {}) ⇒ UriValidator
Returns a new instance of UriValidator.
43 44 45 46 47 48 49 |
# File 'lib/loob/uri_validator.rb', line 43 def initialize( = {}) self.schemes = [:schemes] || [] self.hosts = [:hosts] || [] self.content_types = [:content_types] || [] self.response_codes = [:response_codes] || DEFAULT_HTTP_OK_CODES self.port = [:port] || 80 end |
Instance Attribute Details
#content_types ⇒ Object
Returns the value of attribute content_types.
27 28 29 |
# File 'lib/loob/uri_validator.rb', line 27 def content_types @content_types end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
28 29 30 |
# File 'lib/loob/uri_validator.rb', line 28 def error @error end |
#hosts ⇒ Object
Returns the value of attribute hosts.
27 28 29 |
# File 'lib/loob/uri_validator.rb', line 27 def hosts @hosts end |
#port ⇒ Object
Returns the value of attribute port.
27 28 29 |
# File 'lib/loob/uri_validator.rb', line 27 def port @port end |
#response_codes ⇒ Object
Returns the value of attribute response_codes.
27 28 29 |
# File 'lib/loob/uri_validator.rb', line 27 def response_codes @response_codes end |
#schemes ⇒ Object
Returns the value of attribute schemes.
27 28 29 |
# File 'lib/loob/uri_validator.rb', line 27 def schemes @schemes end |
Class Method Details
.valid_domain?(str, options = {}) ⇒ Boolean
37 38 39 |
# File 'lib/loob/uri_validator.rb', line 37 def valid_domain?(str, = {}) self.new().valid_domain?(str) end |
.valid_uri?(str, options = {}) ⇒ Boolean
32 33 34 |
# File 'lib/loob/uri_validator.rb', line 32 def valid_uri?(str, = {}) self.new().valid_uri?(str) end |
.valid_url? ⇒ Boolean
35 36 37 |
# File 'lib/loob/uri_validator.rb', line 35 def valid_uri?(str, = {}) self.new().valid_uri?(str) end |
Instance Method Details
#valid_domain?(str) ⇒ Boolean
100 101 102 103 104 105 106 107 |
# File 'lib/loob/uri_validator.rb', line 100 def valid_domain?(str) begin uri = URI.parse(str) return valid_uri?("#{uri.scheme || 'http'}://#{uri.host || str}/") rescue URI::InvalidURIError, URI::NameError return set_error(:invalid_format) end end |
#valid_uri?(str) ⇒ Boolean Also known as: valid_url?
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/loob/uri_validator.rb', line 55 def valid_uri?(str) reset_error! begin moved_retry ||= false not_allowed_retry ||= false uri = URI.parse(str) uri.path = '/' if uri.path.length < 1 return set_error(:invalid_scheme) unless validate_scheme(uri) return set_error(:invalid_host) unless validate_host(uri) http = Net::HTTP.new(uri.host, (uri.scheme == 'https') ? 443 : port) if uri.scheme == 'https' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end response = not_allowed_retry ? http.request_get(uri.path) {|r|} : http.request_head(uri.path) raise unless validate_response_code(response) return set_error(:invalid_content_type) unless validate_content_type(response) rescue URI::InvalidURIError return set_error(:invalid_format) rescue if response.is_a?(Net::HTTPMovedPermanently) unless moved_retry moved_retry = true str += '/' # In case webserver is just adding a / retry else return set_error(:moved_permanently) end elsif response.is_a?(Net::HTTPMethodNotAllowed) unless not_allowed_retry # Retry with a GET not_allowed_retry = true retry else return set_error(:not_accessible) end else return set_error(:not_accessible) end end return true end |