1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00
rest-client--rest-client/lib/resource.rb

59 lines
1.2 KiB
Ruby
Raw Normal View History

2008-03-10 20:20:57 -04: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