Module: CouchRest

Defined in:
lib/couchrest.rb,
lib/couchrest/core/view.rb,
lib/couchrest/core/design.rb,
lib/couchrest/core/server.rb,
lib/couchrest/helper/pager.rb,
lib/couchrest/mixins/views.rb,
lib/couchrest/commands/push.rb,
lib/couchrest/core/database.rb,
lib/couchrest/core/document.rb,
lib/couchrest/core/response.rb,
lib/couchrest/more/property.rb,
lib/couchrest/helper/upgrade.rb,
lib/couchrest/helper/streamer.rb,
lib/couchrest/mixins/callbacks.rb,
lib/couchrest/commands/generate.rb,
lib/couchrest/mixins/design_doc.rb,
lib/couchrest/mixins/properties.rb,
lib/couchrest/mixins/validation.rb,
lib/couchrest/more/casted_model.rb,
lib/couchrest/mixins/attachments.rb,
lib/couchrest/mixins/class_proxy.rb,
lib/couchrest/more/extended_document.rb,
lib/couchrest/mixins/document_queries.rb,
lib/couchrest/validation/auto_validate.rb,
lib/couchrest/mixins/extended_attachments.rb,
lib/couchrest/validation/validation_errors.rb,
lib/couchrest/validation/contextual_validators.rb,
lib/couchrest/validation/validators/formats/url.rb,
lib/couchrest/validation/validators/formats/email.rb,
lib/couchrest/validation/validators/format_validator.rb,
lib/couchrest/validation/validators/length_validator.rb,
lib/couchrest/validation/validators/method_validator.rb,
lib/couchrest/validation/validators/generic_validator.rb,
lib/couchrest/validation/validators/numeric_validator.rb,
lib/couchrest/validation/validators/absent_field_validator.rb,
lib/couchrest/validation/validators/confirmation_validator.rb,
lib/couchrest/validation/validators/required_field_validator.rb

Overview

Extracted from dm-validations 0.9.10

Copyright © 2007 Guy van den Berg

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Modules: Callbacks, CastedModel, Commands, Mixins, Validation Classes: Database, Design, Document, ExtendedDocument, Pager, Property, Response, Server, Streamer, Upgrade, View

Constant Summary collapse

VERSION =
'0.23'

Class Method Summary collapse

Class Method Details

.constantize(camel_cased_word) ⇒ Object

extracted from Extlib

Constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.

“Module”.constantize #=> Module “Class”.constantize #=> Class



64
65
66
67
68
69
70
# File 'lib/couchrest.rb', line 64

def constantize(camel_cased_word)
  unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
    raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
  end

  Object.module_eval("::#{$1}", __FILE__, __LINE__)
end

.copy(uri, destination) ⇒ Object



183
184
185
# File 'lib/couchrest.rb', line 183

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

.database(url) ⇒ Object



135
136
137
138
139
# File 'lib/couchrest.rb', line 135

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



129
130
131
132
133
# File 'lib/couchrest.rb', line 129

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

.delete(uri) ⇒ Object



179
180
181
# File 'lib/couchrest.rb', line 179

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

.get(uri) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/couchrest.rb', line 154

def get(uri)
  begin
    JSON.parse(RestClient.get(uri), :max_nesting => false)
  rescue => e
    if $COUCHREST_DEBUG == true
      raise "Error while sending a GET request #{uri}\n: #{e}"
    else
      raise e
    end
  end
end

.humanize(lower_case_and_underscored_word) ⇒ Object

extracted from Extlib

Capitalizes the first word and turns underscores into spaces and strips _id. Like titleize, this is meant for creating pretty output.

Examples:

"employee_salary" #=> "Employee salary"
"author_id" #=> "Author"


80
81
82
# File 'lib/couchrest.rb', line 80

def humanize(lower_case_and_underscored_word)
  lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
end

.new(*opts) ⇒ Object

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



86
87
88
# File 'lib/couchrest.rb', line 86

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

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



187
188
189
190
191
192
193
194
195
196
# File 'lib/couchrest.rb', line 187

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/couchrest.rb', line 90

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



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/couchrest.rb', line 166

def post uri, doc = nil
  payload = doc.to_json if doc
  begin
    JSON.parse(RestClient.post(uri, payload))
  rescue Exception => e
    if $COUCHREST_DEBUG == true
      raise "Error while sending a POST request #{uri}\npayload: #{payload.inspect}\n#{e}"
    else
      raise e
    end
  end
end

.proxy(url) ⇒ Object

set proxy for RestClient to use



122
123
124
# File 'lib/couchrest.rb', line 122

def proxy url
  RestClient.proxy = url
end

.put(uri, doc = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/couchrest.rb', line 141

def put(uri, doc = nil)
  payload = doc.to_json if doc
  begin
    JSON.parse(RestClient.put(uri, payload))
  rescue Exception => e
    if $COUCHREST_DEBUG == true
      raise "Error while sending a PUT request #{uri}\npayload: #{payload.inspect}\n#{e}"
    else
      raise e
    end
  end
end