class Gzlib::Search
Constants
- AcceptableSearchWay
- Para
- Search
Attributes
pages[R]
Public Class Methods
method_missing(search_way, *keywords, &blk)
click to toggle source
Calls superclass method
# File lib/gzlib/search.rb, line 17 def method_missing search_way, *keywords, &blk opt = {searchWay: search_way} opt.merge! keywords.pop if keywords[-1].is_a? Hash return new(keywords.join(' '), opt) if AcceptableSearchWay.include? search_way super end
new(key, opt={})
click to toggle source
# File lib/gzlib/search.rb, line 25 def initialize(key, opt={}) @para = Para.merge({q: escape(key)}).merge opt doc = getHtml @books = getBooks(doc) @pages = (@books.empty? ? 1 : getPages(doc)) end
Public Instance Methods
[](i)
click to toggle source
# File lib/gzlib/search.rb, line 32 def [](i) @books[i] end
curPage()
click to toggle source
# File lib/gzlib/search.rb, line 50 def curPage @para[:page] end
each(&b)
click to toggle source
# File lib/gzlib/search.rb, line 36 def each(&b) @books.each &b while not lastPage? nextPage books = getBooks(getHtml) @books.concat books books.each &b end end
lastPage?()
click to toggle source
# File lib/gzlib/search.rb, line 54 def lastPage? curPage == pages end
total()
click to toggle source
# File lib/gzlib/search.rb, line 46 def total reduce(0){ |sum, book| sum + 1 } end
Private Instance Methods
escape(str)
click to toggle source
# File lib/gzlib/search.rb, line 65 def escape(str) WEBrick::HTTPUtils.escape str.force_encoding('utf-8') end
getBooks(doc)
click to toggle source
# File lib/gzlib/search.rb, line 73 def getBooks(doc) doc.css(".bookmeta").map do |node| Gzlib::Book.new(node) end.to_a end
getHtml()
click to toggle source
# File lib/gzlib/search.rb, line 60 def getHtml html = Net::HTTP.get(URI("#{Search}#{para}")) Nokogiri::HTML(html) end
getPages(doc)
click to toggle source
# File lib/gzlib/search.rb, line 79 def getPages(doc) doc.at_css(".meneame .disabled").text.gsub(/[^\d]/,'').to_i end
nextPage()
click to toggle source
# File lib/gzlib/search.rb, line 83 def nextPage @para.merge!({page: curPage+1}) end
open(str)
click to toggle source
# File lib/gzlib/search.rb, line 87 def open(str) Net::HTTP.get(URI(str)) end
para()
click to toggle source
# File lib/gzlib/search.rb, line 69 def para @para.map{ |k,v| "#{k}=#{v}" }.join('&') end