Class: CouchRest

Inherits:
Object
  • Object
show all
Defined in:
lib/pager.rb,
lib/database.rb,
lib/couchrest.rb

Defined Under Namespace

Classes: Database, Pager

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server = 'http://localhost:5984') ⇒ CouchRest

Returns a new instance of CouchRest.



10
11
12
# File 'lib/couchrest.rb', line 10

def initialize server = 'http://localhost:5984'
  @uri = server
end

Instance Attribute Details

#uriObject

Returns the value of attribute uri.



9
10
11
# File 'lib/couchrest.rb', line 9

def uri
  @uri
end

Class Method Details

.delete(uri) ⇒ Object



54
55
56
# File 'lib/couchrest.rb', line 54

def delete uri
  JSON.parse(RestClient.delete(uri))
end

.get(uri) ⇒ Object



45
46
47
# File 'lib/couchrest.rb', line 45

def get uri
  JSON.parse(RestClient.get(uri), :max_nesting => false)
end

.paramify_url(url, params = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/couchrest.rb', line 58

def paramify_url url, params = nil
  if params
    query = params.collect do |k,v|
      v = v.to_json if %w{key startkey endkey}.include?(k.to_s)
      "#{k}=#{CGI.escape(v.to_s)}"
    end.join("&")
    url = "#{url}?#{query}"
  end
  url
end

.post(uri, doc = nil) ⇒ Object



49
50
51
52
# File 'lib/couchrest.rb', line 49

def post uri, doc = nil
  payload = doc.to_json if doc
  JSON.parse(RestClient.post(uri, payload))
end

.put(uri, doc = nil) ⇒ Object



40
41
42
43
# File 'lib/couchrest.rb', line 40

def put uri, doc = nil
  payload = doc.to_json if doc
  JSON.parse(RestClient.put(uri, payload))
end

Instance Method Details

#create_db(name) ⇒ Object

create a database



34
35
36
37
# File 'lib/couchrest.rb', line 34

def create_db name
  CouchRest.put "#{@uri}/#{name}"
  database name
end

#database(name) ⇒ Object



19
20
21
# File 'lib/couchrest.rb', line 19

def database name
  CouchRest::Database.new(@uri, name)
end

#databasesObject

list all databases on the server



15
16
17
# File 'lib/couchrest.rb', line 15

def databases
  CouchRest.get "#{@uri}/_all_dbs"
end

#infoObject

get the welcome message



24
25
26
# File 'lib/couchrest.rb', line 24

def info
  CouchRest.get "#{@uri}/"
end

#restart!Object

restart the couchdb instance



29
30
31
# File 'lib/couchrest.rb', line 29

def restart!
  CouchRest.post "#{@uri}/_restart"
end