Skip to content

Commit

Permalink
ゆにかねっとを使った検索を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
naokomada committed Nov 28, 2015
1 parent 632828b commit 66f3b78
Showing 1 changed file with 83 additions and 63 deletions.
146 changes: 83 additions & 63 deletions app/controllers/bookjudges_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'rest-client'
require 'nokogiri'
require 'uri'

require "rexml/document"

class BookjudgesController < ApplicationController
before_action :set_bookjudge, only: [:show, :edit, :update, :destroy]
Expand Down Expand Up @@ -32,49 +32,38 @@ def edit
def create
@bookjudge = Bookjudge.new(bookjudge_params)

#共通文字列
query_str = ""
api_path = "http://iss.ndl.go.jp/api/opensearch"
normal_query_str = "?title=" + @bookjudge.title.to_s + "&publisher=" + @bookjudge.author
isbn_query_str = "?isbn=" + @bookjudge.isbn

#NDLへの検索文字列の作成
if @bookjudge.isbn.length > 0
query_str = api_path + isbn_query_str
elsif
query_str = api_path + normal_query_str
end

#検索実施
response = RestClient.get URI.escape(query_str)
response.headers

#タイトル単位で配列に入れる
xml = response.to_str
puts "query_str = " + query_str
puts xml
doc = Nokogiri::XML(xml)
str = ""
myarr = Array.new
doc.xpath('/rss/channel/item/title').each do |item|
myarr.push(item)
end
ndl_result = judge_by_ndl @bookjudge
yunica_result = judge_by_yunica @bookjudge


#結果判定
if myarr.size < 1 #NDLにない
@bookjudge.judge_result = 1
elsif
#結果生成
#judge_result
# 2 ? NDLにないし他にもない 存在しない本?
# 1 O NDLにないが他の場所にある
# 0 X NDLにある
if ndl_result #ndlにあった
@bookjudge.judge_result = 0
elsif !ndl_result && yunica_result #ndlにはない 他の図書館にある
@bookjudge.judge_result = 1
else
@bookjudge.judge_result = 2
end

option_str = "NDLの結果: " + ndl_result.to_s + "<br />"
option_str += "ゆにかねっとの結果: " + yunica_result.to_s + "<br />"

#返却
respond_to do |format|
if @bookjudge.judge_result == 1 && @bookjudge.save
format.html { redirect_to @bookjudge, notice: 'やった! 国会図書館に納本されていないみたいです!' }
format.html { redirect_to @bookjudge, notice: ('やった! 国会図書館に納本されていないみたいです!' + option_str) }
format.json { render :show, status: :created, location: @bookjudge }
elsif @bookjudge.judge_result == 2
format.html { redirect_to @bookjudge, notice: ('すみません、うまく検索できませんでした。もしかしたら存在しない本かもしれません。' + option_str) }
#format.html { render :new }
format.json { render json: @bookjudge.errors, status: :unprocessable_entity }
else
format.html { redirect_to @bookjudge, notice: '残念、もう国会図書館に所蔵されているようです。' }
format.html { redirect_to @bookjudge, notice: ('残念、もう国会図書館に所蔵されているようです。' + option_str) }
#format.html { render :new }
format.json { render json: @bookjudge.errors, status: :unprocessable_entity }
end
Expand Down Expand Up @@ -105,52 +94,83 @@ def destroy
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_bookjudge
@bookjudge = Bookjudge.find(params[:id])

def judge_by_ndl(bookjudge)

#共通文字列
query_str = ""
api_path = "http://iss.ndl.go.jp/api/opensearch"
normal_query_str = "?title=" + @bookjudge.title.to_s + "&publisher=" + @bookjudge.author
isbn_query_str = "?isbn=" + @bookjudge.isbn

#NDLへの検索文字列の作成
if @bookjudge.isbn.length > 0
query_str = api_path + isbn_query_str
elsif
query_str = api_path + normal_query_str
end

# Never trust parameters from the scary internet, only allow the white list through.
def bookjudge_params
params.require(:bookjudge).permit(:title, :author, :isbn, :judge_result)
#検索実施
response = RestClient.get URI.escape(query_str)
response.headers

#タイトル単位で配列に入れる
xml = response.to_str
#puts "query_str = " + query_str
#puts xml
doc = Nokogiri::XML(xml)
str = ""
myarr = Array.new
doc.xpath('/rss/channel/item/title').each do |item|
myarr.push(item)
end

def judge_by_yunica(bookjudge)
#共通文字列 ?operation=searchRetrieve&query%3Ddpid%3D%22iss-yunika%22%20AND%20isbn%3D%229784787200532%22
#結果判定
(myarr.size > 0) ? true : false #ndlで調べた結果 あったらtrue
end



def judge_by_yunica(bookjudge)

#共通文字列
query_str = ""
api_path = "http://iss.ndl.go.jp/api/sru"
normal_query_str = "?title=" + @bookjudge.title.to_s + "&publisher=" + @bookjudge.author
isbn_query_str = "?operation=searchRetrieve&query%3Ddpid%3D%22iss-yunika%22%20AND%20isbn%3D%22" + + "%22"
#normal_query_str = "?title=" + @bookjudge.title.to_s + "&publisher=" + @bookjudge.author
isbn_query_str = "?operation=searchRetrieve&query%3Ddpid%3D%22iss-yunika%22%20AND%20isbn%3D%22" + URI.escape(bookjudge.isbn) + "%22"

#NDLへの検索文字列の作成
logger.debug("yunica isbnstr")
logger.debug(isbn_query_str)
logger.debug("end")


#検索文字列の作成
if @bookjudge.isbn.length > 0
query_str = api_path + isbn_query_str
elsif
query_str = api_path + normal_query_str
# elsif
# query_str = api_path + normal_query_str
end

#検索実施
response = RestClient.get URI.escape(query_str)
response = RestClient.get query_str
response.headers

#タイトル単位で配列に入れる
xml = response.to_str
puts "query_str = " + query_str
puts xml
doc = Nokogiri::XML(xml)
str = ""
myarr = Array.new
doc.xpath('/rss/channel/item/title').each do |item|
myarr.push(item)
end
xdoc = REXML::Document.new(response.to_str)

#結果判定
if myarr.size < 1 #NDLにない
@bookjudge.judge_result = 1
elsif
@bookjudge.judge_result = 0
end
result = xdoc.elements["//searchRetrieveResponse"].elements["numberOfRecords"].text.to_i
(result == 1) ? true : false #ゆにかで調べた結果(他の図書館で所蔵があるか)あったらtrue
end


private
# Use callbacks to share common setup or constraints between actions.
def set_bookjudge
@bookjudge = Bookjudge.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def bookjudge_params
params.require(:bookjudge).permit(:title, :author, :isbn, :judge_result)
end
end

0 comments on commit 66f3b78

Please sign in to comment.