lib/diglossa/remote.rb in diglossa-0.2.24 vs lib/diglossa/remote.rb in diglossa-0.2.27
- old
+ new
@@ -23,11 +23,10 @@
# raise "Could not find config at '#{config_path}'. Run `dg init`"
# end
# end
def load_couchapprc
- # тут есть теперь "data-dir", но она нужна в parse, а не здесь. Как загружать один раз эту хрень?
@config ||= {}
@config = JSON.parse(File.read(File.join(app_dir, '.couchapprc')))
@config['couchapprc'] = JSON.parse(File.read(File.join(app_dir, '.couchapprc')))
end
@@ -39,25 +38,40 @@
raise "No such env: #{env}" unless env_config && env_config['db']
env_config['db']
end
end
+ def get_id id
+ path = "#{db_url}/#{id}?include_docs=true"
+ response = RestClient.get path, :content_type => :json, :accept => :json # rescue nil
+ return "no _id #{id}" unless response.code == 200
+ result = JSON.parse(response.body)
+ response.code == 200 ? result : nil
+ end
+
def author(nic = "abelard")
+ # FIXME: для dbauthors нужен отдельный метод?
+ # gem needs authors info for building book-tree
nics = nic.kind_of?(Array) ? nic : [nic]
- path = "#{db_url}/_design/diglossa.coffee/_view/byAuthor?include_docs=true"
- response = RestClient.post path, {"keys"=> nics}.to_json, :content_type => :json, :accept => :json
+ #path = "#{db_url}/_design/diglossa.coffee/_view/byAuthor?include_docs=true"
+ path = "http://diglossa.ru:5984/diglossa/_design/diglossa.coffee/_view/byAuthor?include_docs=true"
+ response = RestClient.post path, {"keys"=> nics}.to_json, :content_type => :json, :accept => :json rescue nil
+ return "no connection to diglossa.ru" unless response
return "no authors with nics #{nics}" unless response.code == 200
authors = JSON.parse(response.body)["rows"].map{|r|r['doc']}
#pp authors
response.code == 200 ? authors : nil
end
def push_docs docs
docs = docs.kind_of?(Array) ? docs : [docs]
- # http://stackoverflow.com/questions/4435538/ruby-rest-client-make-it-never-timeout
- json = RestClient.post "#{db_url}/_bulk_docs", {"docs" => docs}.to_json, :content_type => :json, :accept => :json
- JSON.parse(json)
+ docs.each_slice(1000) do |chunk|
+ json =RestClient.post "#{db_url}/_bulk_docs", {"docs" => chunk}.to_json, :content_type => :json, :accept => :json
+ JSON.parse(json)
+ puts "pushed #{chunk.size}"
+ end
+ true
end
def get_book url
locales = ["ru", "en"]
books = []
@@ -75,48 +89,72 @@
books.each_with_index do |book, index|
book["_id"] = dbbooks[index]["_id"]
book["_rev"] = dbbooks[index]["_rev"]
end
end
- #pp books
push_docs books
end
- def texts_get url
+ def get_texts url
response = RestClient.get("#{db_url}/_design/diglossa.coffee/_view/byUrl?key=%22#{url}%22&include_docs=true")
JSON.parse(response)['rows'].map{|r|r['doc']} || []
end
- def texts_delete url
- dbdocs = texts_get url
+ def del_texts url
+ dbdocs = get_texts url
return if dbdocs.empty?
dbdocs.each{|doc|doc["_deleted"] = true}
push_docs dbdocs
end
- def texts_push url, nics, docs
- dbdocs = texts_get url
+ def push_texts url, docs
+ dbdocs = get_texts url
if dbdocs.size == docs.size
docs.each_with_index do |doc, index|
doc["_id"] = dbdocs[index]["_id"]
doc["_rev"] = dbdocs[index]["_rev"]
end
else
- texts_delete url
+ del_texts url
end
push_docs docs
end
- def seeds(name = "authors", push = false)
- path = "#{db_url}/_design/diglossa.coffee/_view/byAuthor?include_docs=true"
- response = RestClient.get "#{db_url}/_design/diglossa.coffee/_view/byAuthor?key=%22#{path.to_url}%22&include_docs=true", :content_type => :json, :accept => :json
- #JSON.parse( RestClient.get( "#{Diglossa::DBD}/_design/diglossa.coffee/_view/byAuthor?include_docs=true"))['rows'].map{|r|r['doc']} rescue nil
- response = RestClient.post path, {"keys"=> nics}.to_json, :content_type => :json, :accept => :json
- return "no authors with nics #{nics}" unless response.code == 200
- authors = JSON.parse(response.body)["rows"].map{|r|r['doc']}
- #pp authors
- response.code == 200 ? authors : nil
+ def get_coms url
+ response = RestClient.get("#{db_url}/_design/diglossa.coffee/_view/comsByUrl?key=%22#{url}%22&include_docs=true")
+ JSON.parse(response)['rows'].map{|r|r['doc']} || []
end
+
+ def del_coms url
+ dbdocs = get_coms url
+ return if dbdocs.empty?
+ dbdocs.each{|doc|doc["_deleted"] = true}
+ push_docs dbdocs
+ end
+
+ def push_coms url, docs
+ dbdocs = get_coms url
+ if dbdocs.size == docs.size
+ docs.each_with_index do |doc, index|
+ doc["_id"] = dbdocs[index]["_id"]
+ doc["_rev"] = dbdocs[index]["_rev"]
+ end
+ else
+ del_coms url
+ end
+ push_docs docs
+ end
+
+ # def seeds(name = "authors", push = false)
+ # path = "#{db_url}/_design/diglossa.coffee/_view/byAuthor?include_docs=true"
+ # response = RestClient.get "#{db_url}/_design/diglossa.coffee/_view/byAuthor?key=%22#{path.to_url}%22&include_docs=true", :content_type => :json, :accept => :json
+ # #JSON.parse( RestClient.get( "#{Diglossa::DBD}/_design/diglossa.coffee/_view/byAuthor?include_docs=true"))['rows'].map{|r|r['doc']} rescue nil
+ # response = RestClient.post path, {"keys"=> nics}.to_json, :content_type => :json, :accept => :json
+ # return "no authors with nics #{nics}" unless response.code == 200
+ # authors = JSON.parse(response.body)["rows"].map{|r|r['doc']}
+ # #pp authors
+ # response.code == 200 ? authors : nil
+ # end
end
end