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

Class Method Details

.all_dbs(options = {}) ⇒ Object



35
36
37
# File 'lib/rocking_chair/server.rb', line 35

def self.all_dbs(options = {})
  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, options = {})
  return respond_with_error(404) unless databases.has_key?(db_name)
  databases[db_name].bulk(options)
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, options = {})
  return respond_with_error(404) unless databases.has_key?(db_name)
  destination_id, revision = normalize_url(options['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

.databasesObject



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, options = {})
  options = {
    'rev' => nil
  }.update(options)
  options['rev'] = options['rev'].first if options['rev'].is_a?(Array)
  
  return respond_with_error(404) unless databases.has_key?(db_name)
  databases[db_name].delete(doc_id, options['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

.infoObject



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, options = {})
  return respond_with_error(404) unless databases.has_key?(db_name)
  databases[db_name].load(doc_id, options)
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, options = {})
  return respond_with_error(404) unless databases.has_key?(db_name)
  databases[db_name].all_documents(options)
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.normalize_options(options)
  (options || {}).each do |k,v|
    options[k] = options[k].first if options[k].is_a?(Array)
  end
  options
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, normalize_options(CGI::parse($2 || ''))]
  else
    return [url, {}]
  end
end

.resetObject



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, message=nil)
  message ||= 'no such DB'
  {code => message}.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, options)
  return respond_with_error(404) unless databases.has_key?(db_name)

  databases[db_name].store(doc_id, document, options)
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(options = {})
  options = {
    'count' => 100
  }.update(options)
  options['count'] = options['count'].first if options['count'].is_a?(Array)

  respond_with({"uuids" => RockingChair::Database.uuids(options['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, options = {})
  return respond_with_error(404) unless databases.has_key?(db_name)
  databases[db_name].view(design_doc_id, view_name, options)
rescue RockingChair::Error => e
  e.raise_rest_client_error
end