simple_get.rb(json)
This Ruby code fetches book information from an API using an ISBN and parses the JSON response into a Ruby hash. It starts by importing the net/http
and json
libraries to handle HTTP requests and JSON parsing. The API URL, which includes the ISBN 9784791765553
, is defined as a string. This URL string is then parsed into a URI object. An HTTP GET request is made to this URI, and the response, which is in JSON format, is stored as a string. Finally, the JSON string is parsed into a Ruby hash for easy access and manipulation of the book data.
Execution:
require "net/http"
#=> true
require "json"
#=> true
url = "https://api.openbd.jp/v1/get?isbn=9784791765553"
#=> "https://api.openbd.jp/v1/get?isbn=9784791765553"
uri = URI(url)
#=> #<URI::HTTPS https://api.openbd.jp/v1/get?isbn=9784791765553>
response = Net::HTTP.get(uri)
#=> "[{\"onix\":{\"CollateralDetail\":{},\"RecordReference\":\"9784791765553...
JSON.parse(response)
#=>
[{"onix" =>
{"CollateralDetail" => {},
"RecordReference" => "9784791765553",
"NotificationType" => "03",
"ProductIdentifier" =>
{"ProductIDType" => "15", "IDValue" => "9784791765553"},
"DescriptiveDetail" =>
{"TitleDetail" =>
{"TitleType" => "01",
"TitleElement" =>
{"TitleElementLevel" => "01",
"TitleText" =>
{"collationkey" => "ネット バカ : インターネット ガ ワタシタチ ノ ノウ ニ シテイル コト",
"content" => "ネット・バカ : インターネットがわたしたちの脳にしていること"}}},
"Contributor" =>
[{"SequenceNumber" => "1",
"ContributorRole" => [],
"PersonName" =>
{"content" => "Carr, Nicholas G, 1959-", "collationkey" => ""}},
{"SequenceNumber" => "2",
"ContributorRole" => [],
"PersonName" =>
{"content" => "篠儀, 直子", "collationkey" => "シノギ, ナオコ"}}]},
"PublishingDetail" =>
{"Imprint" => {"ImprintName" => "青土社"},
"PublishingDate" => [{"PublishingDateRole" => "11", "Date" => "201007"}]},
"ProductSupply" =>
{"SupplyDetail" =>
{"ProductAvailability" => "99",
"Price" =>
[{"PriceType" => "01",
"CurrencyCode" => "JPY",
"PriceAmount" => "2200"}]}}},
"hanmoto" =>
{"datecreated" => "2015-08-20 03:41:02",
"reviews" =>
[{"post_user" => "genkina",
"reviewer" => "養老孟司(解剖学者)",
"source_id" => 29,
"kubun_id" => 1,
"source" => "毎日新聞",
"choyukan" => "",
"han" => "",
"link" => "",
"appearance" => "2010-08-22",
"gou" => ""},
{"post_user" => "genkina",
"reviewer" => "森健(ジャーナリスト)",
"source_id" => 18,
"kubun_id" => 1,
"source" => "朝日新聞",
"choyukan" => "",
"han" => "",
"link" => "",
"appearance" => "2010-09-12",
"gou" => ""},
{"post_user" => "genkina",
"reviewer" => "",
"source_id" => 23,
"kubun_id" => 1,
"source" => "日本経済新聞",
"choyukan" => "",
"han" => "",
"link" => "",
"appearance" => "2010-09-12",
"gou" => ""},
{"post_user" => "genkina",
"reviewer" => "",
"source_id" => 19,
"kubun_id" => 1,
"source" => "東京新聞/中日新聞",
"choyukan" => "",
"han" => "",
"link" => "",
"appearance" => "2010-09-12",
"gou" => ""},
{"post_user" => "genkina",
"reviewer" => "鈴木幸一(インターネットイニシアティブ会長)",
"source_id" => 29,
"kubun_id" => 1,
"source" => "毎日新聞",
"choyukan" => "朝刊",
"han" => "",
"link" => "",
"appearance" => "2020-02-16",
"gou" => ""}],
"dateshuppan" => "2010-07",
"datemodified" => "2015-08-20 03:41:02"},
"summary" =>
{"isbn" => "9784791765553",
"title" => "ネット・バカ : インターネットがわたしたちの脳にしていること",
"volume" => "",
"series" => "",
"publisher" => "青土社",
"pubdate" => "201007",
"cover" => "",
"author" => "Carr,NicholasG,1959- 篠儀,直子"}}]
Executed with Ruby 3.4.2