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

RestClient::Resource

This commit is contained in:
Adam Wiggins 2008-03-10 17:20:57 -07:00
parent 15c4bab527
commit d66eaab936
5 changed files with 103 additions and 3 deletions

17
README
View file

@ -3,7 +3,7 @@
A simple REST client for Ruby, inspired by the microframework (Camping,
Sinatra...) style of specifying actions: get, put, post, delete.
== Usage
== Usage: Raw URL
require 'rest_client'
@ -16,11 +16,24 @@ Sinatra...) style of specifying actions: get, put, post, delete.
RestClient.delete 'http://some/resource'
See RestClient module docs for details.
== Usage: ActiveResource-Style
resource = RestClient::Resource.new 'http://some/resource'
resource.get
protected_resource = RestClient::Resource.new 'http://protected/resource', 'user', 'pass'
protected_resource.put File.read('pic.jpg'), :content_type => 'image/jpg'
See RestClient::Resource module docs for details.
== Shell
Require rest_client from within irb to access RestClient interactively, like
using curl at the command line. Better yet, require gem from within your
~/.rush/env.rb and have instant access to it from within your rush (http://rush.heroku.com) sessions.
~/.rush/env.rb and have instant access to it from within your rush
(http://rush.heroku.com) sessions.
== Meta

View file

@ -75,7 +75,7 @@ Rake::RDocTask.new do |t|
t.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
t.options << '--charset' << 'utf-8'
t.rdoc_files.include('README')
t.rdoc_files.include('lib/rest_client.rb')
t.rdoc_files.include('lib/*.rb')
end
CLEAN.include [ 'pkg', '*.gem', '.config' ]

58
lib/resource.rb Normal file
View file

@ -0,0 +1,58 @@
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

View file

@ -1,6 +1,8 @@
require 'uri'
require 'net/http'
require File.dirname(__FILE__) + '/resource'
# This module's static methods are the entry point for using the REST client.
module RestClient
def self.get(url, headers={})

27
spec/resource_spec.rb Normal file
View file

@ -0,0 +1,27 @@
require File.dirname(__FILE__) + '/base'
describe RestClient::Resource do
before do
@resource = RestClient::Resource.new('http://some/resource', 'jane', 'mypass')
end
it "GET" do
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {}, :user => 'jane', :password => 'mypass')
@resource.get
end
it "POST" do
RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'abc', :headers => { :content_type => 'image/jpg' }, :user => 'jane', :password => 'mypass')
@resource.post 'abc', :content_type => 'image/jpg'
end
it "PUT" do
RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'abc', :headers => { :content_type => 'image/jpg' }, :user => 'jane', :password => 'mypass')
@resource.put 'abc', :content_type => 'image/jpg'
end
it "DELETE" do
RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {}, :user => 'jane', :password => 'mypass')
@resource.delete
end
end