Module: Couchdb

Defined in:
lib/leanback.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.addressObject

Returns the value of attribute address.



611
612
613
# File 'lib/leanback.rb', line 611

def address
  @address
end

.portObject

Returns the value of attribute port.



612
613
614
# File 'lib/leanback.rb', line 612

def port
  @port
end

Class Method Details

.add_counter(options, auth_session = "") ⇒ Object

add a counter method to the database this creates a count method that counts documents by key



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/leanback.rb', line 457

def self.add_counter(options,auth_session = "")
 set_address 
 db_name = options[:database]
 key = options[:key] 
 design_doc_name = key + '_counter'
 
 view ='{
 "language" : "javascript",
 "views" :{
    "count_'+key+'" : {
      "map" : "function(doc){ if(doc.'+key+') emit(doc.'+key+',null);}", "reduce": "_count"   
    }
 }
}'

 begin  
  response = RestClient.put 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name, view, {:cookies => {"AuthSession" => auth_session}}
 rescue => e
    hash = Yajl::Parser.parse(e.response.to_s)
    raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
 end
end

.add_finder(options, auth_session = "") ⇒ Object

add a finder method to the database this creates a find by key method



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/leanback.rb', line 376

def self.add_finder(options,auth_session = "")
 set_address 
 db_name = options[:database]
 key = options[:key] 
 design_doc_name = key + '_finder'
 
 view ='{
 "language" : "javascript",
 "views" :{
    "find_by_'+key+'" : {
      "map" : "function(doc){ if(doc.'+key+') emit(doc.'+key+',doc);}"
    }
 }
}'

 begin  
  response = RestClient.put 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name, view, {:cookies => {"AuthSession" => auth_session}}
 rescue => e
    hash = Yajl::Parser.parse(e.response.to_s)
    raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
 end
end

.add_multiple_counter(options, auth_session = "") ⇒ Object

add a multiple counter method to the database this creates a count by keys method



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/leanback.rb', line 429

def self.add_multiple_counter(options,auth_session = "")
 set_address 
 db_name = options[:database]
 keys = options[:keys]
 view_name = keys.join("_")
 key = keys.join(",doc.")
 condition = keys.join(" && doc.")
 design_doc_name = view_name + '_keys_counter'

 view = '{
 "language" : "javascript",
 "views" :{
    "count_by_keys_'+view_name+'" : {
      "map" : "function(doc){ if(doc.'+condition+') emit([doc.'+key+'],null);}", "reduce": "_count"
    }
 }
}' 

 begin  
  response = RestClient.put 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name, view, {:cookies => {"AuthSession" => auth_session}}
 rescue => e
    hash = Yajl::Parser.parse(e.response.to_s)
    raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
 end
end

.add_multiple_finder(options, auth_session = "") ⇒ Object

add a multiple finder method to the database this creates a find by keys method



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/leanback.rb', line 401

def self.add_multiple_finder(options,auth_session = "")
 set_address 
 db_name = options[:database]
 keys = options[:keys]
 view_name = keys.join("_")
 key = keys.join(",doc.")
 condition = keys.join(" && doc.")
 design_doc_name = view_name + '_keys_finder'

 view = '{
 "language" : "javascript",
 "views" :{
    "find_by_keys_'+view_name+'" : {
      "map" : "function(doc){ if(doc.'+condition+') emit([doc.'+key+'],doc);}"
    }
 }
}' 

 begin  
  response = RestClient.put 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name, view, {:cookies => {"AuthSession" => auth_session}}
 rescue => e
    hash = Yajl::Parser.parse(e.response.to_s)
    raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
 end
end

.add_user(user, auth_session = "") ⇒ Object

add a new user



30
31
32
33
34
35
# File 'lib/leanback.rb', line 30

def self.add_user(user, auth_session="")
  o =  [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten;  
  salt  =  (0..50).map{ o[rand(o.length)]  }.join; 
  new_user = {:username => user[:username], :password => user[:password], :roles => user[:roles], :salt => salt}
  create_user(new_user,auth_session)
end

.all(auth_session = "") ⇒ Object

return a list of all databases



237
238
239
240
241
242
243
244
245
# File 'lib/leanback.rb', line 237

def self.all(auth_session = "")
     set_address
      begin
        response = RestClient.get 'http://' + @address + ':' + @port + '/_all_dbs', {:cookies => {"AuthSession" => auth_session}}
        hash = Yajl::Parser.parse(response.to_str)
      rescue => e
          raise e
      end
end

.change_password(username, new_password, auth_session = "") ⇒ Object

change non-admin user password



20
21
22
23
24
25
26
27
# File 'lib/leanback.rb', line 20

def self.change_password(username, new_password,auth_session = "")
 salty = salt()
 password_sha = Digest::SHA1.hexdigest(new_password + salty) 
 user_id = 'org.couchdb.user:' + username
 data = {"salt" => salty,"password_sha" => password_sha}
 doc = { :database => '_users', :doc_id => user_id, :data => data}   
 update_doc doc,auth_session
end

.count(options, auth_session = "") ⇒ Object

count by key



481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/leanback.rb', line 481

def self.count(options,auth_session = "")
 set_address 
 db_name = options[:database]
 index =  options.keys[1].to_s
 search_term = options.values[1]
 design_doc_name = index + '_counter'
 view_name = 'count_' + index
 
 begin 
  view = { :database => db_name, :design_doc => design_doc_name, :view => view_name}
  docs = find view,auth_session,search_term
 rescue CouchdbException => e
    #add a counter index if one doesn't already exist in the database
    #then count_by_key
    add_counter({:database => db_name, :key => index},auth_session)
    docs = find view,auth_session,search_term
 end
  count = docs[0]
 return count.to_i
end

.count_by_keys(options, auth_session = "", params = {}) ⇒ Object

count by keys example: hash = {:firstname =>‘john’, :gender => ‘male’ } Couchdb.count_by_keys(=> ‘contacts’, :keys => hash,auth_session)



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/leanback.rb', line 506

def self.count_by_keys(options,auth_session = "", params = {})
 set_address
 db_name = options[:database]
 keys = []
 search_term = []
 hash = options[:keys]

 hash.each do |k,v|
  keys << k
  search_term << v
 end
 index = keys.join("_")
 design_doc_name = index + '_keys_counter'
 view_name = 'count_by_keys_' + index
 params[:startkey] = search_term
 params[:endkey] = search_term
 
 begin 
  view = { :database => db_name, :design_doc => design_doc_name, :view => view_name}
  docs = find view,auth_session,key=nil,params
 rescue CouchdbException => e
    #add a finder/index if one doesn't already exist in the database
    #then find_by_keys
    add_multiple_counter({:database => db_name, :keys => keys},auth_session)
    docs = find view,auth_session,key=nil,params
 end
 count = docs[0]
 return count.to_i
end

.create(database_name, auth_session = "") ⇒ Object

create a database if one with the same name doesn’t already exist



213
214
215
216
217
218
219
220
221
222
# File 'lib/leanback.rb', line 213

def self.create(database_name,auth_session = "")
     set_address
     begin
       response = RestClient.put 'http://' + @address + ':' + @port + '/' + URI.escape(database_name), {:content_type => :json},{:cookies => {"AuthSession" => auth_session}}
       hash = Yajl::Parser.parse(response.to_str)
     rescue => e
       hash = Yajl::Parser.parse(e.response.to_s)
       raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
     end
end

.create_design(doc, auth_session = "") ⇒ Object

create a design document with views



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/leanback.rb', line 324

def self.create_design(doc,auth_session = "")
 set_address
 db_name = doc[:database]
 design_doc_name = doc[:design_doc]
 json_doc_name = doc[:json_doc]

 begin
  #bind json doc to string
  message_template = ERB.new File.new(json_doc_name).read
  str = message_template.result(binding)
 rescue => e
   raise e
 end

  begin
   
   response = RestClient.put 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name, str, {:cookies => {"AuthSession" => auth_session}}
    hash = Yajl::Parser.parse(response.to_str)
  rescue => e
    hash = Yajl::Parser.parse(e.response.to_s)
    raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
  end
end

.create_doc(doc, auth_session = "") ⇒ Object

create a document



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/leanback.rb', line 142

def self.create_doc( doc,auth_session = "")  
    db_name =  doc[:database]
    doc_id = doc[:doc_id]
    data = doc[:data]
    json_data = Yajl::Encoder.encode(data)
    set_address
    begin
       response = RestClient.put 'http://' + @address + ':' + @port + '/' + URI.escape(db_name) + '/' + URI.escape(doc_id),json_data, {:cookies => {"AuthSession" => auth_session}}
       hash = Yajl::Parser.parse(response.to_str)
     rescue => e
       hash = Yajl::Parser.parse(e.response.to_s)
       raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
     end
end

.create_user(user, auth_session = "") ⇒ Object

create a new user



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/leanback.rb', line 38

def self.create_user(user,auth_session= "")
  password_sha = Digest::SHA1.hexdigest(user[:password] + user[:salt])              
  
  user_hash = { :type => "user",
                   :name => user[:username],
                   :password_sha => password_sha,
                   :salt => user[:salt],
                   :roles => user[:roles]
                  }
   
   str = Yajl::Encoder.encode(user_hash)
   set_address
   begin
    response = RestClient.put 'http://' + @address + ':' + @port + '/_users/org.couchdb.user:' + URI.escape(user[:username]), str,{:cookies => {"AuthSession" => auth_session}}
    hash = Yajl::Parser.parse(response.to_str)
  rescue => e
    hash = Yajl::Parser.parse(e.response.to_s)
    raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
  end 

end

.delete(database_name, auth_session = "") ⇒ Object

delete a database



225
226
227
228
229
230
231
232
233
234
# File 'lib/leanback.rb', line 225

def self.delete(database_name,auth_session = "")
     set_address
      begin
        response = RestClient.delete 'http://' + @address + ':' + @port + '/' + URI.escape(database_name), {:cookies => {"AuthSession" => auth_session}}
        hash = Yajl::Parser.parse(response.to_str)
      rescue => e
        hash = Yajl::Parser.parse(e.response.to_s)
        raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
      end 
end

.delete_config(data, auth_session = "") ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/leanback.rb', line 114

def self.delete_config(data,auth_session = "") 
  section = data[:section]
  key = data[:key] 
  set_address
  begin
   response = RestClient.delete 'http://' + @address + ':' + @port + '/_config/' + URI.escape(section) + '/' + URI.escape(key), {:cookies => {"AuthSession" => auth_session}}
   hash = Yajl::Parser.parse(response.to_str)
  rescue => e
   hash = Yajl::Parser.parse(e.response.to_s)
   raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
 end
end

.delete_doc(doc, auth_session = "") ⇒ Object

delete document



186
187
188
189
190
191
192
193
# File 'lib/leanback.rb', line 186

def self.delete_doc(doc,auth_session = "")  
  db_name = doc[:database]
  doc_id = doc[:doc_id]
  doc = {:database => db_name, :doc_id => doc_id}
  hash = Couchdb.view doc,auth_session
  doc = {:database => db_name, :doc_id => doc_id, :rev => hash["_rev"]}
  delete_rev(doc,auth_session)
end

.delete_rev(doc, auth_session = "") ⇒ Object

delete a doc by rev#



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/leanback.rb', line 197

def self.delete_rev(doc,auth_session = "")
  db_name = doc[:database]
  doc_id = doc[:doc_id]
  rev = doc[:rev]
  set_address
  begin 
   response = RestClient.delete 'http://' + @address + ':' + @port + '/' + URI.escape(db_name)  + '/' + URI.escape(doc_id) + '?rev=' + rev, {:cookies => {"AuthSession" => auth_session}}
     hash = Yajl::Parser.parse(response.to_str)
   rescue => e
    hash = Yajl::Parser.parse(e.response.to_s)
    raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
   end
end

.docs_from(database_name, auth_session = "") ⇒ Object

return a list of all docs in the database



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/leanback.rb', line 591

def self.docs_from(database_name,auth_session = "")
  set_address
  begin
         response = RestClient.get 'http://' + @address + ':' + @port + '/' + URI.escape(database_name) + '/_all_docs?include_docs=true',{:cookies => {"AuthSession" => auth_session}}
         hash = Yajl::Parser.parse(response.to_str)
         rows = hash["rows"]
         count = 0 
         rows.each do |row|
            rows[count] = row["doc"]
            count += 1
          end
        return rows
  rescue => e
     hash = Yajl::Parser.parse(e.response.to_s)
     raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
  end  
end

.edit_doc(doc, auth_session = "") ⇒ Object

edit a document



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/leanback.rb', line 158

def self.edit_doc(doc,auth_session = "")
    db_name = doc[:database]
    doc_id = doc[:doc_id]
    data = doc[:data]
    json_data = Yajl::Encoder.encode(data)
    set_address
    begin
      response = RestClient.put 'http://' + @address + ':' + @port + '/' + URI.escape(db_name) + '/' + URI.escape(doc_id), json_data, {:cookies => {"AuthSession" => auth_session}}
       hash = Yajl::Parser.parse(response.to_str)
     rescue => e
       hash = Yajl::Parser.parse(e.response.to_s)
       raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
    end
end

.find(doc, auth_session = "", key = nil, options = {}) ⇒ Object

query a permanent view



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/leanback.rb', line 295

def self.find(doc,auth_session = "", key=nil, options = {})
 set_address
 db_name = doc[:database]
 design_doc_name = doc[:design_doc]
 view_name = doc[:view]
 params = get_params(options)
   
   begin
    if key == nil
     response = RestClient.get 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name + '/_view/' + view_name + '?' + URI.escape(params),{:cookies => {"AuthSession" => auth_session}}
    else
      key = URI.escape('?key="' + key + '"')
      response = RestClient.get 'http://' + @address + ':' + @port + '/' + db_name + '/_design/' + design_doc_name + '/_view/' + view_name + key + '&' + URI.escape(params) ,{:cookies => {"AuthSession" => auth_session}}
    end
     hash = Yajl::Parser.parse(response.to_str)
     rows = hash["rows"]
     count = 0
     rows.each do |row|
      rows[count] = row["value"]
      count += 1  
     end
     return rows
   rescue => e
    hash = Yajl::Parser.parse(e.response.to_s)
    raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
   end
end

.find_by(options, auth_session = "", params = {}) ⇒ Object

find by key



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/leanback.rb', line 537

def self.find_by(options,auth_session = "", params = {})
 set_address 
 db_name = options[:database]
 index =  options.keys[1].to_s
 search_term = options.values[1]
 design_doc_name = index + '_finder'
 view_name = 'find_by_' + index
 
 begin 
  view = { :database => db_name, :design_doc => design_doc_name, :view => view_name}
  docs = find view,auth_session,search_term,params
 rescue CouchdbException => e
    #add a finder/index if one doesn't already exist in the database
    #then find_by_key
    add_finder({:database => db_name, :key => index},auth_session)
    docs = find view,auth_session,search_term,params
 end
 return docs
end

.find_by_keys(options, auth_session = "", params = {}) ⇒ Object

find by keys example: hash = {:firstname =>‘john’, :gender => ‘male’ } Couchdb.find_by_keys(=> ‘contacts’, :keys => hash,auth_session)



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'lib/leanback.rb', line 561

def self.find_by_keys(options,auth_session = "", params = {})
 set_address
 db_name = options[:database]
 keys = []
 search_term = []
 hash = options[:keys]

 hash.each do |k,v|
  keys << k
  search_term << v
 end
 index = keys.join("_")
 design_doc_name = index + '_keys_finder'
 view_name = 'find_by_keys_' + index
 params[:startkey] = search_term
 params[:endkey] = search_term
 
 begin 
  view = { :database => db_name, :design_doc => design_doc_name, :view => view_name}
  docs = find view,auth_session,key=nil,params
 rescue CouchdbException => e
    #add a finder/index if one doesn't already exist in the database
    #then find_by_keys
    add_multiple_finder({:database => db_name, :keys => keys},auth_session)
    docs = find view,auth_session,key=nil,params
 end
 return docs
end

.find_on_fly(doc, auth_session = "", key = nil, options = {}) ⇒ Object

Query view, create view on fly if it dosen’t already exist



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/leanback.rb', line 349

def self.find_on_fly(doc,auth_session = "",key = nil, options = {})  
  db_name = doc[:database]
  design_doc = doc[:design_doc]
  view = doc[:view]
  json_doc = doc[:json_doc]

  begin 
     if( key == nil)
      docs = find({:database => db_name, :design_doc => design_doc, :view => view},auth_session,key=nil,options) 
     else
      docs = find({:database => db_name, :design_doc => design_doc, :view => view},auth_session,key,options) 
     end
    rescue CouchdbException => e
       document = { :database => db_name, :design_doc => design_doc, :json_doc => json_doc}
       create_design document,auth_session
       if( key == nil)
         docs = find({:database => db_name, :design_doc => design_doc, :view => view},auth_session,key=nil,options) 
       else
         docs = find({:database => db_name, :design_doc => design_doc, :view => view},auth_session,key,options) 
       end
     end
   return docs
end

.get_config(data, auth_session = "") ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/leanback.rb', line 128

def self.get_config(data,auth_session = "") 
  section = data[:section]
  key = data[:key] 
  set_address
  begin
   response = RestClient.get 'http://' + @address + ':' + @port + '/_config/' + URI.escape(section) + '/' + URI.escape(key), {:cookies => {"AuthSession" => auth_session}}
   hash = Yajl::Parser.parse(response.to_str)
  rescue => e
   hash = Yajl::Parser.parse(e.response.to_s)
   raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
 end
end

.get_params(options) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/leanback.rb', line 261

def self.get_params(options)
 params = ""
 if options.has_key?(:startkey)
  if options[:startkey].is_a? String
   params = 'startkey="' + options[:startkey] + '"'
  else
   params = 'startkey=' + options[:startkey].to_s # for complex keys
  end
 end
 if options.has_key?(:endkey)
  if options[:endkey].is_a? String
   params = params + '&endkey="' + options[:endkey] + '"'
  else
   params = params + '&endkey=' + options[:endkey].to_s  #for complex keys
  end
 end

 if options.has_key?(:limit)
  params = params + "&" + "limit=" + options[:limit].to_s
 end

 if options.has_key?(:skip)
  params = params + "&" + "skip=" + options[:skip].to_s
 end

  if options.has_key?(:descending)
  params = params + "&" + "descending=true"
 end

 return params
end

.get_security(db_name, auth_session = "") ⇒ Object

get security object



73
74
75
76
77
78
79
80
81
82
# File 'lib/leanback.rb', line 73

def self.get_security(db_name, auth_session="")
 set_address
 begin
  response = RestClient.get 'http://' + @address + ':' + @port + '/' + URI.escape(db_name) + '/_security/', {:cookies => {"AuthSession" => auth_session}}
  hash = Yajl::Parser.parse(response.to_str)
  rescue => e
  hash = Yajl::Parser.parse(e.response.to_s)
  raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
 end 
end

.login(username, password) ⇒ Object

login to couchdb



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

def self.(username, password)
  set_address
  data = 'name=' + username + '&password=' + password
  begin
   response = RestClient.post 'http://' + @address + ':' + @port + '/_session/', data, {:content_type => 'application/x-www-form-urlencoded'}
   response.cookies
  rescue => e
   hash = Yajl::Parser.parse(e.response.to_s)
   raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
 end
end

.saltObject



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

def self.salt
 o =  [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten;  
 salt  =  (0..50).map{ o[rand(o.length)]  }.join; 
end

.set_addressObject



614
615
616
617
618
619
620
621
# File 'lib/leanback.rb', line 614

def set_address()
 if @address == nil 
    @address = '127.0.0.1'
 end
 if @port == nil
    @port = '5984'
 end 
end

.set_config(data, auth_session = "") ⇒ Object

couchdb configuration api



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/leanback.rb', line 99

def self.set_config(data,auth_session = "") 
  section = data[:section]
  key = data[:key] 
  value = data[:value]
  json_data = Yajl::Encoder.encode(value)
  set_address
  begin
   response = RestClient.put 'http://' + @address + ':' + @port + '/_config/' + URI.escape(section) + '/' + URI.escape(key),json_data, {:cookies => {"AuthSession" => auth_session}}
   hash = Yajl::Parser.parse(response.to_str)
  rescue => e
   hash = Yajl::Parser.parse(e.response.to_s)
   raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
 end
end

.set_security(db_name, data, auth_session = "") ⇒ Object

add security object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/leanback.rb', line 61

def self.set_security(db_name, data,auth_session="")
  security_data = Yajl::Encoder.encode(data)
  set_address
  begin
  response = RestClient.put 'http://' + @address + ':' + @port + '/' + URI.escape(db_name) + '/_security/',security_data, {:cookies => {"AuthSession" => auth_session}}
  hash = Yajl::Parser.parse(response.to_str)
  rescue => e
   hash = Yajl::Parser.parse(e.response.to_s)
   raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
  end 
end

.update_doc(doc, auth_session = "") ⇒ Object

update a doc



174
175
176
177
178
179
180
181
182
183
# File 'lib/leanback.rb', line 174

def self.update_doc(doc,auth_session = "")
     db_name = doc[:database]
     doc_id = doc[:doc_id]
     data = doc[:data]
     doc = {:database => db_name, :doc_id => doc_id}
     options = Couchdb.view doc,auth_session 
     options = options.merge(data)
     doc = {:database => db_name, :doc_id => doc_id, :data => options}
     edit_doc doc,auth_session
end

.view(doc, auth_session = "") ⇒ Object

view a document



248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/leanback.rb', line 248

def self.view(doc,auth_session = "")
 set_address
 db_name = doc[:database]
 doc_id = doc[:doc_id]
  begin
   response = RestClient.get 'http://' + @address + ':' + @port + '/' + db_name + '/' + doc_id,{:cookies => {"AuthSession" => auth_session}}
   hash = Yajl::Parser.parse(response.to_str)
  rescue => e
   hash = Yajl::Parser.parse(e.response.to_s)
   raise CouchdbException.new(hash), "CouchDB: Error - " + hash.values[0] + ". Reason - "  + hash.values[1]
 end 
end