[Ruby] Ruby: Read CouchDB data (Read)

less than 1 minute read

couch_read.rb


#! /usr/bin/ruby
# -*- encoding: utf-8 -*-
#
# couch_read.rb
#
#     Jul/31/2020
#
# ---------------------------------------------------------------------
require 'faraday'
require 'json'
#
# ---------------------------------------------------------------------
STDERR.puts "*** 開始 ***"
#
URL="http://localhost:5984/nagano/_all_docs?include_docs=true"
#
res = Faraday.get URL

# puts res.status

json_str = res.body

dict_data=JSON.parse(json_str)
#
dict_data['rows'].each {|value|
  doc = value["doc"]
  str_out = doc["_id"] + "\t" + doc['name'] + "\t" \
   + doc["population"].to_s + "\t" + doc['date_mod']
  print(str_out,"\n")
 }
#
STDERR.puts "*** 終了 ***"
# ---------------------------------------------------------------------

実行

./couch_read.rb