Module: CouchRest

Defined in:
lib/couchrest.rb,
lib/couchrest/core/view.rb,
lib/couchrest/core/model.rb,
lib/couchrest/core/design.rb,
lib/couchrest/core/server.rb,
lib/couchrest/helper/pager.rb,
lib/couchrest/commands/push.rb,
lib/couchrest/core/database.rb,
lib/couchrest/core/document.rb,
lib/couchrest/helper/streamer.rb,
lib/couchrest/commands/generate.rb

Overview

CouchRest::Model - Document modeling, the CouchDB way

Defined Under Namespace

Modules: Commands Classes: Database, Design, Document, Model, Pager, Response, Server, Streamer, View

Constant Summary collapse

VERSION =
'0.12.6'

Class Method Summary collapse

Class Method Details

.copy(uri, destination) ⇒ Object



120
121
122
# File 'lib/couchrest.rb', line 120

def copy uri, destination
  JSON.parse(RestClient.copy(uri, {'Destination' => destination}))
end

.database(url) ⇒ Object



96
97
98
99
100
# File 'lib/couchrest.rb', line 96

def database url
  parsed = parse url
  cr = CouchRest.new(parsed[:host])
  cr.database(parsed[:database])
end

.database!(url) ⇒ Object

ensure that a database exists creates it if it isn’t already there returns it after it’s been created



90
91
92
93
94
# File 'lib/couchrest.rb', line 90

def database! url
  parsed = parse url
  cr = CouchRest.new(parsed[:host])
  cr.database!(parsed[:database])
end

.delete(uri) ⇒ Object



116
117
118
# File 'lib/couchrest.rb', line 116

def delete uri
  JSON.parse(RestClient.delete(uri))
end

.get(uri) ⇒ Object



107
108
109
# File 'lib/couchrest.rb', line 107

def get uri
  JSON.parse(RestClient.get(uri), :max_nesting => false)
end

.move(uri, destination) ⇒ Object



124
125
126
# File 'lib/couchrest.rb', line 124

def move uri, destination
  JSON.parse(RestClient.move(uri, {'Destination' => destination}))
end

.new(*opts) ⇒ Object

todo, make this parse the url and instantiate a Server or Database instance depending on the specificity.



47
48
49
# File 'lib/couchrest.rb', line 47

def new(*opts)
  Server.new(*opts)
end

.paramify_url(url, params = {}) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/couchrest.rb', line 128

def paramify_url url, params = {}
  if params && !params.empty?
    query = params.collect do |k,v|
      v = v.to_json if %w{key startkey endkey}.include?(k.to_s)
      "#{k}=#{CGI.escape(v.to_s)}"
    end.join("&")
    url = "#{url}?#{query}"
  end
  url
end

.parse(url) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/couchrest.rb', line 51

def parse url
  case url
  when /^http:\/\/(.*)\/(.*)\/(.*)/
    host = $1
    db = $2
    docid = $3
  when /^http:\/\/(.*)\/(.*)/
    host = $1
    db = $2
  when /^http:\/\/(.*)/
    host = $1
  when /(.*)\/(.*)\/(.*)/
    host = $1
    db = $2
    docid = $3
  when /(.*)\/(.*)/
    host = $1
    db = $2
  else
    db = url
  end

  db = nil if db && db.empty?

  {
    :host => host || "127.0.0.1:5984",
    :database => db,
    :doc => docid
  }
end

.post(uri, doc = nil) ⇒ Object



111
112
113
114
# File 'lib/couchrest.rb', line 111

def post uri, doc = nil
  payload = doc.to_json if doc
  JSON.parse(RestClient.post(uri, payload))
end

.proxy(url) ⇒ Object

set proxy for RestClient to use



83
84
85
# File 'lib/couchrest.rb', line 83

def proxy url
  RestClient.proxy = url
end

.put(uri, doc = nil) ⇒ Object



102
103
104
105
# File 'lib/couchrest.rb', line 102

def put uri, doc = nil
  payload = doc.to_json if doc
  JSON.parse(RestClient.put(uri, payload))
end