# -*- 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 load_diglossarc 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_diglossarc # тут есть теперь "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 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 author(nic = "abelard") 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 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) 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 #pp books push_docs books end def texts_get 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 return if dbdocs.empty? dbdocs.each{|doc|doc["_deleted"] = true} push_docs dbdocs end def texts_push url, nics, docs dbdocs = texts_get 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 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