12
9

More than 5 years have passed since last update.

Rubyで国立国会図書館APIに問い合わせ

Posted at

Rubyで国立国会図書館APIに問い合わせ

open_search_client.rb
require "faraday"
require 'rexml/document'

# open_search_client.rb
class OpenSearchClient

  URL_HEAD = 'http://iss.ndl.go.jp'

  def get_book_info_by_isbn(isbn)
      response = get('/api/opensearch', {:isbn => isbn})
      REXML::Document.new(response.body)
  end

  private

  def get(path, pram)
    con = Faraday.new(:url => URL_HEAD) do |f|
      f.request  :url_encoded
      f.response :logger
      f.adapter  Faraday.default_adapter
    end
    con.get path, pram # GET http://iss.ndl.go.jp/api/opensearch?isbn=9784774163666
  end
end
main.rb
o = OpenSearchClient.new
res =  o.get_book_info_by_isbn('978-4774163666')
puts res.elements['//rss/channel/item/title'].text #=> GitHub実践入門 : Pull Requestによる開発の変革
12
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
9