Class: NDD::UrlChecker::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/ndd/url_checker/status.rb

Overview

The result of a URI test.

Author:

  • David DIDIER

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Status

Creates a new instance in the :unknown state.

Parameters:

  • uri (String, URI::HTTP)

    the requested URI.


17
18
19
20
# File 'lib/ndd/url_checker/status.rb', line 17

def initialize(uri)
  @uris = [uri.to_s]
  @code = :unknown
end

Instance Attribute Details

#codeString (readonly)

the state

Returns:

  • (String)

    the current value of code


9
10
11
# File 'lib/ndd/url_checker/status.rb', line 9

def code
  @code
end

#errorString, StandardError (readonly)

the last error if any

Returns:

  • (String, StandardError)

    the current value of error


9
10
11
# File 'lib/ndd/url_checker/status.rb', line 9

def error
  @error
end

#urisArray<String> (readonly)

the list of requested URI

Returns:

  • (Array<String>)

    the current value of uris


9
10
11
# File 'lib/ndd/url_checker/status.rb', line 9

def uris
  @uris
end

Instance Method Details

#directself

When the URI is valid without any redirect.

Returns:

  • (self)

47
48
49
# File 'lib/ndd/url_checker/status.rb', line 47

def direct
  update_code(:direct, %i(unknown))
end

#failed(error) ⇒ self

When a generic error is raised.

Parameters:

  • error (StandardError)

    the generic error.

Returns:

  • (self)

54
55
56
57
# File 'lib/ndd/url_checker/status.rb', line 54

def failed(error)
  @error = error
  update_code(:failed, %i(unknown redirected))
end

#invalid?Boolean

Note that the :unknown code is neither valid nor invalid.

Returns:

  • (Boolean)

    true if invalid, false otherwise.


35
36
37
# File 'lib/ndd/url_checker/status.rb', line 35

def invalid?
  INVALID_CODES.include? @code
end

#redirected(uri) ⇒ self

Adds a new URI to the redirected URI list.

Parameters:

  • uri (String|URI::HTTP)

    the redirection URI.

Returns:

  • (self)

62
63
64
65
# File 'lib/ndd/url_checker/status.rb', line 62

def redirected(uri)
  @uris << uri.to_s
  update_code(:redirected, %i(unknown redirected))
end

#redirected?Boolean

Returns true if redirected, false otherwise.

Returns:

  • (Boolean)

    true if redirected, false otherwise.


40
41
42
# File 'lib/ndd/url_checker/status.rb', line 40

def redirected?
  @code == :redirected
end

#to_sObject

Returns the status representation


80
81
82
# File 'lib/ndd/url_checker/status.rb', line 80

def to_s
  self.inspect
end

#too_many_redirectsself

When there are too many redirects.

Returns:

  • (self)

69
70
71
# File 'lib/ndd/url_checker/status.rb', line 69

def too_many_redirects
  update_code(:too_many_redirects, %i(unknown redirected))
end

#unknown_hostself

When the host cannot be resolved.

Returns:

  • (self)

75
76
77
# File 'lib/ndd/url_checker/status.rb', line 75

def unknown_host
  update_code(:unknown_host, %i(unknown redirected))
end

#uriString

Returns the first requested URI.

Returns:

  • (String)

    the first requested URI.


23
24
25
# File 'lib/ndd/url_checker/status.rb', line 23

def uri
  uris.first
end

#valid?Boolean

Note that the :unknown code is neither valid nor invalid.

Returns:

  • (Boolean)

    true if valid, false otherwise.


29
30
31
# File 'lib/ndd/url_checker/status.rb', line 29

def valid?
  VALID_CODES.include? @code
end