# -*- coding: utf-8 -*- module Diglossa class Remote attr_accessor :app_dir, :env #, :document, :config_path, :revision attr_reader :config def initialize(app_dir, env = 'default') #, config_path = nil) self.env = env self.app_dir = File.expand_path(app_dir) + '/' #self.config_path = config_path #load_config load_couchapprc end # def config_path=(config_path) # @config_path = config_path || File.join(app_dir, 'config.js') # end # def load_config # if File.readable?(config_path) # @config = JSON.parse(File.read(config_path)) # else # raise "Could not find config at '#{config_path}'. Run `dg init`" # end # end def load_couchapprc @config ||= {} @config = JSON.parse(File.read(File.join(app_dir, '.couchapprc'))) @config['couchapprc'] = JSON.parse(File.read(File.join(app_dir, '.couchapprc'))) end def db_url if env =~ /^https?\:\/\// # the env is actual a db_url env else env_config = config['couchapprc']['env'][env] 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" 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] 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 = [] locales.each do |locale| json = RestClient.get "#{db_url}/_design/diglossa.coffee/_view/byBook?key=%5B%22#{url}%22%2C%22#{locale}%22%5D&include_docs=true", :content_type => :json, :accept => :json book = JSON.parse(json)['rows'][0]["doc"] rescue nil books.push book end books end def push_book books dbbooks = get_book books[0][:url] unless dbbooks.compact.empty? books.each_with_index do |book, index| book["_id"] = dbbooks[index]["_id"] book["_rev"] = dbbooks[index]["_rev"] end end push_docs books end 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 del_texts url dbdocs = get_texts url return if dbdocs.empty? dbdocs.each{|doc|doc["_deleted"] = true} push_docs dbdocs end 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 del_texts url end push_docs docs end 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