class Throw::Server
Attributes
db_name[R]
host[R]
Public Class Methods
new(db_name, host = nil)
click to toggle source
# File lib/couchdb/server.rb, line 10 def initialize(db_name, host = nil) @db_name = db_name @host = host || 'localhost:5984' @_designs = load_designs end
Public Instance Methods
delete(path)
click to toggle source
# File lib/couchdb/server.rb, line 46 def delete(path) current = get(path) data = {_rev: current[:_rev]} if current db_request(:delete, path, data) end
exists?(id)
click to toggle source
# File lib/couchdb/server.rb, line 36 def exists?(id) !!head(id) rescue RestClient::ResourceNotFound false end
find(id)
click to toggle source
# File lib/couchdb/server.rb, line 28 def find(id) get id end
get(path, params = {})
click to toggle source
# File lib/couchdb/server.rb, line 56 def get(path, params = {}) db_request(:get, path, params) end
head(path)
click to toggle source
# File lib/couchdb/server.rb, line 52 def head(path) db_request(:head, path) end
post(path, data = {})
click to toggle source
# File lib/couchdb/server.rb, line 64 def post(path, data = {}) db_request(:post, path, data) end
put(path, data = {})
click to toggle source
# File lib/couchdb/server.rb, line 60 def put(path, data = {}) db_request(:put, path, data) end
reset!()
click to toggle source
# File lib/couchdb/server.rb, line 68 def reset! request(:delete, @db_name) request(:put, @db_name) end
save(doc, opts = {})
click to toggle source
# File lib/couchdb/server.rb, line 32 def save(doc, opts = {}) doc.has_key?(:_id) ? put(doc[:_id], doc) : post('', doc) end
update_design(name, details = {})
click to toggle source
# File lib/couchdb/server.rb, line 16 def update_design(name, details = {}) details = begin get("_design/#{name}").merge(details) rescue RestClient::ResourceNotFound details end put("_design/#{name}", details) @_designs[name.to_sym] = Design.new(self, details) end
uuids(count: 10)
click to toggle source
# File lib/couchdb/server.rb, line 42 def uuids(count: 10) request(:get, '_uuids', count: count)[:uuids] end
Private Instance Methods
db_request(method, path, params = {})
click to toggle source
# File lib/couchdb/server.rb, line 79 def db_request(method, path, params = {}) request(method, "#{@db_name}/#{path}", params) end
load_designs()
click to toggle source
# File lib/couchdb/server.rb, line 103 def load_designs designs = get('_all_docs', startkey: '_design/', endkey: '_design0') designs[:rows].inject({}) do |memo, row| id = row[:id] || row[:_id] memo[id.split('/').last.to_sym] = Design.new(self, get(id)) memo end end
method_missing(name, *args, &block)
click to toggle source
Calls superclass method
# File lib/couchdb/server.rb, line 75 def method_missing(name, *args, &block) @_designs[name.to_sym] || super end
quote_params(params)
click to toggle source
# File lib/couchdb/server.rb, line 112 def quote_params(params) params[:startkey] = JSON.dump(params[:startkey]) if params.has_key? :startkey params[:endkey] = JSON.dump(params[:endkey]) if params.has_key? :endkey params end
request(method, path, params = {})
click to toggle source
# File lib/couchdb/server.rb, line 83 def request(method, path, params = {}) url = "http://#{@host}/#{path}" response = case method when :get params = quote_params(params) RestClient.get(url, params: params, accept: :json, content_type: :json) when :post RestClient.post(url, JSON.dump(params), accept: :json, content_type: :json) when :put RestClient.put(url, JSON.dump(params), accept: :json, content_type: :json) when :delete RestClient.delete(url, params: JSON.dump(params), accept: :json) when :head RestClient.head(url, accept: :json) end JSON.parse(response, symbolize_names: true) unless response.nil? || response.empty? end