Class: NdlBook

Inherits:
Object
  • Object
show all
Defined in:
app/models/ndl_book.rb

Defined Under Namespace

Classes: AlreadyImported

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#urlObject

Returns the value of attribute url.



46
47
48
# File 'app/models/ndl_book.rb', line 46

def url
  @url
end

Class Method Details

.find_by_isbn(isbn) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/ndl_book.rb', line 23

def self.find_by_isbn(isbn)
  url = "http://iss.ndl.go.jp/api/opensearch?dpid=iss-ndl-opac&isbn=#{isbn}&cnt=1&idx=1"
  Rails.logger.debug url
  xml = open(url).read
  doc = Nokogiri::XML(xml).at('//channel/item')
  {
    :title => doc.at('./title').content,
    :publisher => doc.xpath('./dc:publisher').collect(&:content).join(' '),
    :creator => doc.xpath('./dc:creator[@xsi:type="dcndl:NDLNH"]').collect(&:content).join(' '),
    :isbn => doc.at('./dc:identifier[@xsi:type="dcndl:ISBN"]').try(:content).to_s
  }
end

.import_from_sru_response(jpno) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'app/models/ndl_book.rb', line 36

def self.import_from_sru_response(jpno)
  manifestation = Manifestation.where(:nbn => jpno).first
  return if manifestation
  url = "http://iss.ndl.go.jp/api/sru?operation=searchRetrieve&recordSchema=dcndl&&maximumRecords=1&&query=%28jpno=#{jpno}%29"
  response = Nokogiri::XML(open("http://iss.ndl.go.jp/api/sru?operation=searchRetrieve&recordSchema=dcndl&&maximumRecords=1&&query=%28jpno=#{jpno}%29")).at('//xmlns:recordData')
  return unless response.content
  doc = Nokogiri::XML(response.content)
  Manifestation.import_record(doc)
end

.per_pageObject



19
20
21
# File 'app/models/ndl_book.rb', line 19

def self.per_page
  10
end

.search(query, page = 1, per_page = self.per_page) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/models/ndl_book.rb', line 3

def self.search(query, page = 1, per_page = self.per_page)
  if query
    cnt = self.per_page
    page = 1 if page.to_i < 1
    idx = (page.to_i - 1) * cnt + 1
    doc = Nokogiri::XML(Manifestation.search_ndl(query, {:cnt => cnt, :page => page, :idx => idx, :raw => true}).to_s)
#      raise doc.to_s
    items = doc.xpath('//channel/item')
    total_entries = doc.at('//channel/openSearch:totalResults').content.to_i

    {:items => items, :total_entries => total_entries}
  else
    {:items => [], :total_entries => 0}
  end
end