Array
YALTools::YaJsonRows is a container of json rows.
Here is an example to delete all documents of the example db.
@couch = YALTools::Main.new(host, port, opts)
...
json = @couch.get("/example/_all_docs")
delete_list = YALTools::YaJsonRows.new(@couch, "example")
json["rows"] do |item|
delete_list << item
end
failed_list = delete_list.delete_all
In the other way, it can delete json rows reading from $stdin and with the update_all method.
update_list = YALTools::YaJsonRows.new(@couch, @dbname)
$stdin.each_line do |line|
begin
json = JSON::parse(line)
json["_deleted"] = true
rescue
json = {}
ensure
update_list << line if not json.empty?
end
end
failed_list = update_list.update_all
The “update_all“ method helps to add, delete or modify key/value.
When creating initial document without “_id”, POST method is required.
In this case, the “post” string can be pass to the method argument.
# File yalt/jsonrows.rb, line 48
48: def initialize(couch, dbname)
49: @couch = couch
50: @dbname = dbname
51:
52: @debug = false
53: end
adds “_deleted” = true to each item and executes update query.
The delete_all finally returns the failed json rows.
# File yalt/jsonrows.rb, line 58
58: def delete_all
59: self.each do |i|
60: i["_deleted"] = true
61: end
62: update_all
63: end
new push method
# File yalt/jsonrows.rb, line 100
100: def push(json)
101: case json
102: when String
103: hash = {}
104: begin
105: hash = JSON::parse(json)
106: rescue
107: hash = {}
108: ensure
109: original_push(hash)
110: end
111: when Hash
112: original_push(json)
113: when Array
114: $stderr.puts "[warn] Array json representation in YALTools::YaJsonRows is as-is basis."
115: original_push(json)
116: end
117: end
posts all item to _bulk_docs interface, then it returns failed json rows.
# File yalt/jsonrows.rb, line 66
66: def update_all
67: uri = format("/%s/_bulk_docs", @dbname)
68: json = { "docs" => self }
69: res = @couch.post(uri,json)
70: $stderr.puts "[debug] res=#{res}" if @debug
71: self.clear
72: failed_list = self.clone
73: if res.kind_of?(Net::HTTPSuccess)
74: JSON.parse(res.body).each do |doc|
75: if doc.has_key?("error")
76: failed_list << doc
77: $stderr.puts "[debug] error_doc=#{doc}" if @debug
78: end
79: end
80: else
81: begin
82: json = JSON.parse(res.body)
83: failed_list << json
84: rescue
85: failed_list << res.body
86: end
87: end
88: return failed_list
89: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.