Ruby: Create CouchDB data (Create)

couch_create.rb


#! /usr/bin/ruby
# -*- encoding: utf-8 -*-
#
#	couch_create.rb
#
#					Jul/31/2020
#
# ---------------------------------------------------------------------
require 'faraday'
require 'json'
#
# ---------------------------------------------------------------------
def dict_append_proc (dict_aa,key,name,population,date_mod)
	unit = {}
	unit['name'] = name
	unit['population'] = population
	unit['date_mod'] = date_mod
	dict_aa[key] = unit
	return dict_aa
end

# ---------------------------------------------------------------------
def prepare_data_proc ()
dict_aa={}

dict_aa=dict_append_proc(dict_aa,'t2021',"Nagano",51847,"2006-3-19")
dict_aa=dict_append_proc(dict_aa,'t2022',"Matsumoto",23789,"2006-7-27")
dict_aa=dict_append_proc(dict_aa,'t2023',"Ueda",47251,"2006-2-8")
dict_aa=dict_append_proc(dict_aa,'t2024',"Komoro",78623,"2006-1-22")
dict_aa=dict_append_proc(dict_aa,'t2025',"Okaya",91489,"2006-5-11")
dict_aa=dict_append_proc(dict_aa,'t2026',"Shiojiri",27915,"2006-4-17")
dict_aa=dict_append_proc(dict_aa,'t2027',"Chino",36972,"2006-9-18")
dict_aa=dict_append_proc(dict_aa,'t2028',"Iida",74921,"2006-8-15")
dict_aa=dict_append_proc(dict_aa,'t2029',"Nakano",81923,"2006-10-11")
dict_aa=dict_append_proc(dict_aa,'t2030',"Suwa",47598,"2006-9-12")
dict_aa=dict_append_proc(dict_aa,'t2031',"Komagane",51738,"2006-7-22")
dict_aa=dict_append_proc(dict_aa,'t2032',"Saku",82514,"2006-8-17")
dict_aa=dict_append_proc(dict_aa,'t2033',"Ina",74361,"2006-12-29")
dict_aa=dict_append_proc(dict_aa,'t2034',"Chikuma",67293,"2006-10-18")

end
# ---------------------------------------------------------------------
STDERR.puts	"***start***"
#

URL="http://localhost:5984/nagano"
#
res = Faraday.delete URL
puts	res.status

res = Faraday.put URL
puts	res.status

#
dict_aa=prepare_data_proc()
#
dict_aa.sort.each {|key, value|
	url_aa = URL + "/" + key
	puts url_aa
	con = Faraday.new 
	res = con.put do |req|
		req.url url_aa
		req.headers['Content-Type'] = 'application/json'
		req.body = JSON.pretty_generate(value)
	end
}

STDERR.puts	"***End***"

# ---------------------------------------------------------------------

Run

./couch_create.rb

Recommended Posts

Ruby: Create CouchDB data (Create)
Ruby: Update CouchDB data (Update)
Ruby: Delete CouchDB data
Ruby: Read CouchDB data (Read)
Create a fortune using Ruby
Manipulating data in GCS during Ruby
I started Ruby
Ruby: Read CouchDB data (Read)
[Ruby on Rails] Introduction of initial data
How to get date data in Ruby
Create variable length binary data in Java
Create realistic dummy data with gem Faker
Ruby on Rails controller create / delete command
Procedure to create Ruby execution environment locally
Beginners create portfolio in Ruby on Rails