Module: CouchdbRubyServer
- Defined in:
- lib/couchdb-ruby-server/server.rb,
lib/couchdb-ruby-server/version.rb
Defined Under Namespace
Constant Summary collapse
- MIME_TYPES =
{ :all => "*/*", :text => "text/plain; charset=utf-8", :html => "text/html; charset=utf-8", :xhtml => "application/xhtml+xml", :xml => "application/xml", :js => "text/javascript", :css => "text/css", :ics => "text/calendar", :csv => "text/csv; charset=utf-8", :rss => "application/rss+xml", :atom => "application/atom+xml", :yaml => "application/x-yaml", :multipart_form => "multipart/form-data", :url_encoded_form => "application/x-www-form-urlencoded", :json => "application/json; charset=utf-8" }
- TYPES_MIME =
- COMPILED_PROCS =
{}
- VERSION =
'1.0.0'- @@map_results =
[]
- @@callables =
[]
- @@ddocs =
{}
- @@chunks =
list state
[]
- @@start_resp =
{}
- @@got_row =
false- @@last_row =
false- @@provides_used =
[]
- @@resp_mime =
nil- @@view_emit =
view filter
nil
Class Method Summary collapse
- .compile_proc(source, binding, name) ⇒ Object
- .emit(key, value) ⇒ Object
- .get_row ⇒ Object
- .json_parse(str) ⇒ Object
- .log(message) ⇒ Object
- .maybe_wrap_response(resp) ⇒ Object
- .provides(*args, &block) ⇒ Object
- .render_headers ⇒ Object
- .reset_list ⇒ Object
- .reset_provides ⇒ Object
- .respond(obj) ⇒ Object
- .respond_error(error, reason) ⇒ Object
- .respond_fatal(error, reason) ⇒ Object
- .run ⇒ Object
- .run_provides(req) ⇒ Object
- .run_reduce(srcs, ks, vs, rereduce, b) ⇒ Object
- .send(chunk) ⇒ Object
- .start(resp) ⇒ Object
- .to_json(obj) ⇒ Object
- .update_case(fn, args) ⇒ Object
Class Method Details
.compile_proc(source, binding, name) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/couchdb-ruby-server/server.rb', line 76 def self.compile_proc source, binding, name source_hash = source.hash fn = COMPILED_PROCS[source_hash] return fn if fn begin fn = eval source, binding, name, 1 raise "#{name} must respond_to #call" unless fn.respond_to?(:call) COMPILED_PROCS[source_hash] = fn rescue Exception respond_error :compilation_error, $! end fn end |
.emit(key, value) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/couchdb-ruby-server/server.rb', line 103 def self.emit(key, value) if @@view_emit.nil? @@map_results.push [key, value] else @@view_emit = true end end |
.get_row ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/couchdb-ruby-server/server.rb', line 128 def self.get_row return nil if @@last_row if not @@got_row @@got_row = true render_headers respond ["start", @@chunks, @@start_resp] @@start_resp = {} else respond ["chunks", @@chunks] end @@chunks = [] json = json_parse ARGF.gets if json[0] == 'list_end' @@last_row = true return nil elsif json[0] != 'list_row' respond_fatal :list_error, "not a row: #{json[0]}" end json[1] end |
.json_parse(str) ⇒ Object
46 47 48 49 |
# File 'lib/couchdb-ruby-server/server.rb', line 46 def self.json_parse(str) return nil if str.nil? Oj.load str end |
.log(message) ⇒ Object
51 52 53 |
# File 'lib/couchdb-ruby-server/server.rb', line 51 def self.log respond ['log', "#{message}"] end |
.maybe_wrap_response(resp) ⇒ Object
150 151 152 153 |
# File 'lib/couchdb-ruby-server/server.rb', line 150 def self.maybe_wrap_response resp {:body=>''}.merge( resp.respond_to?(:to_hash) ? resp.to_hash : {:body=>"#{resp}"}) end |
.provides(*args, &block) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/couchdb-ruby-server/server.rb', line 111 def self.provides(*args, &block) @@provides_used << {:format => args[0], :block => block}.merge( args.size > 1 ? {:mime => args[1]} : {} ) nil end |
.render_headers ⇒ Object
180 181 182 183 |
# File 'lib/couchdb-ruby-server/server.rb', line 180 def self.render_headers @@start_resp[:headers] ||= {} @@start_resp[:headers]["Content-Type"] ||= @@resp_mime if @@resp_mime end |
.reset_list ⇒ Object
168 169 170 171 172 173 |
# File 'lib/couchdb-ruby-server/server.rb', line 168 def self.reset_list @@got_row = false @@last_row = false @@chunks = [] @@start_resp = {} end |
.reset_provides ⇒ Object
175 176 177 178 |
# File 'lib/couchdb-ruby-server/server.rb', line 175 def self.reset_provides @@provides_used = [] @@resp_mime = nil end |
.respond(obj) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/couchdb-ruby-server/server.rb', line 55 def self.respond obj puts to_json obj rescue log "respond error converting object to JSON: #{$!.message}\n#{$!.backtrace.join("\n")}" puts 'false' end |
.respond_error(error, reason) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/couchdb-ruby-server/server.rb', line 62 def self.respond_error error, reason if reason.respond_to?(:message) && reason.respond_to?(:backtrace) reason = "#{reason.class.name}: #{reason.message}\n#{reason.backtrace.join("\n")}" end respond ['error', error, reason] end |
.respond_fatal(error, reason) ⇒ Object
69 70 71 72 |
# File 'lib/couchdb-ruby-server/server.rb', line 69 def self.respond_fatal error, reason respond_error error, reason exit(2) end |
.run ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 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 292 293 294 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 322 323 324 325 326 327 328 |
# File 'lib/couchdb-ruby-server/server.rb', line 211 def self.run log "CouchdbRubyServer started, RUBY_VERSION: #{RUBY_VERSION}" log "RUBY_GC_MALLOC_LIMIT=#{ENV['RUBY_GC_MALLOC_LIMIT']}" while cmd = $stdin.gets cmd = json_parse cmd case cmd[0] when "reset" @@callables = [] respond(true) when "add_fun" fn = compile_proc cmd[1], binding, "map (#{cmd[1][0..100]}...)" next unless fn @@callables << fn respond(true) when "map_doc" results = [] @@view_emit = nil doc = cmd[1] doc.freeze @@callables.each do |callable| @@map_results = [] begin callable.call(doc) results.push(@@map_results) rescue log "map raised exception with doc._id #{doc['_id']}", $! results.push([]) end end respond(results) when "reduce" ks, vs = cmd[2].transpose run_reduce(cmd[1], ks, vs, false, binding) when "rereduce" run_reduce(cmd[1], nil, cmd[2], true, binding) when "ddoc" cmd.shift ddoc_id = cmd.shift if ddoc_id == 'new' ddoc_id = cmd.shift @@ddocs[ddoc_id] = cmd.shift respond true else ddoc = @@ddocs[ddoc_id] if not ddoc respond_fatal :query_protocol_error, "uncached design doc: #{ddoc_id}" end point = ddoc path = cmd.shift begin path.each do |level| point = (point[level] || raise("missing #{level} function in #{ddoc_id}")) end rescue respond_error :not_found, $! next end next unless (fn = compile_proc point, binding, "#{ddoc['_id']}/#{path.join("/")}") begin response = nil args = cmd.shift case path[0] when "updates" respond update_case fn, args when "lists" #head = args[0] reset_list reset_provides tail = fn.call(*args) begin tail = run_provides(args[1]) unless @@provides_used.empty? rescue respond_error :not_acceptable, $! next end get_row unless @@got_row @@chunks << tail unless tail.nil? respond ["end", @@chunks] when "shows" respond ["resp", maybe_wrap_response(fn.call(*args))] when "filters" res = [] args[0].each do |doc| res.push(fn.call(doc, args[1]) ? true : false) end respond [true, res] when "views" res = [] args[0].each do |doc| @@view_emit = false fn.call doc res.push(@@view_emit ? true : false) end respond [true, res] when "validate_doc_update" begin fn.call(*args) respond 1 rescue respond({:forbidden => "bad doc"}) end else respond_fatal :unknown_command, "unknown ddoc command #{path[0]}" next end # case ddoc rescue Fatal respond_fatal $!.key, $!. rescue Error respond_error $!.key, $!. rescue respond_error :render_error, $! end end else respond_fatal :unknown_command, "unknown command #{cmd[0]}" end end end |
.run_provides(req) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/couchdb-ruby-server/server.rb', line 185 def self.run_provides req format = :unknown best_fun = if req['query'] and req['query']['format'] format = req['query']['format'] @@provides_used.find do |p| p[:format].to_s == format end elsif req['headers'] && (format = req['headers']['Accept']) f = nil format.split(',').each do |a| break if (f = a =~ /^\*\/\*/ ? @@provides_used.first : @@provides_used.find do |p| a == p[:mime] or p[:format] == TYPES_MIME[a] end) end f else @@provides_used.first end raise "format #{format} not supported" unless best_fun raise "no block in provides" unless best_fun[:block] && best_fun[:block].respond_to?(:call) @@resp_mime = best_fun[:mime] || MIME_TYPES[best_fun[:format]] best_fun[:block].call end |
.run_reduce(srcs, ks, vs, rereduce, b) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/couchdb-ruby-server/server.rb', line 155 def self.run_reduce(srcs, ks, vs, rereduce, b) fns = srcs.collect { |src| compile_proc(src, b, "reduce (#{src[0..100]}...)") || return } reductions = fns.collect { |fn| begin fn.call(ks, vs, rereduce) rescue log "#{rereduce ? 'rereduce' : 'reduce'} raised exception", $! nil end } respond [true, reductions] end |
.send(chunk) ⇒ Object
123 124 125 126 |
# File 'lib/couchdb-ruby-server/server.rb', line 123 def self.send(chunk) @@chunks << "#{chunk}" nil end |
.start(resp) ⇒ Object
118 119 120 121 |
# File 'lib/couchdb-ruby-server/server.rb', line 118 def self.start(resp) @@start_resp = resp if resp nil end |
.to_json(obj) ⇒ Object
42 43 44 |
# File 'lib/couchdb-ruby-server/server.rb', line 42 def self.to_json(obj) Oj.dump(obj) end |
.update_case(fn, args) ⇒ Object
17 18 19 20 |
# File 'lib/couchdb-ruby-server/server.rb', line 17 def self.update_case fn, args result = fn.call(*args) return ["up", result[0], maybe_wrap_response(result[1])] end |