Sha256: 94223324f28c8ee94bf6a2f1a544c7c3877cfa340974696baf42b093333c509d

Contents?: true

Size: 1.38 KB

Versions: 6

Compression:

Stored size: 1.38 KB

Contents

module RestAPI

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

  def put(uri, doc = nil)
    payload = doc.to_json if doc
    begin
      JSON.parse(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

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

  def post(uri, doc = nil)
    payload = doc.to_json if doc
    begin
      JSON.parse(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

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

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

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
couchrest-1.1.0.pre2 lib/couchrest/rest_api.rb
couchrest-1.1.0.pre lib/couchrest/rest_api.rb
couchrest-1.0.2 lib/couchrest/rest_api.rb
couchrest-1.0.1 lib/couchrest/rest_api.rb
couchrest-1.0.0 lib/couchrest/rest_api.rb
couchrest-1.0.0.beta3 lib/couchrest/rest_api.rb