def search(page_num = 0)
      @error = nil
      @message = nil

      search_proc = lambda do |page|
        begin
          uri = URI(URI.encode(to_s page))
          res = Net::HTTP.get_response(uri)
          if res.code == '301'
            path = Net::HTTP::Get.new(res.header['location'])
            res = Net::HTTP.start(uri.host) { |http| http.request path }
          end

          @pages = 0 and return if res.code == '404'

          doc = Nokogiri::HTML(res.body)

          @results[page] = doc.xpath('//table[@class="data"]//tr[position()>1]/td[1]').map do |node|
            { path:     href_of(node, 'a.torType'),
              title:    node.css('a.cellMainLink').text,
              magnet:   href_of(node, 'a[title="Torrent magnet link"]'),
              download: href_of(node, 'a[title="Download torrent file"]'),
              size:     (node = node.next_element).text,
              files:    (node = node.next_element).text.to_i,
              age:      (node = node.next_element).text,
              seeds:    (node = node.next_element).text.to_i,
              leeches:  node.next_element.text.to_i }
          end

          # If we haven't previously performed a search with this query string, get the
          # number of pages from the pagination bar at the bottom of the results page.
          # If there's no pagination bar there's only 1 page of results.
          if @pages == -1
            p = doc.css('div.pages > a').last
            @pages = p ? [1, p.text.to_i].max : 1
          end
        rescue => e
          @error = { error: e }
        end unless @results[page] || (@pages > -1 && page >= @pages)
      end

      # Make sure we do a query for the first page of results before getting
      # subsequent pages in order to correctly figure out the total number of
      # pages of results.
      pages = (page_num.is_a?(Range) ? page_num.to_a : [page_num])
      pages.unshift(0) if @pages == -1 && !pages.include?(0)
      pages.each { |i| search_proc.call i }

      results[page_num.is_a?(Range) ? page_num.max : page_num]
    end
def search(page_num = 0)
      @error = nil
      @message = nil

      search_proc = lambda do |page|
        begin
          uri = URI(URI.encode(to_s page))
          res = Net::HTTP.get_response(uri)
          if res.code == '301'
            path = Net::HTTP::Get.new(res.header['location'])
            res = Net::HTTP.start(uri.host) { |http| http.request path }
          end

          @pages = 0 and return if res.code == '404'

          doc = Nokogiri::HTML(res.body)

          @results[page] = doc.xpath('//table[@class="data"]//tr[position()>1]/td[1]').map do |node|
            { path:     href_of(node, 'a.torType'),
              title:    node.css('a.cellMainLink').text,
              magnet:   href_of(node, 'a[title="Torrent magnet link"]'),
              download: href_of(node, 'a[title="Download torrent file"]'),
              size:     (node = node.next_element).text,
              files:    (node = node.next_element).text.to_i,
              age:      (node = node.next_element).text,
              seeds:    (node = node.next_element).text.to_i,
              leeches:  node.next_element.text.to_i }
          end

          # If we haven't previously performed a search with this query string, get the
          # number of pages from the pagination bar at the bottom of the results page.
          # If there's no pagination bar there's only 1 page of results.
          if @pages == -1
            p = doc.css('div.pages > a').last
            @pages = p ? [1, p.text.to_i].max : 1
          end
        rescue => e
          @error = { error: e }
        end unless @results[page] || (@pages > -1 && page >= @pages)
      end

      # Make sure we do a query for the first page of results before getting
      # subsequent pages in order to correctly figure out the total number of
      # pages of results.
      pages = (page_num.is_a?(Range) ? page_num.to_a : [page_num])
      pages.unshift(0) if @pages == -1 && !pages.include?(0)
      pages.each { |i| search_proc.call i }

      results[page_num.is_a?(Range) ? page_num.max : page_num]
    end
Perform the search, supplying an optional page number to search on. Returns
 a result set limited to the 25 results Kickass Torrents returns itself. Will
 cache results for subsequent calls of search with the same query string.