Class: Leanback::Couchdb
- Inherits:
-
Object
- Object
- Leanback::Couchdb
- Defined in:
- lib/leanback.rb
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #create ⇒ Object
- #create_doc(doc_id, doc) ⇒ Object
- #delete ⇒ Object
- #delete_config(section, key) ⇒ Object
- #delete_doc(doc_id, rev) ⇒ Object
- #delete_doc!(doc_id) ⇒ Object
- #edit_doc!(doc_id, data) ⇒ Object
- #get_config(section, key) ⇒ Object
- #get_doc(doc_id) ⇒ Object
-
#initialize(args = {}) ⇒ Couchdb
constructor
A new instance of Couchdb.
- #security_object ⇒ Object
- #security_object=(security_settings) ⇒ Object
- #set_config(section, key, value) ⇒ Object
- #update_doc(doc_id, data) ⇒ Object
- #view(design_doc_name, view_name, options = {}) ⇒ Object
- #where(hash, options = {}) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Couchdb
Returns a new instance of Couchdb.
19 20 21 22 23 24 25 |
# File 'lib/leanback.rb', line 19 def initialize(args = {}) @database = args.fetch(:database, nil) @address = args.fetch(:address, 'http://127.0.0.1') @port = args.fetch(:port, '5984') @username = args.fetch(:username, nil) @password = args.fetch(:password, nil) end |
Instance Attribute Details
#address ⇒ Object (readonly)
Returns the value of attribute address.
14 15 16 |
# File 'lib/leanback.rb', line 14 def address @address end |
#database ⇒ Object (readonly)
Returns the value of attribute database.
18 19 20 |
# File 'lib/leanback.rb', line 18 def database @database end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
17 18 19 |
# File 'lib/leanback.rb', line 17 def password @password end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
15 16 17 |
# File 'lib/leanback.rb', line 15 def port @port end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
16 17 18 |
# File 'lib/leanback.rb', line 16 def username @username end |
Instance Method Details
#create ⇒ Object
26 27 28 |
# File 'lib/leanback.rb', line 26 def create api_request { RestClient.put "#{address_port}/#{db_uri}", content_type, } end |
#create_doc(doc_id, doc) ⇒ Object
32 33 34 |
# File 'lib/leanback.rb', line 32 def create_doc(doc_id, doc) api_request { RestClient.put "#{address_port}/#{db_uri}/#{doc_uri(doc_id)}", generate_json(doc), } end |
#delete ⇒ Object
29 30 31 |
# File 'lib/leanback.rb', line 29 def delete api_request { RestClient.delete "#{address_port}/#{db_uri}", } end |
#delete_config(section, key) ⇒ Object
79 80 81 82 83 |
# File 'lib/leanback.rb', line 79 def delete_config(section, key) !!(RestClient.delete "#{address_port}/_config/#{URI.escape(section)}/#{URI.escape(key)}", ) rescue => e raise_error(e) end |
#delete_doc(doc_id, rev) ⇒ Object
35 36 37 |
# File 'lib/leanback.rb', line 35 def delete_doc(doc_id, rev) api_request { RestClient.delete "#{address_port}/#{db_uri}/#{doc_uri(doc_id)}?rev=#{rev}", } end |
#delete_doc!(doc_id) ⇒ Object
38 39 40 41 |
# File 'lib/leanback.rb', line 38 def delete_doc!(doc_id) document = get_doc(doc_id) delete_doc(document[:_id], document[:_rev]) end |
#edit_doc!(doc_id, data) ⇒ Object
48 49 50 51 52 |
# File 'lib/leanback.rb', line 48 def edit_doc!(doc_id, data) document = get_doc(doc_id) document_with_rev = document.merge(data) update_doc(doc_id, document_with_rev) end |
#get_config(section, key) ⇒ Object
74 75 76 77 78 |
# File 'lib/leanback.rb', line 74 def get_config(section, key) RestClient.get "#{address_port}/_config/#{URI.escape(section)}/#{URI.escape(key)}", rescue => e raise_error(e) end |
#get_doc(doc_id) ⇒ Object
42 43 44 |
# File 'lib/leanback.rb', line 42 def get_doc(doc_id) api_request { RestClient.get "#{address_port}/#{@database}/#{doc_id}", } end |
#security_object ⇒ Object
68 69 70 |
# File 'lib/leanback.rb', line 68 def security_object api_request { RestClient.get "#{address_port}/#{db_uri}/_security/", } end |
#security_object=(security_settings) ⇒ Object
65 66 67 |
# File 'lib/leanback.rb', line 65 def security_object=(security_settings) api_request { RestClient.put "#{address_port}/#{db_uri}/_security/", generate_json(security_settings), } end |
#set_config(section, key, value) ⇒ Object
71 72 73 |
# File 'lib/leanback.rb', line 71 def set_config(section, key, value) !!(RestClient.put "#{address_port}/_config/#{URI.escape(section)}/#{URI.escape(key)}", value, ) end |
#update_doc(doc_id, data) ⇒ Object
45 46 47 |
# File 'lib/leanback.rb', line 45 def update_doc(doc_id, data) api_request { RestClient.put "#{address_port}/#{db_uri}/#{doc_uri(doc_id)}", generate_json(data), } end |
#view(design_doc_name, view_name, options = {}) ⇒ Object
53 54 55 |
# File 'lib/leanback.rb', line 53 def view(design_doc_name, view_name, = {}) api_request { RestClient.get "#{address_port}/#{db_uri}/#{URI.escape(design_doc_name)}/_view/#{URI.escape(view_name)}?#{URI.encode_www_form()}", } end |
#where(hash, options = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/leanback.rb', line 56 def where(hash, = {}) search_term = hash.values index = hash.keys.join("_") = .merge({startkey: search_term.to_s, endkey: search_term.to_s}) view!("_design/#{index}_keys_finder", "find_by_keys_#{index}", ) rescue CouchdbException => e add_multiple_finder(hash.keys) view!("_design/#{index}_keys_finder", "find_by_keys_#{index}", ) end |