module SycLink::LinkChecker

Methods to check links for availability. A link may be an URI or a file

Methods to check links for availability. A link may be an URI or a file

Public Instance Methods

response() click to toggle source

Checks whether a link is reachable. If so code '200' is returned otherwise 'Error' response expects that the including class has an attribute 'url'

# File lib/syclink/link_checker.rb, line 12
def response
  uri = to_uri(url)
  uri ? response_of_uri(uri) : response_of_file(url)
end
response_of_file(file) click to toggle source

Checks whether a file exists. If so it returns '200' otherwise 'error'

# File lib/syclink/link_checker.rb, line 31
def response_of_file(file)
  File.exists?(file) ? "200" : "Error"
end
response_of_uri(uri) click to toggle source

Checks whether the uri is reachable. If so it returns '200' otherwise 'Error'. uri has to have a host, that is uri.host should not be nil

# File lib/syclink/link_checker.rb, line 19
def response_of_uri(uri)
  begin
    Net::HTTP.start(uri.host, uri.port) do |http|
      http.head(uri.path.size > 0 ? uri.path : "/").code
    end
  rescue Exception => e
    "Error"
  end
end
to_uri(url) click to toggle source

Transforms an URL (String) to an URI. If URL is not valid false is returned

# File lib/syclink/link_checker.rb, line 37
def to_uri(url)
  uri = URI.parse(url)
  uri.host.nil? ? false : uri
rescue URI::BadURIError
  false
rescue URI::InvalidURIError
  false
end