mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
init
This commit is contained in:
commit
71b61e8597
4 changed files with 170 additions and 0 deletions
22
Rakefile
Normal file
22
Rakefile
Normal file
|
@ -0,0 +1,22 @@
|
|||
require 'rake'
|
||||
require 'spec/rake/spectask'
|
||||
|
||||
desc "Run all specs"
|
||||
Spec::Rake::SpecTask.new('spec') do |t|
|
||||
t.spec_files = FileList['spec/*_spec.rb']
|
||||
end
|
||||
|
||||
desc "Print specdocs"
|
||||
Spec::Rake::SpecTask.new(:doc) do |t|
|
||||
t.spec_opts = ["--format", "specdoc", "--dry-run"]
|
||||
t.spec_files = FileList['spec/*_spec.rb']
|
||||
end
|
||||
|
||||
desc "Run all examples with RCov"
|
||||
Spec::Rake::SpecTask.new('rcov') do |t|
|
||||
t.spec_files = FileList['spec/*_spec.rb']
|
||||
t.rcov = true
|
||||
t.rcov_opts = ['--exclude', 'examples']
|
||||
end
|
||||
|
||||
task :default => :spec
|
69
lib/rest.rb
Normal file
69
lib/rest.rb
Normal file
|
@ -0,0 +1,69 @@
|
|||
require 'uri'
|
||||
require 'net/http'
|
||||
require 'rexml/document'
|
||||
|
||||
module Rest
|
||||
def self.get(url)
|
||||
uri = parse_url(url)
|
||||
transmit uri, Net::HTTP::Get.new(uri.path, headers)
|
||||
end
|
||||
|
||||
def self.post(url, payload=nil)
|
||||
uri = parse_url(url)
|
||||
transmit uri, Net::HTTP::Post.new(uri.path, headers), payload
|
||||
end
|
||||
|
||||
def self.put(url, payload=nil)
|
||||
uri = parse_url(url)
|
||||
transmit uri, Net::HTTP::Put.new(uri.path, headers), payload
|
||||
end
|
||||
|
||||
def self.delete(url)
|
||||
uri = parse_url(url)
|
||||
transmit uri, Net::HTTP::Delete.new(uri.path, headers)
|
||||
end
|
||||
|
||||
####
|
||||
|
||||
def self.parse_url(url)
|
||||
url = "http://#{url}" unless url.match(/^http/)
|
||||
URI.parse(url)
|
||||
end
|
||||
|
||||
def self.transmit(uri, req, payload=nil) # :nodoc:
|
||||
Net::HTTP.start(uri.host, uri.port) do |http|
|
||||
process_result http.request(req, payload)
|
||||
end
|
||||
end
|
||||
|
||||
class RequestFailed < Exception; end
|
||||
class Unauthorized < Exception; end
|
||||
|
||||
def self.process_result(res)
|
||||
if %w(200 201 202).include? res.code
|
||||
res.body
|
||||
elsif res.code == "401"
|
||||
raise Unauthorized
|
||||
else
|
||||
raise RequestFailed, error_message(res)
|
||||
end
|
||||
end
|
||||
|
||||
def self.parse_error_xml(body)
|
||||
xml(body).elements.to_a("//errors/error").map { |a| a.text }.join(" / ")
|
||||
rescue
|
||||
"unknown error"
|
||||
end
|
||||
|
||||
def self.error_message(res)
|
||||
"HTTP code #{res.code}: #{parse_error_xml(res.body)}"
|
||||
end
|
||||
|
||||
def self.headers
|
||||
{ 'Accept' => 'application/xml' }
|
||||
end
|
||||
|
||||
def self.xml(raw)
|
||||
REXML::Document.new(raw)
|
||||
end
|
||||
end
|
4
spec/base.rb
Normal file
4
spec/base.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
require 'rubygems'
|
||||
require 'spec'
|
||||
|
||||
require File.dirname(__FILE__) + '/../lib/rest'
|
75
spec/rest_spec.rb
Normal file
75
spec/rest_spec.rb
Normal file
|
@ -0,0 +1,75 @@
|
|||
require File.dirname(__FILE__) + '/base'
|
||||
|
||||
describe Rest do
|
||||
context "internal methods" do
|
||||
it "requests xml mimetype" do
|
||||
Rest.headers['Accept'].should == 'application/xml'
|
||||
end
|
||||
|
||||
it "converts an xml document" do
|
||||
REXML::Document.should_receive(:new).with('body')
|
||||
Rest.xml('body')
|
||||
end
|
||||
|
||||
it "processes a successful result" do
|
||||
res = mock("result")
|
||||
res.stub!(:code).and_return("200")
|
||||
res.stub!(:body).and_return('body')
|
||||
Rest.process_result(res).should == 'body'
|
||||
end
|
||||
|
||||
it "parses a url into a URI object" do
|
||||
URI.should_receive(:parse).with('http://example.com/resource')
|
||||
Rest.parse_url('http://example.com/resource')
|
||||
end
|
||||
|
||||
it "adds http:// to the front of resources specified in the syntax example.com/resource" do
|
||||
URI.should_receive(:parse).with('http://example.com/resource')
|
||||
Rest.parse_url('example.com/resource')
|
||||
end
|
||||
end
|
||||
|
||||
context "public API" do
|
||||
before do
|
||||
@uri = mock("uri")
|
||||
@uri.stub!(:path).and_return('/resource')
|
||||
Rest.should_receive(:parse_url).with('http://some/resource').and_return(@uri)
|
||||
end
|
||||
|
||||
it "GET url" do
|
||||
Net::HTTP::Get.should_receive(:new).with('/resource', Rest.headers).and_return(:get)
|
||||
Rest.should_receive(:transmit).with(@uri, :get)
|
||||
Rest.get('http://some/resource')
|
||||
end
|
||||
|
||||
it "POST url" do
|
||||
Net::HTTP::Post.should_receive(:new).with('/resource', Rest.headers).and_return(:post)
|
||||
Rest.should_receive(:transmit).with(@uri, :post, nil)
|
||||
Rest.post('http://some/resource')
|
||||
end
|
||||
|
||||
it "POST url, payload" do
|
||||
Net::HTTP::Post.should_receive(:new).with('/resource', Rest.headers).and_return(:post)
|
||||
Rest.should_receive(:transmit).with(@uri, :post, 'payload')
|
||||
Rest.post('http://some/resource', 'payload')
|
||||
end
|
||||
|
||||
it "PUT url" do
|
||||
Net::HTTP::Put.should_receive(:new).with('/resource', Rest.headers).and_return(:put)
|
||||
Rest.should_receive(:transmit).with(@uri, :put, nil)
|
||||
Rest.put('http://some/resource')
|
||||
end
|
||||
|
||||
it "PUT url, payload" do
|
||||
Net::HTTP::Put.should_receive(:new).with('/resource', Rest.headers).and_return(:put)
|
||||
Rest.should_receive(:transmit).with(@uri, :put, 'payload')
|
||||
Rest.put('http://some/resource', 'payload')
|
||||
end
|
||||
|
||||
it "DELETE url" do
|
||||
Net::HTTP::Delete.should_receive(:new).with('/resource', Rest.headers).and_return(:delete)
|
||||
Rest.should_receive(:transmit).with(@uri, :delete)
|
||||
Rest.delete('http://some/resource')
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue