This repository has been archived by the owner on Jul 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
64 lines (57 loc) · 1.46 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require 'rake'
require 'rubygems'
require 'yaml'
require 'rest_client'
require 'json'
SITE = "http://localhost"
PORT = "5984"
DATABASE = "deposits"
desc "script data"
task :script_data do
if ENV['RACK_ENV'] && ENV['RACK_ENV'] == 'production'
db = ENV['COUCH_URL']
else
conf = YAML.load(File.read('config/virtualserver/config.yml'))
db = conf[:couch_url]
end
view = "_design/data/_view/by_id"
data_url = "#{db}/#{view}?descending=true"
data = RestClient.get "#{data_url}"
rows = JSON.parse(data.body)["rows"]
path = ""
unless File.exists?("db/_docs")
Dir.mkdir("db/_docs")
end
rows.each do |rec|
unless File.exists?("db/_docs/#{rec["id"]}.json")
doc = "{\n"
rec["value"].each do |value|
if value[0] != "_id" and value[0] != "_rev"
doc = "#{doc} \"#{value[0]}\" : "
doc = process_data_values(value[1], doc)
end
end
doc = "#{doc}}"
doc.gsub!(",\n}", "\n}")
File.open("db/_docs/#{rec["id"]}.json", 'a') { |f| f.write(doc) }
end
end
end
def process_data_values value, doc, suppress_line_break=false
if value.class == String
doc = "#{doc}\"#{value.gsub(/"/, '\"')}\","
unless suppress_line_break
doc = "#{doc}\n"
end
elsif value.class == Array
doc = "#{doc}["
value.each do |elem|
doc = process_data_values(elem, doc, true)
end
doc = "#{doc}],\n"
doc.gsub!(",]", "]")
else
doc = "#{doc}#{value},\n"
end
doc
end