Class: Kauperts::LinkChecker
- Inherits:
-
Object
- Object
- Kauperts::LinkChecker
- Defined in:
- lib/kauperts_link_checker.rb,
lib/kauperts_link_checker/version.rb
Overview
Checks the status of an object which responds to url
. The returned status can be accessed via status
. It contains either a string representation of a numeric http status code or an error message.
Supports HTTPS and IDN-domains.
The following keys are used to translate error messages using the I18n gem:
-
kauperts.link_checker.errors.timeout
: rescues from Timeout::Error -
kauperts.link_checker.errors.generic_network
: (currently) rescues from all other exceptions -
kauperts.link_checker.status.redirect_permanently
: translation for 301 permanent redirects
Defined Under Namespace
Classes: Configuration
Constant Summary collapse
- VERSION =
'0.5.1'
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
-
.check!(object, options = {}) ⇒ Object
Immediately checks
object
and returns the LinkChecker instance.
Instance Method Summary collapse
-
#check! ⇒ Object
Checks the associated url object.
-
#initialize(object, options = {}) ⇒ LinkChecker
constructor
Parameters *
object
: an arbitrary object which responds tourl
. -
#ok? ⇒ Boolean
Returns if a check has been run and the return code was ‘200 OK’ or if a 301 permanent redirect only added a trailing slash while
ignore_trailing_slash_redirects
has been set to true.
Constructor Details
#initialize(object, options = {}) ⇒ LinkChecker
Parameters
-
object
: an arbitrary object which responds tourl
. -
options
: optional configuration parameters, see below.
Available Options
-
ignore_trailing_slash_redirects
: ignores redirects to the same URI but only with an added trailing slash (default: false)
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/kauperts_link_checker.rb', line 33 def initialize(object, = {}) object.respond_to?(:url) ? @object = object : raise(ArgumentError.new("object doesn't respond to url")) # Assign config variables @configuration = Configuration.new = { :ignore_trailing_slash_redirects => false, :ignore_302_redirects => false }.merge().each do |key, val| @configuration.send(:"#{key}=", val) end end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
20 21 22 |
# File 'lib/kauperts_link_checker.rb', line 20 def configuration @configuration end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
20 21 22 |
# File 'lib/kauperts_link_checker.rb', line 20 def object @object end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
20 21 22 |
# File 'lib/kauperts_link_checker.rb', line 20 def status @status end |
Class Method Details
.check!(object, options = {}) ⇒ Object
Immediately checks object
and returns the LinkChecker instance
82 83 84 85 86 |
# File 'lib/kauperts_link_checker.rb', line 82 def self.check!(object, = {}) checker = new(object, ) checker.check! checker end |
Instance Method Details
#check! ⇒ Object
Checks the associated url object. Sets and returns status
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/kauperts_link_checker.rb', line 45 def check! begin uri = parsed_uri(@object.url) if uri.scheme == 'https' http = Net::HTTP.new(uri.host , 443) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE response = http.start{ http.get2(uri.to_s) } else response = Net::HTTP.get_response(uri) end status = if response.code == '301' @redirect_with_trailing_slash_only = "#{uri}/" == response['location'] "#{I18n.t :"kauperts.link_checker.status.redirect_permanently", :default => "Moved permanently"} (#{response['location']})" else response.code end rescue Timeout::Error => e status = "#{I18n.t :"kauperts.link_checker.errors.timeout", :default => "Timeout"} (#{e.})" rescue Exception => e status = "#{I18n.t :"kauperts.link_checker.errors.generic_network", :default => "Generic network error"} (#{e.})" end @status = status end |
#ok? ⇒ Boolean
Returns if a check has been run and the return code was ‘200 OK’ or if a 301 permanent redirect only added a trailing slash while ignore_trailing_slash_redirects
has been set to true
73 74 75 76 77 78 79 |
# File 'lib/kauperts_link_checker.rb', line 73 def ok? return true if @status == '200' return true if (@status == '302' and self.configuration.ignore_302_redirects) return true if (@redirect_with_trailing_slash_only == true and self.configuration.ignore_trailing_slash_redirects) false end |