Last change
on this file since 477 was
477,
checked in by hendrikvanantwerpen, 12 years ago
|
Quick hack to allow responding despite not being authenticated. Something like tokes need to be added.
|
File size:
1.2 KB
|
Line | |
---|
1 | request = require './q-request' |
---|
2 | _ = require 'underscore' |
---|
3 | Q = require 'q' |
---|
4 | |
---|
5 | class CouchDB |
---|
6 | constructor: (url) -> |
---|
7 | @url = normalizeURL url |
---|
8 | get: (id) -> |
---|
9 | url = "#{@url}/#{id}" |
---|
10 | couchRequest 'GET', url |
---|
11 | post: (doc) -> |
---|
12 | url = "#{@url}" |
---|
13 | couchRequest 'POST', url, doc |
---|
14 | put: (id, doc) -> |
---|
15 | url = "#{@url}/#{id}" |
---|
16 | couchRequest 'PUT', url, doc |
---|
17 | |
---|
18 | normalizeURL = (url) -> |
---|
19 | url.replace /\/$/, '' |
---|
20 | |
---|
21 | couchRequest = (method, url, data) -> |
---|
22 | options = |
---|
23 | method: method, |
---|
24 | headers: |
---|
25 | 'content-type': 'application/json; charset=utf-8' |
---|
26 | 'accept': 'application/json' |
---|
27 | body: JSON.stringify (stringifyFunctions (data || {})) |
---|
28 | request(url, options) |
---|
29 | .then (res) => |
---|
30 | JSON.parse res |
---|
31 | , (err) => |
---|
32 | Q.reject (JSON.parse err) |
---|
33 | |
---|
34 | stringifyFunctions = (value) -> |
---|
35 | if value is null |
---|
36 | null |
---|
37 | else if _.isArray value |
---|
38 | value |
---|
39 | else if _.isFunction value |
---|
40 | value.toString() |
---|
41 | else if _.isObject value |
---|
42 | value = _.clone value |
---|
43 | _.each value, (propValue, propName) => |
---|
44 | value[propName] = stringifyFunctions propValue |
---|
45 | value |
---|
46 | else |
---|
47 | value |
---|
48 | |
---|
49 | exports.CouchDB = CouchDB |
---|
Note: See
TracBrowser
for help on using the repository browser.