Module: REST

Defined in:
lib/rest.rb,
lib/rest/error.rb,
lib/rest/request.rb,
lib/rest/response.rb

Overview

REST is basically a convenience wrapper around Net::HTTP. It defines a simple and consistant API for doing REST-style HTTP calls.

In addition it provides wrappers for the many error classes that can be raised while making requests. See REST::Error for a complete discussion of options.

Defined Under Namespace

Modules: Error Classes: DisconnectedError, Request, Response

Constant Summary collapse

VERSION =

Library version

'0.8.0'

Class Method Summary collapse

Class Method Details

.delete(uri, headers = {}, options = {}, &configure_block) ⇒ Object

Performs a DELETE on a resource. See REST::Request.new for a complete discussion of options.

response = REST.delete('http://example.com/pigeons/12')
if response.ok?
  puts "Your pigeon died ): )"
elsif response.found?
  puts "Someone moved your pigeon!"
else
  puts "Couldn't delete your pigeon (#{response.status_code})"
end

54
55
56
# File 'lib/rest.rb', line 54

def self.delete(uri, headers={}, options={}, &configure_block)
  REST::Request.perform(:delete, URI.parse(uri), nil, headers, options, &configure_block)
end

.get(uri, headers = {}, options = {}, &configure_block) ⇒ Object

Performs a GET on a resource. See REST::Request.new for a complete discussion of options.

response = REST.get('http://example.com/pigeons/12',
  {'Accept' => 'text/plain'},
  {:username => 'admin', :password => 'secret'}
)
if response.ok?
  puts response.body
else
  puts "Couldn't fetch your pigeon (#{response.status_code})"
end

26
27
28
# File 'lib/rest.rb', line 26

def self.get(uri, headers={}, options={}, &configure_block)
  REST::Request.perform(:get, URI.parse(uri), nil, headers, options, &configure_block)
end

.head(uri, headers = {}, options = {}, &configure_block) ⇒ Object

Performs a HEAD on a resource. See REST::Request.new for a complete discussion of options.

response = REST.head('http://example.com/pigeons/12')
if response.ok?
  puts "Your pigeon exists!"
elsif response.found?
  puts "Someone moved your pigeon!"
else
  puts "Couldn't fetch your pigeon (#{response.status_code})"
end

40
41
42
# File 'lib/rest.rb', line 40

def self.head(uri, headers={}, options={}, &configure_block)
  REST::Request.perform(:head, URI.parse(uri), nil, headers, options, &configure_block)
end

.patch(uri, body, headers = {}, options = {}, &configure_block) ⇒ Object

Performs a PATCH on a resource. See REST::Request.new for a complete discussion of options.

response = REST.patch('http://example.com/pigeons/12',
  {'Name' => 'Homer'}.to_xml,
  {'Accept' => 'application/xml, */*', 'Content-Type' => 'application/xml'}
)
if response.ok?
  puts "Your pigeon was renamed to 'Homer'!"
else
  puts "Couldn't rename your pigeon (#{response.status_code})"
  puts XML.parse(response.body).reason
end

70
71
72
# File 'lib/rest.rb', line 70

def self.patch(uri, body, headers={}, options={}, &configure_block)
  REST::Request.perform(:patch, URI.parse(uri), body, headers, options, &configure_block)
end

.post(uri, body, headers = {}, options = {}, &configure_block) ⇒ Object

Performs a POST on a resource. See REST::Request.new for a complete discussion of options.

response = REST.post('http://example.com/pigeons',
  {'Name' => 'Bowser'}.to_xml,
  {'Accept' => 'application/xml, */*', 'Content-Type' => 'application/xml'}
)
if response.created?
  puts "Created a new pigeon called 'Bowser'"
else
  puts "Couldn't create your pigeon (#{response.status_code})"
  puts XML.parse(response.body).reason
end

102
103
104
# File 'lib/rest.rb', line 102

def self.post(uri, body, headers={}, options={}, &configure_block)
  REST::Request.perform(:post, URI.parse(uri), body, headers, options, &configure_block)
end

.put(uri, body, headers = {}, options = {}, &configure_block) ⇒ Object

Performs a PUT on a resource. See REST::Request.new for a complete discussion of options.

response = REST.put('http://example.com/pigeons/12',
  {'Name' => 'Homer'}.to_xml,
  {'Accept' => 'application/xml, */*', 'Content-Type' => 'application/xml'}
)
if response.ok?
  puts "Your pigeon 'Bowser' was replaced by 'Homer'!"
else
  puts "Couldn't replace your pigeon (#{response.status_code})"
  puts XML.parse(response.body).reason
end

86
87
88
# File 'lib/rest.rb', line 86

def self.put(uri, body, headers={}, options={}, &configure_block)
  REST::Request.perform(:put, URI.parse(uri), body, headers, options, &configure_block)
end