Module: EnjuNii::CiNiiBook::ClassMethods

Defined in:
lib/enju_nii/cinii_book.rb

Instance Method Summary collapse

Instance Method Details

#import_from_cinii_books(options) ⇒ Object



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

def import_from_cinii_books(options)
  # if options[:isbn]
  lisbn = Lisbn.new(options[:isbn])
  raise EnjuNii::InvalidIsbn unless lisbn.valid?
  # end

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

  doc = return_rdf(lisbn.isbn)
  raise EnjuNii::RecordNotFound unless doc
  # raise EnjuNii::RecordNotFound if doc.at('//openSearch:totalResults').content.to_i == 0
  import_record_from_cinii_books(doc)
end

#import_record_from_cinii_books(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
117
118
119
# File 'lib/enju_nii/cinii_book.rb', line 23

def import_record_from_cinii_books(doc)
  # http://ci.nii.ac.jp/info/ja/api/api_outline.html#cib_od
  # return nil

  ncid = doc.at('//cinii:ncid').try(:content)
  identifier_type = IdentifierType.where(name: 'ncid').first
  identifier_type = IdentifierType.create!(name: 'ncid') unless identifier_type
  identifier = Identifier.where(body: ncid, identifier_type_id: identifier_type.id).first
  return identifier.manifestation if identifier

  creators = get_cinii_creator(doc)
  publishers = get_cinii_publisher(doc)

  # title
  title = get_cinii_title(doc)
  manifestation = Manifestation.new(title)

  # date of publication
  pub_date = doc.at('//dc:date').try(:content)
  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
  manifestation.pub_date = pub_date

  manifestation.statement_of_responsibility = doc.at('//dc:creator').try(:content)
  manifestation.extent = doc.at('//dcterms:extent').try(:content)
  manifestation.dimensions = doc.at('//cinii:size').try(:content)

  language = Language.where(iso_639_3: get_cinii_language(doc)).first
  if language
    manifestation.language_id = language.id
  else
    manifestation.language_id = 1
  end

  urn = doc.at("//dcterms:hasPart[@rdf:resource]")
  if urn
    urn = urn.attributes["resource"].value
    if urn =~ /^urn:isbn/
      isbn = Lisbn.new(urn.gsub(/^urn:isbn:/, "")).isbn
    end
  end

  identifier = {}
  if ncid
    identifier[:ncid] = Identifier.new(body: ncid)
    identifier_type_ncid = IdentifierType.where(name: 'ncid').first
    identifier_type_ncid = IdentifierType.where(name: 'ncid').create! unless identifier_type_ncid
    identifier[:ncid].identifier_type = identifier_type_ncid
  end
  if isbn
    identifier[:isbn] = Identifier.new(body: isbn)
    identifier_type_isbn = IdentifierType.where(name: 'isbn').first
    identifier_type_isbn = IdentifierType.where(name: 'isbn').create! unless identifier_type_isbn
    identifier[:isbn].identifier_type = identifier_type_isbn
  end
  identifier.each do |k, v|
    manifestation.identifiers << v
  end

  manifestation.carrier_type = CarrierType.where(name: 'volume').first
  manifestation.manifestation_content_type = ContentType.where(name: 'text').first

  if manifestation.valid?
    Agent.transaction do
      manifestation.save!
      create_cinii_series_statements(doc, manifestation)
      publisher_patrons = Agent.import_agents(publishers)
      creator_patrons = Agent.import_agents(creators)
      manifestation.publishers = publisher_patrons
      manifestation.creators = creator_patrons
      if defined?(EnjuSubject)
        subjects = get_cinii_subjects(doc)
        subject_heading_type = SubjectHeadingType.where(name: 'bsh').first
        subject_heading_type = SubjectHeadingType.create!(name: 'bsh') unless subject_heading_type
        subjects.each do |term|
          subject = Subject.where(term: term[:term]).first
          unless subject
            subject = Subject.new(term)
            subject.subject_heading_type = subject_heading_type
            subject_type = SubjectType.where(name: 'concept').first
            subject_type = SubjectType.create(name: 'concept') unless subject_type
            subject.subject_type = subject_type
          end
          manifestation.subjects << subject
        end
      end
    end
  end

  manifestation
end

#return_rdf(isbn) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/enju_nii/cinii_book.rb', line 139

def return_rdf(isbn)
  rss = self.search_cinii_by_isbn(isbn)
  if rss.channel.totalResults.to_i == 0
    rss = self.search_cinii_by_isbn(cinii_normalize_isbn(isbn))
  end
  if rss.items.first
    conn = Faraday.new("#{rss.items.first.link}.rdf") do |faraday|
      faraday.use FaradayMiddleware::FollowRedirects
      faraday.adapter :net_http
    end
    conn.get.body
  end
end

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/enju_nii/cinii_book.rb', line 121

def search_cinii_book(query, options = {})
  options = {p: 1, count: 10, raw: false}.merge(options)
  doc = nil
  results = {}
  startrecord = options[:idx].to_i
  if startrecord == 0
    startrecord = 1
  end
  url = "https://ci.nii.ac.jp/books/opensearch/search?q=#{URI.escape(query)}&p=#{options[:p]}&count=#{options[:count]}&format=rss"
  if options[:raw] == true
    open(url).read
  else
    RSS::RDF::Channel.install_text_element("opensearch:totalResults", "http://a9.com/-/spec/opensearch/1.1/", "?", "totalResults", :text, "opensearch:totalResults")
    RSS::BaseListener.install_get_text_element("http://a9.com/-/spec/opensearch/1.1/", "totalResults", "totalResults=")
    RSS::Parser.parse(url, false)
  end
end

#search_cinii_by_isbn(isbn) ⇒ Object



153
154
155
156
157
158
# File 'lib/enju_nii/cinii_book.rb', line 153

def search_cinii_by_isbn(isbn)
  url = "https://ci.nii.ac.jp/books/opensearch/search?isbn=#{isbn}&format=rss"
  RSS::RDF::Channel.install_text_element("opensearch:totalResults", "http://a9.com/-/spec/opensearch/1.1/", "?", "totalResults", :text, "opensearch:totalResults")
  RSS::BaseListener.install_get_text_element("http://a9.com/-/spec/opensearch/1.1/", "totalResults", "totalResults=")
  RSS::Parser.parse(url, false)
end