Class: Gzlib::Search

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/gzlib/search.rb

Constant Summary collapse

Search =
'http://opac.gzlib.gov.cn/opac/search?hasholding=1&'
Para =
{page: 1, searchWay: 'title', sortWay: 'title200Weight', rows: 100, curlibcode: 'GT'}
AcceptableSearchWay =
[:title, :marc, :isbn, :author, :subject, :class, :publish, :callno]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, opt = {}) ⇒ Search

Returns a new instance of Search.



25
26
27
28
29
30
# 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

Instance Attribute Details

#pagesObject (readonly)

Returns the value of attribute pages.



12
13
14
# File 'lib/gzlib/search.rb', line 12

def pages
  @pages
end

Class Method Details

.method_missing(search_way, *keywords, &blk) ⇒ Object



17
18
19
20
21
22
# 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

Instance Method Details

#[](i) ⇒ Object



32
33
34
# File 'lib/gzlib/search.rb', line 32

def [](i)
  @books[i]
end

#curPageObject



50
51
52
# File 'lib/gzlib/search.rb', line 50

def curPage
  @para[:page]
end

#each(&b) ⇒ Object



36
37
38
39
40
41
42
43
44
# 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?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/gzlib/search.rb', line 54

def lastPage?
  curPage == pages
end

#totalObject



46
47
48
# File 'lib/gzlib/search.rb', line 46

def total
  reduce(0){ |sum, book| sum + 1 }
end