class Bookbinder::CssLinkChecker

Public Instance Methods

Private Instance Methods

http_reachable?(link) click to toggle source
# File lib/bookbinder/css_link_checker.rb, line 61
def http_reachable?(link)
  Net::HTTP.get_response(URI(link)).code == '200'
rescue SocketError => e
  return false
end
path_info(link) click to toggle source
# File lib/bookbinder/css_link_checker.rb, line 52
def path_info(link)
  link.sub(/[?#].+\z/, '')
end
strip_location(id) click to toggle source
# File lib/bookbinder/css_link_checker.rb, line 56
def strip_location(id)
  dirs, filepath = id.split('=> ')
  [dirs, filepath]
end
target_exists?(localized_identifier) click to toggle source
# File lib/bookbinder/css_link_checker.rb, line 32
def target_exists?(localized_identifier)
  dirs, link = strip_location(localized_identifier)
  link.gsub!(/'|"/, '')

  data_uri      = /^['"]?data:image\//
  remote_uri    = /^['"]?https?:\/\//
  absolute_uri  = /^['"]?\//
  relative_uri  = //

  case link
    when data_uri then true
    when remote_uri then http_reachable?(link)
    when absolute_uri then File.exists?(File.join('.', 'public', path_info(link)))
    when relative_uri then
      dirs = dirs.split('/')[0...-1]
      computed_uri = File.expand_path(File.join dirs, path_info(link))
      File.exists?(computed_uri)
  end
end