Module: EnjuNdl::NdlSearch::ClassMethods

Defined in:
lib/enju_ndl/ndl_search.rb

Instance Method Summary collapse

Instance Method Details

#create_frbr_instance(doc, manifestation) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/enju_ndl/ndl_search.rb', line 124

def create_frbr_instance(doc, manifestation)
  title = get_title(doc)
  creators = get_creators(doc).uniq
  language = get_language(doc)
  subjects = get_subjects(doc).uniq

  Patron.transaction do
    creator_patrons = Patron.import_patrons(creators)
    language_id = Language.where(:iso_639_2 => language).first.id rescue 1
    content_type_id = ContentType.where(:name => 'text').first.id rescue 1
    manifestation.creators << creator_patrons
    if defined?(EnjuSubject)
      subject_heading_type = SubjectHeadingType.where(:name => 'ndlsh').first
      unless subject_heading_type
        subject_heading_type = SubjectHeadingType.create!(:name => 'ndlsh')
      end
      subjects.each do |term|
        subject = Subject.where(:term => term[:term]).first
        unless subject
          subject = Subject.new(term)
          subject.subject_type = SubjectType.find(1)
          subject.save!
        end
        if subject.valid?
          manifestation.subjects << subject
          subject.subject_heading_types << subject_heading_type if subject.subject_heading_types.where(:id => subject_heading_type.id).empty?
        end
      end
    end
  end
end

#import_isbn(isbn) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/enju_ndl/ndl_search.rb', line 9

def import_isbn(isbn)
  return nil unless isbn
  lisbn = Lisbn.new(isbn)
  raise EnjuNdl::InvalidIsbn unless lisbn.valid?

  manifestation = Manifestation.find_by_isbn(lisbn.isbn)
  return manifestation if manifestation

  doc = return_xml(lisbn.isbn)
  raise EnjuNdl::RecordNotFound unless doc
  #raise EnjuNdl::RecordNotFound if doc.at('//openSearch:totalResults').content.to_i == 0
  import_record(doc)
end

#import_isbn!(isbn) ⇒ Object



118
119
120
121
122
# File 'lib/enju_ndl/ndl_search.rb', line 118

def import_isbn!(isbn)
  manifestation = import_isbn(isbn)
  manifestation.save!
  manifestation
end

#import_record(doc) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/enju_ndl/ndl_search.rb', line 23

def import_record(doc)
  nbn = doc.at('//dcterms:identifier[@rdf:datatype="http://ndl.go.jp/dcndl/terms/JPNO"]').try(:content)
  manifestation = Manifestation.where(:nbn => nbn).first if nbn
  return manifestation if manifestation

  publishers = get_publishers(doc)

  # title
  title = get_title(doc)

  # date of publication
  pub_date = doc.at('//dcterms:date').try(:content).to_s.gsub(/\./, '-')
  unless pub_date =~ /^\d+(-\d{0,2}){0,2}$/
    pub_date = nil
  end
  if pub_date
    date = pub_date.split('-')
    if date[0] and date[1]
      date = sprintf("%04d-%02d", date[0], date[1])
    else
      date = pub_date
    end
  end

  language = Language.where(:iso_639_2 => get_language(doc)).first
  if language
    language_id = language.id
  else
    language_id = 1
  end

  isbn = Lisbn.new(doc.at('//dcterms:identifier[@rdf:datatype="http://ndl.go.jp/dcndl/terms/ISBN"]').try(:content).to_s).try(:isbn)
  issn_l = StdNum::ISSN.normalize(doc.at('//dcterms:identifier[@rdf:datatype="http://ndl.go.jp/dcndl/terms/ISSNL"]').try(:content))
  classification_urls = doc.xpath('//dcterms:subject[@rdf:resource]').map{|subject| subject.attributes['resource'].value}
  if classification_urls
    ndc9_url = classification_urls.map{|url| URI.parse(URI.escape(url))}.select{|u| u.path.split('/').reverse[1] == 'ndc9'}.first
    if ndc9_url
      ndc = ndc9_url.path.split('/').last
    end
  end

  carrier_type = content_type = nil
  doc.xpath('//dcndl:materialType[@rdf:resource]').each do |d|
    case d.attributes['resource'].try(:content)
    when 'http://ndl.go.jp/ndltype/Book'
      carrier_type = CarrierType.where(:name => 'print').first
      content_type = ContentType.where(:name => 'text').first
    when 'http://purl.org/dc/dcmitype/Sound'
      content_type = ContentType.where(:name => 'audio').first
    when 'http://purl.org/dc/dcmitype/MovingImage'
      content_type = ContentType.where(:name => 'video').first
    when 'http://ndl.go.jp/ndltype/ElectronicResource'
      carrier_type = CarrierType.where(:name => 'file').first
    end
  end

  description = doc.at('//dcterms:abstract').try(:content)
  price = doc.at('//dcndl:price').try(:content)
  volume_number_string = doc.at('//dcndl:volume/rdf:Description/rdf:value').try(:content)
  extent = get_extent(doc)

  manifestation = nil
  Patron.transaction do
    publisher_patrons = Patron.import_patrons(publishers)

    manifestation = Manifestation.new(
      :original_title => title[:manifestation],
      :title_transcription => title[:transcription],
      :title_alternative => title[:alternative],
      :title_alternative_transcription => title[:alternative_transcription],
      # TODO: NDLサーチに入っている図書以外の資料を調べる
      #:carrier_type_id => CarrierType.where(:name => 'print').first.id,
      :language_id => language_id,
      :isbn => isbn,
      :pub_date => date,
      :description => description,
      :volume_number_string => volume_number_string,
      :price => price,
      :nbn => nbn,
      :start_page => extent[:start_page],
      :end_page => extent[:end_page],
      :height => extent[:height],
      :ndc => ndc
    )
    manifestation.carrier_type = carrier_type if carrier_type
    manifestation.manifestation_content_type = content_type if content_type
    manifestation.publishers << publisher_patrons
    create_frbr_instance(doc, manifestation)
    create_series_statement(doc, manifestation)
  end

  #manifestation.send_later(:create_frbr_instance, doc.to_s)
  return manifestation
end

#normalize_isbn(isbn) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/enju_ndl/ndl_search.rb', line 174

def normalize_isbn(isbn)
  if isbn.length == 10
    Lisbn.new(isbn).isbn13
  else
    Lisbn.new(isbn).isbn10
  end
end

#return_xml(isbn) ⇒ Object



182
183
184
185
186
187
188
189
190
191
# File 'lib/enju_ndl/ndl_search.rb', line 182

def return_xml(isbn)
  rss = self.search_ndl(isbn, {:dpid => 'iss-ndl-opac', :item => 'isbn'})
  if rss.channel.totalResults.to_i == 0
    isbn = normalize_isbn(isbn)
    rss = self.search_ndl(isbn, {:dpid => 'iss-ndl-opac', :item => 'isbn'})
  end
  if rss.items.first
    doc = Nokogiri::XML(open("#{rss.items.first.link}.rdf").read)
  end
end

#search_ndl(query, options = {}) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/enju_ndl/ndl_search.rb', line 156

def search_ndl(query, options = {})
  options = {:dpid => 'iss-ndl-opac', :item => 'any', :idx => 1, :per_page => 10, :raw => false}.merge(options)
  doc = nil
  results = {}
  startrecord = options[:idx].to_i
  if startrecord == 0
    startrecord = 1
  end
  url = "http://iss.ndl.go.jp/api/opensearch?dpid=#{options[:dpid]}&#{options[:item]}=#{URI.escape(query)}&cnt=#{options[:per_page]}&idx=#{startrecord}"
  if options[:raw] == true
    open(url).read
  else
    RSS::Rss::Channel.install_text_element("openSearch:totalResults", "http://a9.com/-/spec/opensearchrss/1.0/", "?", "totalResults", :text, "openSearch:totalResults")
    RSS::BaseListener.install_get_text_element "http://a9.com/-/spec/opensearchrss/1.0/", "totalResults", "totalResults="
    feed = RSS::Parser.parse(url, false)
  end
end