Module: CouchRest::RestAPI

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

Instance Method Summary collapse

Instance Method Details

#copy(uri, destination) ⇒ Object



54
55
56
57
58
59
# File 'lib/couchrest/rest_api.rb', line 54

def copy(uri, destination)
  JsonResponse.create(RestClient::Request.execute( :method => :copy,
                                          :url => uri,
                                          :headers => default_headers.merge('Destination' => destination)
                                        ))
end

#default_headersObject



5
6
7
8
9
10
# File 'lib/couchrest/rest_api.rb', line 5

def default_headers
  {
    :content_type => :json,
    :accept       => :json
  }
end

#delete(uri) ⇒ Object



50
51
52
# File 'lib/couchrest/rest_api.rb', line 50

def delete(uri)
  JsonResponse.create(RestClient.delete(uri, default_headers))
end

#get(uri) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/couchrest/rest_api.rb', line 25

def get(uri)
  begin
    JsonResponse.create(RestClient.get(uri, default_headers))
  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



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/couchrest/rest_api.rb', line 37

def post(uri, doc = nil)
  payload = doc.to_json if doc
  begin
    JsonResponse.create(RestClient.post(uri, payload, default_headers))
  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



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/couchrest/rest_api.rb', line 12

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