class Kindai::Searcher

Attributes

keyword[RW]

Public Class Methods

Public Instance Methods

each() { |new_from_permalink| ... } click to toggle source
# File lib/kindai/searcher.rb, line 16
def each
  (1..(1/0.0)).each{ |page|
    Kindai::Util.logger.debug "page #{page}"
    uris = result_for(@keyword, page)
    return if uris.empty?
    uris.each{ |uri|
      yield Kindai::Book.new_from_permalink(uri)
    }
  }
end
length() click to toggle source
# File lib/kindai/searcher.rb, line 12
def length
  @length ||= total_of(@keyword)
end

Protected Instance Methods

result_for(keyword, page = 1) click to toggle source
# File lib/kindai/searcher.rb, line 36
def result_for keyword, page = 1
  page = Nokogiri Kindai::Util.fetch_uri(uri_for(keyword, page))
  page.search('a.item-link').map{ |item|
    'http://dl.ndl.go.jp' + item.attr('href')
  }
end
total_of(keyword) click to toggle source
# File lib/kindai/searcher.rb, line 28
def total_of(keyword)
  page = Nokogiri(Kindai::Util.fetch_uri(uri_for(keyword)))
  total = page.at('.tableheadercontent-left p').content.scan(/\d+/).first.to_i

  Kindai::Util.logger.debug "total: #{total}"
  total
end
uri_for(keyword, page = 1) click to toggle source
# File lib/kindai/searcher.rb, line 43
def uri_for keyword, page = 1
  rows = 100
  params = { :SID => 'kindai', :viewRestricted => 0, :searchWord => keyword, :pageNo => page, :rows => rows }
  root = URI.parse("http://dl.ndl.go.jp/search/searchResult")
  query = '?' + Kindai::Util.expand_params(params)
  root + query
end