module Kani
  # Page encapsulates the idea of a web page and extracts URLs
  class Page
    def initialize(body)
      @body = body
    end

    def urls
      # Parse the URLs out of the anchor tag href attributes with a regular expression
      #
      # REXML (in the standard library) will error on badly formed XML, so
      # likely won't be flexible enough for HTML from the internets.
      #
      # I also didn't want to bring nokogiri in as a dependecy as this library
      # is designed for use on legacy projects and is therefore dependency free
      # in order to aid interoperability. To clarify: bringing in nokogiri only
      # at versions compatible with Ruby 1.8.7 would (based on my
      # understanding) cause an unresolvable dependency conflict with any Ruby
      # projects that happen to use an up to date version of nokogiri. Since
      # Nokogiri is a popular library this situation is likely to happen often.
      #
      # So those are my reasons and I'm sticking to them. Forgive me for what I
      # am about to do.
      @body.scan(/<a.*href\s*=\s*["'](.*)['"].*>/).flatten
    end
  end
end
