Module: RockingChair::Server
- Defined in:
- lib/rocking_chair/server.rb
Constant Summary collapse
- BASE_URL =
/(http:\/\/)?127\.0\.0\.1:5984\//
Class Method Summary collapse
- .all_dbs(options = {}) ⇒ Object
- .bulk(db_name, options = {}) ⇒ Object
- .copy(db_name, doc_id, options = {}) ⇒ Object
- .create_db(name) ⇒ Object
- .database(name, parameters) ⇒ Object
- .databases ⇒ Object
- .delete(db_name, doc_id, options = {}) ⇒ Object
- .delete_db(name) ⇒ Object
- .info ⇒ Object
- .load(db_name, doc_id, options = {}) ⇒ Object
- .load_all(db_name, options = {}) ⇒ Object
- .normalize_options(options) ⇒ Object
- .normalize_url(url) ⇒ Object
- .reset ⇒ Object
- .respond_with(obj) ⇒ Object
- .respond_with_error(code, message = nil) ⇒ Object
- .store(db_name, doc_id, document, options) ⇒ Object
- .uuids(options = {}) ⇒ Object
- .view(db_name, design_doc_id, view_name, options = {}) ⇒ Object
Class Method Details
.all_dbs(options = {}) ⇒ Object
35 36 37 |
# File 'lib/rocking_chair/server.rb', line 35 def self.all_dbs( = {}) respond_with(databases.keys) end |
.bulk(db_name, options = {}) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/rocking_chair/server.rb', line 100 def self.bulk(db_name, = {}) return respond_with_error(404) unless databases.has_key?(db_name) databases[db_name].bulk() rescue RockingChair::Error => e e.raise_rest_client_error end |
.copy(db_name, doc_id, options = {}) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/rocking_chair/server.rb', line 92 def self.copy(db_name, doc_id, = {}) return respond_with_error(404) unless databases.has_key?(db_name) destination_id, revision = normalize_url(['Destination']) databases[db_name].copy(doc_id, destination_id, revision['rev']) rescue RockingChair::Error => e e.raise_rest_client_error end |
.create_db(name) ⇒ Object
48 49 50 51 |
# File 'lib/rocking_chair/server.rb', line 48 def self.create_db(name) databases[name] = RockingChair::Database.new respond_with({"ok" => true}) end |
.database(name, parameters) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/rocking_chair/server.rb', line 114 def self.database(name, parameters) if databases.has_key?(name) respond_with({ "db_name" => name, "doc_count" => databases[name].document_count, "doc_del_count" => 0, "update_seq" => 10, "purge_seq" => 0, "compact_running" => false, "disk_size" => 16473, "instance_start_time" => "1265409273572320", "disk_format_version" => 4}) else return respond_with_error(404) end end |
.databases ⇒ Object
7 8 9 |
# File 'lib/rocking_chair/server.rb', line 7 def self.databases @databases ||= {} end |
.delete(db_name, doc_id, options = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rocking_chair/server.rb', line 66 def self.delete(db_name, doc_id, = {}) = { 'rev' => nil }.update() ['rev'] = ['rev'].first if ['rev'].is_a?(Array) return respond_with_error(404) unless databases.has_key?(db_name) databases[db_name].delete(doc_id, ['rev']) rescue RockingChair::Error => e e.raise_rest_client_error end |
.delete_db(name) ⇒ Object
53 54 55 56 |
# File 'lib/rocking_chair/server.rb', line 53 def self.delete_db(name) databases.delete(name) respond_with({"ok" => true}) end |
.info ⇒ Object
31 32 33 |
# File 'lib/rocking_chair/server.rb', line 31 def self.info respond_with({"couchdb" => "Welcome","version" => "0.10.1"}) end |
.load(db_name, doc_id, options = {}) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/rocking_chair/server.rb', line 78 def self.load(db_name, doc_id, = {}) return respond_with_error(404) unless databases.has_key?(db_name) databases[db_name].load(doc_id, ) rescue RockingChair::Error => e e.raise_rest_client_error end |
.load_all(db_name, options = {}) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/rocking_chair/server.rb', line 85 def self.load_all(db_name, = {}) return respond_with_error(404) unless databases.has_key?(db_name) databases[db_name].all_documents() rescue RockingChair::Error => e e.raise_rest_client_error end |
.normalize_options(options) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/rocking_chair/server.rb', line 15 def self.() ( || {}).each do |k,v| [k] = [k].first if [k].is_a?(Array) end end |
.normalize_url(url) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/rocking_chair/server.rb', line 22 def self.normalize_url(url) url = url.to_s.gsub(BASE_URL, '') #.gsub('%2F', '/') if url.match(/\A(.*)\?(.*)?\Z/) return [$1, (CGI::parse($2 || ''))] else return [url, {}] end end |
.reset ⇒ Object
11 12 13 |
# File 'lib/rocking_chair/server.rb', line 11 def self.reset @databases = {} end |
.respond_with(obj) ⇒ Object
131 132 133 |
# File 'lib/rocking_chair/server.rb', line 131 def self.respond_with(obj) obj.to_json end |
.respond_with_error(code, message = nil) ⇒ Object
135 136 137 138 |
# File 'lib/rocking_chair/server.rb', line 135 def self.respond_with_error(code, =nil) ||= 'no such DB' {code => }.to_json end |
.store(db_name, doc_id, document, options) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/rocking_chair/server.rb', line 58 def self.store(db_name, doc_id, document, ) return respond_with_error(404) unless databases.has_key?(db_name) databases[db_name].store(doc_id, document, ) rescue RockingChair::Error => e e.raise_rest_client_error end |
.uuids(options = {}) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/rocking_chair/server.rb', line 39 def self.uuids( = {}) = { 'count' => 100 }.update() ['count'] = ['count'].first if ['count'].is_a?(Array) respond_with({"uuids" => RockingChair::Database.uuids(['count']) }) end |
.view(db_name, design_doc_id, view_name, options = {}) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/rocking_chair/server.rb', line 107 def self.view(db_name, design_doc_id, view_name, = {}) return respond_with_error(404) unless databases.has_key?(db_name) databases[db_name].view(design_doc_id, view_name, ) rescue RockingChair::Error => e e.raise_rest_client_error end |