require 'mechanize'

module Gh
  module Trending
    module Parsers
      # Abstract parser of the trending page.
      class Parser
        attr_reader :agent

        def initialize(agent = nil)
          @agent = agent || default_agent
        end

        def parse
          raise NotImplementedError
        end

        protected

        # Prepends the github's website URL for a given parsed url.
        # url can be a repository url or a developer url.
        #
        # url has the format: /some/url, because that's the format returned to
        # us by the crawler
        def build_github_url(url)
          "https://github.com#{url}"
        end

        def build_request_url_with(parser_args)
          return resource_url if parser_args.no_args?

          url = resource_url
          url += "/#{parser_args.language}" if parser_args.language
          url += "?since=#{parser_args.time_period}" if parser_args.time_period
          url
        end

        def resource_url
          raise NotImplementedError
        end

        private

        def default_agent
          Mechanize.new
        end
      end
    end
  end
end
