Class: Couchdb

Inherits:
Object
  • Object
show all
Defined in:
lib/couchdb_basic.rb

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Couchdb

Returns a new instance of Couchdb.



7
8
9
# File 'lib/couchdb_basic.rb', line 7

def initialize(url)
    @url = url
end

Instance Method Details

#_get(url = "") ⇒ Object



11
12
13
14
# File 'lib/couchdb_basic.rb', line 11

def _get(url="")
    r = RestClient.get "#{@url}#{url}"
    MultiJson.load(r.to_str, :symbolize_keys => true)
end

#_post(data, bulk = false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/couchdb_basic.rb', line 16

def _post(data,bulk=false)
    begin
        url = @url
        if bulk
          url = "#{url}/_bulk_docs"
        end
        r = RestClient.post "#{url}", MultiJson.dump(data), :content_type => :json, :accept => :json
    rescue RestClient::Forbidden => e
        puts e.response.to_str
        r = e.response
    end
    MultiJson.load(r.to_str, :symbolize_keys => true)
end

#create(doc) ⇒ Object



34
35
36
37
38
39
# File 'lib/couchdb_basic.rb', line 34

def create(doc)
    r = _post(doc)
    doc[:_id]  = r[:id]
    doc[:_rev] = r[:rev]
    doc
end

#create_bulk(docs) ⇒ Object



41
42
43
44
# File 'lib/couchdb_basic.rb', line 41

def create_bulk(docs)
    docs = {:docs=>docs}
    _post(docs,true)    
end

#dbObject



30
31
32
# File 'lib/couchdb_basic.rb', line 30

def db()
    _get()
end

#delete(doc) ⇒ Object



63
64
65
66
# File 'lib/couchdb_basic.rb', line 63

def delete(doc)
    r = RestClient.delete "#{@url}/#{doc[:_id]}?rev=#{doc[:_rev]}", :content_type => :json
    nil
end

#get(id) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/couchdb_basic.rb', line 52

def get(id)
    begin
        _get "/#{id.to_uri}"
    rescue RestClient::ResourceNotFound
        nil
    rescue RestClient::BadRequest => e
        puts e.response.to_str
        nil
    end
end

#get_all(args = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/couchdb_basic.rb', line 85

def get_all(args={})
    url = "/_all_docs?include_docs=true"
    if args.has_key?( :skip )
        skip = MultiJson.dump( args[:skip] )
        url << "&skip=#{skip}"
    end
    if args.has_key?( :limit )
        limit = MultiJson.dump( args[:limit] )
        url << "&limit=#{limit}"
    end
    docs = []
    _get(url)[:rows].each{ | row | docs << row[:doc ] }
    docs
end

#update(doc) ⇒ Object



46
47
48
49
50
# File 'lib/couchdb_basic.rb', line 46

def update(doc)
    r = _post(doc)
    doc[:_rev] = r[:rev]
    doc
end

#view(design, view, args = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/couchdb_basic.rb', line 68

def view(design,view,args={})
    url = "/_design/#{design}/_view/#{view}?"
    if args.has_key?(:key)
        key = MultiJson.dump(args[:key]).to_uri
        url << "&key=#{key}"
    end
    if args.has_key?(:group)
        key = MultiJson.dump(args[:group]).to_uri
        url << "&group=#{key}"
    end
    if args.has_key?(:reduce)
        key = MultiJson.dump(args[:reduce]).to_uri
        url << "&reduce=#{key}"
    end
    _get(url)[:rows]
end