Class: Rid::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/rid/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Database

Initialize new Database object by url.



7
8
9
# File 'lib/rid/database.rb', line 7

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/rid/database.rb', line 3

def name
  @name
end

Instance Method Details

#base_url(doc) ⇒ Object

Base URL for design document



41
42
43
# File 'lib/rid/database.rb', line 41

def base_url(doc)
  File.join(@name, CGI.escape(doc.id))
end

#get(doc, options = {}) ⇒ Object

Get document.



13
14
15
16
# File 'lib/rid/database.rb', line 13

def get(doc, options = {})
  response = RestClient.get(url(doc, options)) rescue nil
  Rid::Document.new(:json => response.body) if response
end

#save(doc) ⇒ Object

Save document. Creates it, until exists.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rid/database.rb', line 21

def save(doc)
  # create database unless exists
  # TODO: make this lazy
  begin
    RestClient.put @name, nil
  rescue RestClient::PreconditionFailed
    # database already exists
  end
  # push document
  response = RestClient.put(url(doc), doc.to_json)
  json = JSON.parse(response.body)
  if json && json["ok"] && json["rev"]
    doc.rev = json["rev"]
  end
  # return response
  response
end

#url(doc, options = {}) ⇒ Object

URL for accessing document

Takes an optional options hash which gets converted to url encoded options and appended to the documents base url



51
52
53
# File 'lib/rid/database.rb', line 51

def url(doc, options = {})
  base_url(doc) + build_options_string(options)
end