Class: Gemqs::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/gemqs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScraper

Returns a new instance of Scraper.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gemqs.rb', line 22

def initialize
  #~ puts "  gemqs (gem quick-search) "
  #~ puts "    by David Heitzman, 2012. http://aptifuge.com"
  #~ puts 
  case ARGV.size
  when 0 
    puts "usage: gemqs [search terms] "
  else
    print "gemqs "
    query=ARGV.join("+")
    conf[:terms] = query.split "+"
    query = "/search?query="+query
    conf[:query] = query
  end 
  @results = []
end

Instance Attribute Details

#resultsObject

Returns the value of attribute results.



21
22
23
# File 'lib/gemqs.rb', line 21

def results
  @results
end

Instance Method Details

#agentObject



38
39
40
# File 'lib/gemqs.rb', line 38

def agent
  @agent=Mechanize.new
end

#get_pageObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gemqs.rb', line 45

def get_page
  if conf[:query].size > 0
    puts "  searching #{conf['urls'].map{|k,v| v }.join(', ')} on the terms: "+conf[:terms].join( " " )
#          puts conf['urls']['rubygems']+conf[:query]

    rpage=html_page
    ritems = rpage.css('div.gems.border>ol>li')
    ritems.each do |rli| 
    
    self.results << [
      rli.children[1].text.split(" ").first.gsub(",","").to_i,
      rli.children[3].text.split("\n")[1],
  #       rli.children[3].text.split("\n")[2], 
      rli.css("a").text.split("\n")[2].delete("\r"),   
      rli.css("a>strong").text, 
      rli.css("div>strong").text, 
      ]
    end 
    results.sort{|a,b| b.first <=> a.first}.each do |i|
       printf("%-40.40s %14.14s downloads #{ i[2] }\n",i[1],i[4] )
    end 
  end 
end

#html_pageObject



41
42
43
# File 'lib/gemqs.rb', line 41

def html_page
  @html_page ||= Nokogiri::HTML( open( conf['urls']['rubygems']+conf[:query] ) )
end