Ruby: Update CouchDB data (Update)

couch_update.rb


#! /usr/bin/ruby
# -*- encoding: utf-8 -*-
#
#	couch_update.rb
#
#					Jul/31/2020
#
# ---------------------------------------------------------------------
require 'faraday'
require 'json'
require 'date'
#
# ---------------------------------------------------------------------
STDERR.puts	"***start***"
#
key_in = ARGV[0]
population_in = ARGV[1].to_i
puts key_in,population_in

URL="http://localhost:5984/nagano/" + key_in
#
res = Faraday.get URL

puts	res.status

if res.status == 200 then
	json_str = res.body

	data_aa=JSON.parse(json_str)
#
	puts data_aa
	puts data_aa["_rev"]
	puts data_aa["name"]
#
	args = {}
	args["name"] = data_aa["name"]
	args["population"] = population_in
	args["date_mod"] = Date.today

	url_up = URL + "?rev=" + data_aa["_rev"]

	con = Faraday.new 
	res = con.put do |req|
		req.url url_up
		req.headers['Content-Type'] = 'application/json'
		req.body = JSON.pretty_generate(args)
	end

	puts res.status
end

STDERR.puts	"***End***"
# ---------------------------------------------------------------------

Run

./couch_update.rb t2029 4213500

Recommended Posts

Ruby: Update CouchDB data (Update)
Ruby: Delete CouchDB data
Ruby: Read CouchDB data (Read)
Update Ruby in Unicorn environment
Rasils data save, update, delete
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