Class: Dcs::Debian::Searcher
- Inherits:
-
Object
- Object
- Dcs::Debian::Searcher
- Defined in:
- lib/dcs/debian/helper.rb
Constant Summary collapse
- BASE_URL =
"http://codesearch.debian.net"
Instance Attribute Summary collapse
-
#n_limit ⇒ Object
Returns the value of attribute n_limit.
-
#page_limit ⇒ Object
Returns the value of attribute page_limit.
Instance Method Summary collapse
- #extract_entry(node, target) ⇒ Object
- #extract_next_page_uri(node) ⇒ Object
- #indent ⇒ Object
-
#initialize ⇒ Searcher
constructor
A new instance of Searcher.
- #pagination(target, keyword) ⇒ Object
Constructor Details
#initialize ⇒ Searcher
Returns a new instance of Searcher.
11 12 13 14 |
# File 'lib/dcs/debian/helper.rb', line 11 def initialize @page_limit = 10 @n_limit = 10 end |
Instance Attribute Details
#n_limit ⇒ Object
Returns the value of attribute n_limit.
7 8 9 |
# File 'lib/dcs/debian/helper.rb', line 7 def n_limit @n_limit end |
#page_limit ⇒ Object
Returns the value of attribute page_limit.
6 7 8 |
# File 'lib/dcs/debian/helper.rb', line 6 def page_limit @page_limit end |
Instance Method Details
#extract_entry(node, target) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/dcs/debian/helper.rb', line 58 def extract_entry(node, target) url = BASE_URL + node.xpath("a").attribute("href").text raw = node.xpath("a/code").text raw =~ /(.+)debian\/(.+):(.+)/ package = $1 file = $2 line = $3 entry = {} case file when target entry[:path] = raw entry[:url] = url entry[:package] = package entry[:file] = file entry[:line] = line node.xpath("pre").each do |pre| data = [] pre.children.each do |cnode| if cnode.kind_of?(Nokogiri::XML::Element) if cnode.name != "br" data << indent + cnode.text end else data << indent + cnode.text end end entry[:pre] = data.join("\n") end else raise Error end entry end |
#extract_next_page_uri(node) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dcs/debian/helper.rb', line 45 def extract_next_page_uri(node) next_uri = nil node.xpath("//div[@id='pagination']").each do |div| div.xpath("a").each do |a| case a.text when "Next page" next_uri = BASE_URL + a.attribute("href").text end end end next_uri end |
#indent ⇒ Object
92 93 94 |
# File 'lib/dcs/debian/helper.rb', line 92 def indent " " * 2 end |
#pagination(target, keyword) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/dcs/debian/helper.rb', line 16 def pagination(target, keyword) next_uri = nil data = [] unless next_uri next_uri = sprintf("%s/search?q=%s+path%%3Adebian%%2F%s%%24", BASE_URL, keyword, target) end page = 1 n = 1 until next_uri.nil? or n > @n_limit or page > @page_limit html = open(next_uri, "r:utf-8").read Nokogiri.parse(html) do |doc| doc.xpath("//ul[@id='results']/li").each do |li| entry = extract_entry(li, target) unless entry.empty? if entry[:pre].include?(keyword) n = n + 1 yield(entry) end end end next_uri = extract_next_page_uri(doc) end page = page + 1 end end |