Module: RestAPI

Included in:
CouchRest
Defined in:
lib/couchrest/rest_api.rb

Instance Method Summary collapse

Instance Method Details

#copy(uri, destination) ⇒ Object



45
46
47
48
49
50
# File 'lib/couchrest/rest_api.rb', line 45

def copy(uri, destination) 
  JSON.parse(RestClient::Request.execute( :method => :copy,
                                          :url => uri,
                                          :headers => {'Destination' => destination}
                                        ).to_s)
end

#delete(uri) ⇒ Object



41
42
43
# File 'lib/couchrest/rest_api.rb', line 41

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

#get(uri) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/couchrest/rest_api.rb', line 16

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

#post(uri, doc = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/couchrest/rest_api.rb', line 28

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

#put(uri, doc = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/couchrest/rest_api.rb', line 3

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