Class: StubRequests::URI::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/stub_requests/uri/validator.rb

Overview

Validator provides functionality for validating a URI

Since:

  • 0.1.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Validator

Initialize a new instance of StubRequests::URI::Validator

Parameters:

  • uri (String)

    the full URI

Raises:

Since:

  • 0.1.0



50
51
52
53
54
55
56
# File 'lib/stub_requests/uri/validator.rb', line 50

def initialize(uri)
  @uri    = ::URI.parse(uri)
  @host   = @uri.host
  @scheme = @uri.scheme
rescue ::URI::InvalidURIError
  raise InvalidUri, uri
end

Instance Attribute Details

#hostObject (readonly)

Since:

  • 0.1.0



36
37
38
# File 'lib/stub_requests/uri/validator.rb', line 36

def host
  @host
end

#schemeObject (readonly)

Since:

  • 0.1.0



40
41
42
# File 'lib/stub_requests/uri/validator.rb', line 40

def scheme
  @scheme
end

#uriObject (readonly)

Since:

  • 0.1.0



32
33
34
# File 'lib/stub_requests/uri/validator.rb', line 32

def uri
  @uri
end

Class Method Details

.valid?(uri) ⇒ true, false

Validates a URI

Parameters:

  • uri (String)

    a full uri with path

Returns:

  • (true, false)

Since:

  • 0.1.0



25
26
27
# File 'lib/stub_requests/uri/validator.rb', line 25

def self.valid?(uri)
  new(uri).valid?
end

Instance Method Details

#valid?true, false

Checks if a URI is valid

Returns:

  • (true, false)

Since:

  • 0.1.0



64
65
66
# File 'lib/stub_requests/uri/validator.rb', line 64

def valid?
  URI::Scheme.valid?(scheme) && URI::Suffix.valid?(host)
end