Module: Exegesis::Database::Rest
- Included in:
- InstanceMethods
- Defined in:
- lib/exegesis/database/rest.rb
Instance Method Summary collapse
-
#delete(doc = {}) ⇒ Object
DELETE the doc from the database.
-
#get(id, opts = {}) ⇒ Object
GETs a document with the given id from the database.
-
#post(url, body = {}, headers = {}) ⇒ Object
POSTs the body to the database.
-
#put(id, body, headers = {}) ⇒ Object
PUTs the body to the given id in the database.
-
#raw_get(id, options = {}) ⇒ Object
performs a raw GET request against the database.
-
#save(docs) ⇒ Object
saves a document or collection thereof.
Instance Method Details
#delete(doc = {}) ⇒ Object
DELETE the doc from the database. requires a hash with _id and _rev keys
56 57 58 59 |
# File 'lib/exegesis/database/rest.rb', line 56 def delete(doc={}) raise ArgumentError, "doc must have both '_id' and '_rev' keys" unless doc['_id'] && doc['_rev'] Exegesis::Http.delete "#{@uri}/#{doc['_id']}?rev=#{doc['_rev']}" end |
#get(id, opts = {}) ⇒ Object
GETs a document with the given id from the database
17 18 19 20 21 22 23 24 25 |
# File 'lib/exegesis/database/rest.rb', line 17 def get(id, opts={}) if id.kind_of?(Array) collection = opts.delete(:collection) # nil or true for yes, false for no r = post '_all_docs?include_docs=true', {"keys"=>id} r['rows'].map {|d| Exegesis.instantiate d['doc'], self } else Exegesis.instantiate raw_get(id), self end end |
#post(url, body = {}, headers = {}) ⇒ Object
POSTs the body to the database
47 48 49 50 51 52 53 |
# File 'lib/exegesis/database/rest.rb', line 47 def post(url, body={}, headers={}) if body.is_a?(Hash) && body.empty? body = url url = '' end Exegesis::Http.post "#{@uri}/#{url}", (body || '').to_json, headers end |
#put(id, body, headers = {}) ⇒ Object
PUTs the body to the given id in the database
42 43 44 |
# File 'lib/exegesis/database/rest.rb', line 42 def put(id, body, headers={}) Exegesis::Http.put "#{@uri}/#{id}", (body || '').to_json, headers end |
#raw_get(id, options = {}) ⇒ Object
performs a raw GET request against the database
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/exegesis/database/rest.rb', line 5 def raw_get(id, ={}) keys = .delete(:keys) id = Exegesis::Http.escape_id id url = Exegesis::Http.format_url "#{@uri}/#{id}", if id.match(%r{^_design/.*/_view/.*$}) && keys Exegesis::Http.post url, {:keys => keys}.to_json else Exegesis::Http.get url end end |
#save(docs) ⇒ Object
saves a document or collection thereof
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/exegesis/database/rest.rb', line 28 def save(docs) if docs.is_a?(Array) post "_bulk_docs", { 'docs' => docs } else result = docs['_id'] ? put(docs['_id'], docs) : post(docs) if result['ok'] docs['_id'] = result['id'] docs['_rev'] = result['rev'] end docs end end |