couchrest_model/deps/rest-client/lib/resource.rb

59 lines
1.2 KiB
Ruby
Raw Normal View History

2008-03-19 17:09:19 +01:00
module RestClient
# A class that can be instantiated for access to a RESTful resource,
# including authentication.
#
# Example:
#
# resource = RestClient::Resource.new('http://some/resource')
# jpg = resource.get(:accept => 'image/jpg')
#
# With HTTP basic authentication:
#
# resource = RestClient::Resource.new('http://protected/resource', 'user', 'pass')
# resource.delete
#
class Resource
attr_reader :url, :user, :password
def initialize(url, user=nil, password=nil)
@url = url
@user = user
@password = password
end
def get(headers={})
Request.execute(:method => :get,
:url => url,
:user => user,
:password => password,
:headers => headers)
end
def post(payload, headers={})
Request.execute(:method => :post,
:url => url,
:payload => payload,
:user => user,
:password => password,
:headers => headers)
end
def put(payload, headers={})
Request.execute(:method => :put,
:url => url,
:payload => payload,
:user => user,
:password => password,
:headers => headers)
end
def delete(headers={})
Request.execute(:method => :delete,
:url => url,
:user => user,
:password => password,
:headers => headers)
end
end
end